Exemplo n.º 1
0
        /**************************************
         * ORDER FUNCTIONS BELOW
         * *************************************/

        ///<summary>
        ///This option takes a DB context and an order object and inserts it into the DB
        ///</summary>
        public void AddOrder(Order order)
        {
            //make an EF-Friendly Orders object
            Orders orders = new Orders();

            orders.CustomerId = order.CustomerID;
            orders.LocationId = order.LocationID;
            orders.OrderId    = order.OrderID;

            foreach (var item in order.itemsOrdered)
            {
                //make ProductsFromOrder object for each product
                ProductsFromOrder prodsInsert = new ProductsFromOrder();
                prodsInsert.OrderId   = order.OrderID;
                prodsInsert.Quantity  = item.Value;
                prodsInsert.ProductId = _dbContext.Products
                                        .Where(x => x.ProductName == item.Key)
                                        .Select(x => x.ProductId)
                                        .First();

                orders.ProductsFromOrder.Add(prodsInsert);
            }
            _dbContext.Add(orders);
            _dbContext.SaveChanges();

            var success = DecreaseInventory(order);

            if (success == false)
            {
                throw new Exception("There was an error updating the DB. Don't worry. Your order was placed successfully.");
            }
        }
Exemplo n.º 2
0
        ///<summary>
        ///This option not required at this time
        ///</summary>
        //public static List<Customer> ReadAllCustomers()
        //{
        //    return customers;
        //}

        ///<summary>
        ///This option not required at this time
        ///</summary>
        //public static List<Customer> UpdateCustomer(List<Customer> Customers, Customer customer)
        //{
        //    return Customers;
        //}

        ///<summary>
        ///This option not required at this time
        ///</summary>
        //public static List<Customer> DeleteCustomer(List<Customer> Customers, Customer customer)
        //{
        //    //Customers.Find(customer);//find out how to find and delete from a list.
        //    return Customers;
        //}

        /**************************************
         * ORDER FUNCTIONS BELOW
         * *************************************/

        ///<summary>
        ///This option takes a DB context and an order object and inserts it into the DB
        ///</summary>
        public static void AddOrder(Project0Context context, Order order)
        {
            Orders orders = new Orders();

            orders.CustomerId = order.CustomerID;
            orders.LocationId = order.LocationID;
            orders.OrderId    = order.OrderID;

            foreach (var item in order.itemsOrdered)
            {
                //make ProductsFromOrder object for each product
                ProductsFromOrder prodsInsert = new ProductsFromOrder();
                prodsInsert.OrderId   = order.OrderID;
                prodsInsert.Quantity  = item.Value;
                prodsInsert.ProductId = context.Products
                                        .Where(x => x.ProductName == item.Key)
                                        .Select(x => x.ProductId)
                                        .First();

                orders.ProductsFromOrder.Add(prodsInsert);
            }
            context.Add(orders);
            context.SaveChanges();
        }