/// <summary>
        /// Saves the order to the database
        /// </summary>
        /// <param name="order">The checked out order to be saved</param>
        public void saveOrder(lib.Order order)
        {
            try
            {
                var locationDict = getLocationDict();
                using var context = new StoreDBContext(_options);

                var dataOrder = new DataAccess.Order();
                dataOrder.CustomerId = order.CustomerId;
                dataOrder.OrderTime  = order.Time;
                dataOrder.LocationId = context.Locations.Where(l => l.Name == order.Location).Select(l => l.LocationId).FirstOrDefault();
                context.Add(dataOrder);
                context.SaveChanges();

                foreach (var p in order.Products)
                {
                    var dataOrderLine = new DataAccess.OrderLine();
                    dataOrderLine.OrderId   = dataOrder.OrderId;
                    dataOrderLine.ProductId = p.ProductId;
                    dataOrderLine.Amount    = p.Amount;
                    context.Add(dataOrderLine);
                }

                context.SaveChanges();
            }
            catch (Exception)
            {
                Console.WriteLine("Error creating new orders");
            }
        }
        /// <summary>
        /// Saves the order to the database
        /// </summary>
        /// <param name="order">The checked out order to be saved</param>
        public void saveOrder(lib.Order order)
        {
            try
            {
                var dataOrder    = new DataAccess.Order();
                var dataCustomer = _context.Customers.Where(c => c.CustomerId == order.CustomerId).FirstOrDefault();
                dataOrder.CustomerId = order.CustomerId;
                dataOrder.OrderTime  = DateTime.Parse(order.Time);
                dataOrder.LocationId = _context.Locations.Where(l => l.Name == order.Location).Select(l => l.LocationId).FirstOrDefault();
                _context.Add(dataOrder);
                _context.SaveChanges();

                foreach (var p in order.Products)
                {
                    var dataOrderLine = new DataAccess.OrderLine();
                    dataOrderLine.OrderId   = dataOrder.OrderId;
                    dataOrderLine.ProductId = p.ProductId;
                    dataOrderLine.Amount    = p.Amount;
                    dataCustomer.Balance   -= p.Amount * p.Price;
                    _context.Add(dataOrderLine);
                }
                _context.Update(dataCustomer);
                _context.SaveChanges();
            }
            catch (Exception)
            {
                Console.WriteLine("Error creating new orders");
            }
        }
Пример #3
0
        public void handleStoreOptions()
        {
            bool exit = false;

            while (!exit)
            {
                try
                {
                    _outputter.printString("\n1 - Customer/Location\n2 - View Inventory\n3 - Add To Cart\n4 - View Cart\n5 - Checkout\n6 - Exit\n");
                    int userInput = _inputter.getNumber();

                    switch (userInput)
                    {
                    case 1:
                        handleCustomerInfo();
                        break;

                    case 2:
                        _outputter.printInventory(_locationInventory);
                        break;

                    case 3:
                        _outputter.printString("Enter the ProductId.");
                        var productInput = _inputter.getNumber();
                        _outputter.printString("How many to purchase?");
                        var numInput = _inputter.getNumber();

                        _location.addToCart(productInput, numInput, _locationInventory);
                        break;

                    case 4:
                        _outputter.printCart(_locationInventory, _location.Cart);
                        break;

                    case 5:
                        lib.Order finalOrder = _location.checkout(_orders, _customer, _locationInventory);

                        _storeRespository.saveAllInventory(_inventory);
                        _storeRespository.saveOrder(finalOrder);
                        _storeRespository.saveCustomer(_customer);
                        break;

                    case 6:
                        exit = true;
                        break;

                    default:
                        _outputter.printString("Invalid Input.");
                        break;
                    }
                }
                catch (Exception e)
                {
                    _outputter.printString("Invalid Input.");
                }
            }
        }
 public void addNewOrder(lib.Order order)
 {
     _dataRepository.saveOrder(order);
 }