public async Task <ActionResult <Models.Cart> > Post(CartForCreation cartForCreation) { var cart = _mapper.Map <Entities.Cart>(cartForCreation); _cartRepository.AddCart(cart); await _cartRepository.SaveChanges(); var cartToReturn = _mapper.Map <Models.Cart>(cart); return(CreatedAtRoute( "GetCart", new { cartId = cart.CartId }, cartToReturn)); }
public async Task <IActionResult> AddToCart(string cartId, Guid ProductId, CartForCreation cartForCreationDto) { if (await _repo.GetCart(cartId) == null) { await _repo.CreateNewCart(new Cart(), cartId); } var getCartFormRepo = await _repo.GetCart(cartId); var getProductFromRepo = await _repo.GetProduct(ProductId); if (getProductFromRepo != null) { var cartItem = _mapper.Map <CartItem> (cartForCreationDto); foreach (CartItem item in getCartFormRepo.Items) { if (item.ProductId == ProductId) { cartItem.ProductId = ProductId; item.Quantity++; if (await _repo.SaveChanges()) { var cartItemToReturn = _mapper.Map <CartItemToReturn> (item); return(CreatedAtRoute(nameof(GetCart), new { cartId, item.ItemId }, cartItemToReturn)); } } } cartItem.ProductId = ProductId; cartItem.ProductName = getProductFromRepo.Name; cartItem.ProductPrice = getProductFromRepo.Price; cartItem.ProductPhoto = getProductFromRepo.Photos.FirstOrDefault().Url; getCartFormRepo.Items.Add(cartItem); if (await _repo.SaveChanges()) { var cartItemToReturn = _mapper.Map <CartItemToReturn> (cartItem); return(CreatedAtRoute(nameof(GetCart), new { cartId, cartItem.ItemId }, cartItemToReturn)); } } return(BadRequest("there is an err ")); }