Example #1
0
        /// <summary>
        /// Take a finalized order from the model, and convert it into a db order, adjust store invintories,
        /// and insert it.
        /// </summary>
        /// <exception cref="NullReferenceException">
        /// This might throw a null reference if the store has no invintory for an item in the order in the database.
        /// </exception>
        /// <param name="o">A model order. This order should have had Finalize() called on it, as this method doesn't</param>
        public void PlaceOrder(Store.Order o)
        {
            using Project0DBContext DBContext = new Project0DBContext(dbContextOptions);

            Order newOrder = new Order();
            //default is null.
            int nextid = (DBContext.Orders.OrderByDescending(cust => cust.Id).FirstOrDefault()?.Id ?? 0) + 1;

            newOrder.Id            = nextid;
            newOrder.StoreLocation = o.OrderLoc.Where;
            newOrder.Customer      = GetDBCustomerByName(DBContext, o.Customer.CustomerName);
            newOrder.OrderTime     = o.Time;

            decimal total = 0;

            foreach (ItemCount item in o.Items)
            {
                total += item.Count * (Decimal)item.ThisItem.cost;

                OrderItem orderItem = new OrderItem();
                orderItem.Order    = newOrder;
                orderItem.Quantity = item.Count;
                orderItem.ItemId   = item.ThisItem.name;
                newOrder.OrderItems.Add(orderItem);

                //change store stocks, Assumes there's already an invintory entry, otherwise throws exception.
                Invintory iv = DBContext.Invintories.Find(o.OrderLoc.Where, item.ThisItem.name);
                iv.Quantity -= item.Count;
            }
            newOrder.OrderTotal = total;

            DBContext.Orders.Add(newOrder);
            DBContext.SaveChanges();
        }
Example #2
0
        /// <summary>
        /// Takes a model customer, creates a DB customer, and sends it to the database. Will register
        /// the customer with the data model if the customer isn't already.
        /// </summary>
        /// <remarks>
        /// May throw exceptions if the store name is over 100 characters, or doesn't exist in the DB.
        /// </remarks>
        /// <param name="customer">The model customer.</param>
        public void CreateCustomer(Store.Customer customer)
        {
            using Project0DBContext DBContext = new Project0DBContext(dbContextOptions);
            if (!Customers.Instance.HasCustomer(customer.CustomerName))
            {
                Customers.Instance.RegisterCustomer(customer);
            }

            DataModel.Customer newcustomer = new Customer();
            newcustomer.Id        = (DBContext.Customers.OrderByDescending(cust => cust.Id).Take(1).First().Id) + 1;
            newcustomer.LastName  = customer.CustomerName.Last;
            newcustomer.FirstName = customer.CustomerName.First;

            if (customer.CustomerName.MiddleInitial != null)
            {
                newcustomer.MiddleInitial = customer.CustomerName.MiddleInitial.ToString();
            }

            if (customer.DefaultStore != null)
            {
                //TODO: limit to 100 characters.
                newcustomer.StoreLocation = customer.DefaultStore.Where;
            }

            DBContext.Customers.Add(newcustomer);
            DBContext.SaveChanges();
        }