void Istoreinventory.Updateinventory(string store_phonenumber, string product, int quantity) { Store storequery = _context.Stores.Where(x => x.StorePhoneNumber.Equals(store_phonenumber)).First(); Models.Product newproduct = new Models.Product(); Product productquery = _context.Products.Where(x => x.ProductName.Equals(product)).First(); StoreInventory storeInventory = _context.StoreInventories.FirstOrDefault(x => x.StoreId.Equals(storequery.Id) && x.ProductId == productquery.Id); if (storeInventory == null) { StoreInventory storei = new StoreInventory(); storei.ProductId = productquery.Id; storei.ProductQuantity = quantity; storei.StoreId = storequery.Id; _context.Add(storei); _context.SaveChanges(); } else { storeInventory.ProductQuantity = quantity; _context.Update(storeInventory); _context.SaveChanges(); } }
void IOrder.ChangeInventory(int id, int quantity, int productid) { StoreInventory dbInventory = _context.StoreInventories.Where(x => x.StoreId.Equals(id) && x.ProductId.Equals(productid)).FirstOrDefault(); if (dbInventory.ProductQuantity < 1) { throw new ArgumentException("Item is out of stock!"); } else { int itemquantity = dbInventory.ProductQuantity - quantity; dbInventory.ProductQuantity = itemquantity; } _context.Update(dbInventory); _context.SaveChanges(); }