public async Task <ActionResult <ApiResponse <Cart> > > AddProduct(AddCartProductDto dto) { try { var createdCart = await _cartService.AddCartProduct(dto); return(createdCart.CreateSuccessResponse("Cart created successfully!")); } catch (Exception exception) { return(BadRequest(exception.CreateErrorResponse())); } }
private CartProduct _prepareCartProduct(Product product, AddCartProductDto dto) { var cartProduct = new CartProduct { ProductId = product.ID, Quantity = dto.Quantity, SKU = product.SKU, Title = product.Title, UnitPrice = product?.Pricing.Price ?? 0, Size = dto.Size, Color = dto.Color }; return(cartProduct); }
private async Task <Cart> _addProductToExistingCart(AddCartProductDto dto) { var product = await _productService.GetProductById(dto.ProductId); var cart = await GetCartById(dto.CartId); var cartProduct = _prepareCartProduct(product, dto); cart.Products.Add(cartProduct); if (dto.CustomerId == "") { dto.CustomerId = null; } cart.CustomerId = dto.CustomerId; var updatedCart = await _updateCartById(cart.ID, cart); updatedCart = await _populateCartProductsImage(updatedCart); return(updatedCart); }
public async Task <Cart> AddCartProduct(AddCartProductDto dto) { if (!string.IsNullOrWhiteSpace(dto.CartId)) { return(await _addProductToExistingCart(dto)); } var product = await _productService.GetProductById(dto.ProductId); if (product == null) { return(null); } var productColor = product.ProductColors.FirstOrDefault(x => x.ColorCode.ToLower() == dto.Color.ToLower()); if (productColor == null) { return(null); } if (!productColor.Sizes.Any(productSize => productSize == dto.Size)) { return(null); } var cartProduct = _prepareCartProduct(product, dto); if (dto.CustomerId == "") { dto.CustomerId = null; } var cart = new Cart { CustomerId = dto.CustomerId, Products = new List <CartProduct> { cartProduct }, Status = CartStatus.Active }; var createdCart = await _addCart(cart); return(createdCart); }