Last active
June 19, 2020 05:11
-
-
Save mickaelw/3f3ac8b565d8be40d8f71af0c414da12 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface BasketBody { | |
basket_items: string[] | |
} | |
interface BasketResponse { | |
basket_items: string[] | |
} | |
export class HTTPBasketRepository implements BasketRepository { | |
private endpoint = 'https://www.fakeapi.com/baskets' | |
constructor(private httpClient: HTTPClient) { | |
} | |
getBasket(): Promise<Basket> { | |
return this.httpClient.get<BasketResponse>(this.endpoint) | |
.then(this.mapToBasket) | |
} | |
save(basket: Basket): Promise<void> { | |
return this.httpClient.post<BasketBody>(this.endpoint, this.mapToBookBody(basket)) | |
} | |
private mapToBasket(basketResponse: BasketResponse): Basket { | |
const basket = new Basket() | |
basketResponse.basket_items.forEach(item => basket.add(new Book(item))) | |
return basket | |
} | |
private mapToBookBody(basket: Basket): BasketBody { | |
return { basket_items: basket.items.map(item => item.title) } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment