public async Task <Result> AddProductAsync(CartIteamRequestModel model, string userId) { var isHas = await AllAsNoTracking .AnyAsync(x => x.ProductAttributeId == model.ProductAttributeId && x.UserId == userId) .ConfigureAwait(false); var productId = model.ProductAttributeId; var quantity = model.Quantity; var productAttribute = await GetProductAttribute(productId) .ConfigureAwait(false); if (productAttribute == null) { return(NotFound); } if (quantity > productAttribute.StockQuantity) { return(NotEnoughProductsMessage); } if (!isHas) { var cartItem = new CartItem { UserId = userId, ProductAttribute = productAttribute, Quantity = quantity }; await Data.AddAsync(cartItem) .ConfigureAwait(false); } else { var result = await All .FirstOrDefaultAsync(x => x.ProductAttributeId == productId && x.UserId == userId) .ConfigureAwait(false); if (result == null) { return(InvalidErrorMessage); } result.Quantity = quantity; } await Data.SaveChangesAsync() .ConfigureAwait(false); return(Result.Success); }
public async Task <ActionResult> AddProduct(CartIteamRequestModel model) { return(await _cartItemsService .AddProductAsync(model, _currentUser.UserId) .ToActionResult()); }