示例#1
0
        // Multi-purpsoe
        public void CustomerPlaceOneOrder(COrder order, CStore store, double totalCost)
        {
            using var context = new Project0databaseContext(_contextOptions);
            // update order
            var newOrder = new Orderr
            {
                Orderid     = order.Orderid,
                Storeloc    = order.StoreLocation.Storeloc,
                Customerid  = order.Customer.Customerid,
                Orderedtime = DateTime.Now,
                Totalcost   = totalCost
            };

            context.Orderrs.Add(newOrder);
            context.SaveChanges();

            // update Orderproduct
            foreach (var product in order.ProductList)
            {
                var newOP = new Orderproduct
                {
                    Orderid   = order.Orderid,
                    Productid = product.UniqueID,
                    Quantity  = product.Quantity
                };
                context.Orderproducts.Add(newOP);
            }
            context.SaveChanges();

            var dbStore = context.Stores.Include(x => x.Inventories)
                          .FirstOrDefault(x => x.Storeloc == order.StoreLocation.Storeloc);

            // if (dbStore == null) return null;
            // update inventory quantity
            foreach (var product in order.ProductList)
            {
                foreach (var dbProd in dbStore.Inventories)
                {
                    if (product.UniqueID == dbProd.Productid)
                    {
                        dbProd.Quantity = store.Inventory[product.UniqueID].Quantity;
                    }
                }
            }
            context.SaveChanges();
        }