예제 #1
0
        public async Task <IActionResult> AddItemToBasket(BasketItemCreateViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Invalid model"));
            }

            BasketViewModel basket = await _basketViewModelService.GetOrCreateBasketForUserAsync(User.Identity.Name);

            var priceResult = Pounds.Create(model.PriceInPounds);

            if (priceResult.IsFailure)
            {
                return(BadRequest(priceResult.Error));
            }

            var result = await _basketService.AddItemToBasketAsync(basket.Id, model.ProductId, model.Quantity, priceResult.Value);

            if (result.IsFailure)
            {
                return(NotFound(result.Error));
            }

            BasketItemViewModel item = await _basketViewModelService.GetBasketItemForUserAsync(User.Identity.Name, result.Value);

            return(CreatedAtAction("GetItem", new { id = item.Id }, item));
        }
        public async Task <IActionResult> GetBasket()
        {
            BasketViewModel basket = await _basketViewModelService.GetOrCreateBasketForUserAsync(User.Identity.Name);

            return(Ok(basket));
        }