public async Task <IActionResult> AddItems([FromBody] AddItemsRequest request) { if (request == null || request.Quantity <= 0) { return(BadRequest()); } var customer = await GetCustomer(); try { await _serviceProvider.New <BasketService>().AddItemsToBasket(customer.Id, request); } catch (Exception ex) { return(BadRequest(ex.Message)); } return(Ok()); }
public async Task AddItemsToBasket(Guid customerId, AddItemsRequest request) { var basket = await GetCustomerBasket(customerId); if (basket == null) { basket = await CreateBasketForCustomer(customerId); } var item = basket.Items?.FirstOrDefault(x => x.ProductId == request.ProductId); if (item != null) { item.Quantity += request.Quantity; await _checkoutContext.SaveChangesAsync(); } else { var products = await _productService.GetProducts(); if (products.Any(x => x.Id == request.ProductId)) { item = new Item { UpdatedAt = DateTime.Now, BasketId = basket.Id, ProductId = request.ProductId, Quantity = request.Quantity }; await _checkoutContext.Items.AddAsync(item); await _checkoutContext.SaveChangesAsync(); } else { throw new Exception("The specified product does not exist"); } } }
public async Task AddItemsToBasket(AddItemsRequest request) { await _http.Put("api/basket/items", request); }