public HttpResponseMessage UpdateProducts([FromUri] string email, List <Product> products) { ProductPurchased purchased = new ProductPurchased(); Product product; decimal count = 0; if (products == null) { return(Request.CreateResponse(HttpStatusCode.BadRequest, ModelState)); } Sale sale = new Sale(); sale.SaleDate = DateTime.Today; sale.Email = email; db.Sales.Add(sale); foreach (var items in products) { sale.TotalAmount += items.InventoryCount * items.Price; } foreach (var item in products) { product = db.Products.Find(item.ProductId); product.InventoryCount = product.InventoryCount - item.InventoryCount; purchased.ProductId = item.ProductId; purchased.Quantity = item.InventoryCount; purchased.SaleId = sale.SaleId; db.ProductPurchased.Add(purchased); //db.Entry(product).State = EntityState.Modified; //db.Entry(purchased).State = EntityState.Modified; //db.Entry(sale).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!ProductExists(item.ProductId)) { return(Request.CreateResponse(HttpStatusCode.NoContent)); } else { throw; } } } return(Request.CreateResponse(HttpStatusCode.OK)); }
public static void OnProductPurchased(ProductData data) => ProductPurchased?.Invoke(data);