public async Task <ShoppingCartDto> AddOrUpdateProduct(int shoppingCartId, ShoppingCartProductDto shoppingCartProductDto, CancellationToken cancellationToken) { using (var client = GetClient()) { var response = await client.PostAsync($"{ShoppingCartUrl}/{shoppingCartId}", shoppingCartProductDto, new JsonMediaTypeFormatter(), cancellationToken).ConfigureAwait(false); response.EnsureSuccessStatusCode(); return(await response.Content.ReadAsAsync <ShoppingCartDto>().ConfigureAwait(false)); } }
public IHttpActionResult Create([FromBody] ShoppingCartProductDto shoppingCartProductDto) { var shoppingCartProduct = new ShoppingCartProduct() { ShoppingCartId = shoppingCartProductDto.ShoppingCartId, ProductId = shoppingCartProductDto.ProductId, Quantity = shoppingCartProductDto.Quantity }; shoppingCartProductRepository.Add(shoppingCartProduct); shoppingCartProductRepository.SaveChanges(); return(Ok(shoppingCartProduct)); }
public IHttpActionResult Update(int shoppingCartId, int productId, [FromBody] ShoppingCartProductDto shoppingCartProductDto) { if (shoppingCartId != shoppingCartProductDto.ShoppingCartId || productId != shoppingCartProductDto.ProductId) { return(BadRequest()); } var shoppingCartProduct = new ShoppingCartProduct() { ShoppingCartId = shoppingCartProductDto.ShoppingCartId, ProductId = shoppingCartProductDto.ProductId, Quantity = shoppingCartProductDto.Quantity }; shoppingCartProductRepository.Update(shoppingCartProduct); shoppingCartProductRepository.SaveChanges(); return(Ok(shoppingCartProduct)); }
public ActionResult <ShoppingCartDto> AddOrUpdateProduct(int id, [FromBody] ShoppingCartProductDto product) { var cart = _dataSession.GetSet <ShoppingCartDto, ShoppingCart>().FirstOrDefault(x => x.Id == id); if (cart == null) { return(NotFound()); } var p = cart.Products?.FirstOrDefault(x => x.Id == product.Id); if (p != null) { cart.Products.Remove(p); } cart.Products.Add(product); return(_dataSession.Save <ShoppingCartDto, ShoppingCart>(cart)); }