public async Task <Result <string> > AddToCart(CreateCartInput input) { Cart cart = await _cartRepository.GetCartByUserId(input.UserId); if (cart == null) { List <CartItem> cartItems = new List <CartItem>(); input.CartItems.ForEach(item => { cartItems.Add(new CartItem() { Id = Guid.NewGuid().ToString(), Image = item.Image, ProductType = item.ProductType, Format = item.Format, Quantity = item.Quantity, Price = item.Price, Title = item.Title }); }); cart = new Cart() { UserId = input.UserId, CartItems = cartItems, Price = CalculateCartPrice(cartItems) }; return(new Result <string>() { Success = true, Data = await _cartRepository.CreateAsync(cart) }); } else { input.CartItems.ForEach(item => cart.CartItems.Add(new CartItem() { Id = Guid.NewGuid().ToString(), Image = item.Image, ProductType = item.ProductType, Format = item.Format, Quantity = item.Quantity, Price = item.Price, Title = item.Title })); cart.Price = CalculateCartPrice(cart.CartItems); await _cartRepository.UpdateAsync(cart); return(new Result <string>() { Success = true, Data = cart.Id }); } }
public async Task <IActionResult> AddToCart([FromForm] CreateCartInput input) { Result <string> result = await _cartManager.AddToCart(input); if (result.Success) { return(Ok(result)); } else { return(BadRequest(result)); } }