public async Task Delete(string id) { ShopApp.Models.Product product = this.dbContext.Products.FirstOrDefault(p => p.Id == id); // we delete the product only if it already exists if (product != null) { this.dbContext.Products.Remove(product); await this.dbContext.SaveChangesAsync(); } }
public async Task Edit(ProductBaseInputModel model) { ShopApp.Models.Product productEntity = this.dbContext.Products .FirstOrDefault(product => product.Id == ((ProductEditModel)model).Id); if (productEntity == null) { throw new InvalidOperationException("Invalid product id!"); } productEntity.Name = model.Name; productEntity.Price = model.Price; productEntity.Description = model.Description; productEntity.CoverUrl = model.CoverUrl; productEntity.CategoryId = model.CategoryId; await this.dbContext.SaveChangesAsync(); }
public async Task <ProductBaseInputModel> Create(ProductBaseInputModel model) { ShopApp.Models.Product productEntity = new ShopApp.Models.Product { Id = Guid.NewGuid().ToString(), Name = model.Name, AddedOn = DateTime.UtcNow, Description = model.Description, CoverUrl = model.CoverUrl, Price = model.Price, CategoryId = model.CategoryId }; this.dbContext.Products.Add(productEntity); await this.dbContext.SaveChangesAsync(); return(model); }
public async Task DecrementProductStockCount(string id, int quantity) { bool productExists = this.Exists(id); if (productExists) { ShopApp.Models.Product product = this.dbContext.Products.FirstOrDefault(p => p.Id == id); if (product.StockCount - quantity < 0) { throw new InvalidOperationException($"Not enough quantity of {product.Name} product."); } product.StockCount = product.StockCount - quantity; await this.dbContext.SaveChangesAsync(); } else { throw new InvalidOperationException($"Invalid product id."); } }