Esempio n. 1
0
        /// <summary>
        /// Enter a new order into db
        /// </summary>
        /// <param name="appOrder"></param>
        public void CreateOrder(Order appOrder)
        {
            using var context = new P1DbContext(_contextOptions);
            //map library order to db
            var dbOrder = new OrderEntity()
            {
                Store    = context.Stores.First(s => s.Id == appOrder.TargetStore.Id),
                Customer = context.Customers.First(c => c.Id == appOrder.Orderer.Id),
                Time     = appOrder.Time
            };

            //map all items in the order to db
            foreach (Product selection in appOrder.Selections)
            {
                //create a new item, Store = null unless item is part of an inventory
                var dbOrderItems = new OrderItemsEntity()
                {
                    Product  = context.Products.First(p => p.Name == selection.Name),
                    Quantity = selection.Quantity,
                    Order    = dbOrder
                };
                context.Add(dbOrderItems);
            }
            context.Add(dbOrder);
            context.SaveChanges();
        }
Esempio n. 2
0
        /// <summary>
        /// Enter a new customer into db, and app
        /// </summary>
        /// <param name="customerName"></param>
        public void CreateCustomer(string firstName, string lastName, List <Customer> customers)
        {
            using var context = new P1DbContext(_contextOptions);
            int newId = context.Customers.OrderBy(x => x.Id).Last().Id + 1;

            //add to app
            Customer newCustomer = new Customer(firstName, lastName, newId);

            customers.Add(newCustomer);

            //add to db

            var dbCustomer = new CustomerEntity()
            {
                FirstName = firstName, LastName = lastName
            };

            context.Add(dbCustomer);
            context.SaveChanges();
        }