Пример #1
0
 /// <summary>
 /// Turn a location into an entity location
 /// </summary>
 /// <param name="location"></param>
 /// <returns></returns>
 public static Entities.LocationEntity Map(Library.Models.Location location)
 {
     return(new Entities.LocationEntity
     {
         Id = location.ID,
         Name = location.LocationName
     });
 }
Пример #2
0
 /// <summary>
 /// This turns a Location Model into a Location entity, by assigning each relavent property
 /// to a column in the Location table
 /// </summary>
 /// <param name="location">The location model.</param>
 /// <returns new Entity.Location></returns>
 public static Entities.LocationEntity MapLocationWithOrders(Library.Models.Location location)
 {
     return(new Entities.LocationEntity
     {
         Id = location.ID,
         Name = location.LocationName,
         Orders = location.OrderHistory.Select(Mapper_Order.MapOrderWithOrderLines).ToList()
     });
 }
Пример #3
0
 public static Location MapLibLocation(Library.Models.Location storeLocation)
 {
     return(new Location()
     {
         LocationId = storeLocation.Id,
         LocationName = storeLocation.Name,
         OrderHistory = storeLocation.OrderHistory.Select(LibOrdersMap).ToList()
     });
 }
        /// <summary>
        /// Retrieve all orders in the database submitted from a particular location
        /// </summary>
        /// <returns>A group of Business-Model order objects</returns>
        public List <Library.Models.Order> GetLocationOrders(Library.Models.Location location)
        {
            var dbOrders = _dbContext.Orders
                           .Where(o => o.LocationId == location.Id)
                           .OrderByDescending(o => o.Date).ToList();
            List <Library.Models.Order> orders = new List <Library.Models.Order>();

            foreach (var order in dbOrders)
            {
                orders.Add(GetOrderById(order.Id));
            }
            return(orders);
        }
        /// <summary>
        /// Retrieve Business-Model order object from database via order id
        /// </summary>
        /// <param name="id">Order id</param>
        /// <returns>Business-Model order object</returns>
        public Library.Models.Order GetOrderById(int id)
        {
            var dbOrder = _dbContext.Orders
                          .Include(o => o.Location)
                          .Include(o => o.Customer)
                          .Include(o => o.OrderContents)
                          .ThenInclude(oc => oc.Product)
                          .FirstOrDefault(o => o.Id == id);

            if (dbOrder is null)
            {
                return(null);
            }

            var location = new Library.Models.Location(
                dbOrder.Location.Name,
                dbOrder.Location.Address,
                dbOrder.Location.City,
                dbOrder.Location.State,
                dbOrder.Location.Country,
                dbOrder.Location.PostalCode,
                dbOrder.Location.Phone
                )
            {
                Id = dbOrder.LocationId
            };
            var customer = new Library.Models.Customer()
            {
                Id        = dbOrder.CustomerId,
                FirstName = dbOrder.Customer.FirstName,
                LastName  = dbOrder.Customer.LastName,
                Email     = dbOrder.Customer.Email
            };

            Dictionary <int, int>     products = new Dictionary <int, int>();
            Dictionary <int, decimal> prices   = new Dictionary <int, decimal>();

            foreach (OrderContent oc in dbOrder.OrderContents)
            {
                products.Add(oc.ProductId, oc.Quantity);
                prices.Add(oc.ProductId, oc.Price);
            }

            return(new Library.Models.Order()
            {
                Id = dbOrder.Id, Products = products, PricePaid = prices, Customer = customer, Location = location, Time = dbOrder.Date
            });
        }
        /// <summary>
        /// Add a new store location to the database
        /// </summary>
        /// <param name="location">Business-Model location object</param>
        public void AddLocation(Library.Models.Location location)
        {
            if (location.Id != 0)   // IDs are assigned by database, so it already exists if not 0
            {
                throw new ArgumentException("Location already exists.");
            }

            var dbLocation = new Location()
            {
                Name       = location.Name,
                Address    = location.Address,
                City       = location.City,
                State      = location.State,
                Country    = location.Country,
                PostalCode = location.Zip,
                Phone      = location.Phone
            };

            _dbContext.Locations.Add(dbLocation);
        }
        //
        // Update Methods
        //

        /// <summary>
        /// Update the amount of some product that a particular location has in stock
        /// </summary>
        /// <param name="location">Business-Model location object</param>
        /// <param name="product">Business-Model product object</param>
        /// <param name="qty">integer number of items to add</param>
        public void UpdateLocationStock(Library.Models.Location location, Library.Models.Product product)
        {
            LocationInventory dbLocationInventory;

            try {
                dbLocationInventory       = _dbContext.LocationInventories.First(x => x.LocationId == location.Id && x.ProductId == product.Id);
                dbLocationInventory.Stock = location.Stock[product];
            } catch (InvalidOperationException) {
                var dbLocation = _dbContext.Locations.First(l => l.Id == location.Id);
                var dbProduct  = _dbContext.Products.First(p => p.Id == product.Id);
                dbLocationInventory = new LocationInventory()
                {
                    Location = dbLocation,
                    Product  = dbProduct,
                    Stock    = location.Stock[product],
                    Price    = location.Prices[product]
                };
                _dbContext.LocationInventories.Add(dbLocationInventory);
            }
        }
        //
        // Update Methods
        //

        /// <summary>
        /// Update the amount of some product that a particular location has in stock
        /// </summary>
        /// <param name="location">Business-Model location object</param>
        /// <param name="product">Business-Model product object</param>
        /// <param name="qty">integer number of items to add</param>
        public void UpdateLocationStock(Library.Models.Location location)
        {
            if (location is null)
            {
                throw new ArgumentNullException(nameof(location));
            }
            foreach (var product in location.Stock)
            {
                int productId = product.Key;
                LocationInventory dbLocationInventory;
                try {
                    dbLocationInventory       = _dbContext.LocationInventories.First(x => x.LocationId == location.Id && x.ProductId == productId);
                    dbLocationInventory.Stock = location.Stock[productId];
                } catch (InvalidOperationException) {
                    var dbLocation = _dbContext.Locations.FirstOrDefault(l => l.Id == location.Id);

                    if (dbLocation is null)
                    {
                        throw new InvalidOperationException("Tried to update stock of a nonexistent location.");
                    }

                    var dbProduct = _dbContext.Products.FirstOrDefault(p => p.Id == productId);

                    if (dbProduct is null)
                    {
                        throw new InvalidOperationException("Tried to update stock of a nonexistent product.");
                    }

                    dbLocationInventory = new LocationInventory()
                    {
                        Location = dbLocation,
                        Product  = dbProduct,
                        Stock    = location.Stock[productId],
                        Price    = location.Prices[productId]
                    };
                    _dbContext.LocationInventories.Add(dbLocationInventory);
                }
            }
        }
Пример #9
0
        /// <summary>
        /// The purpose is to turn the db information for a given locations order history into readable text.
        /// </summary>
        /// <param name="locationID"></param>
        /// <returns></returns>
        public string GetOrderHistoryByLocationID(int locationID)
        {
            // This method is called because we need the information on the whole catalog
            // Since the catalog is small I went with this implementation.
            // If it was much large I would only fill the library with the relevant books
            FillBookLibrary();

            string results = "";

            // Find if the location exists and include all information including orders and their orderlines
            LocationEntity dbLocation = _context.Locations
                                        .Include(o => o.Orders)
                                        .ThenInclude(ol => ol.Orderlines)
                                        .FirstOrDefault(l => l.Id == locationID);

            // If it doesn't exist then return that the location does not exist.
            if (dbLocation == null)
            {
                return("This location id does not exist.");
            }

            // If it does then map the location to their orders
            Library.Models.Location m_location = Mapper_Location.MapLocationWithOrders(dbLocation);

            // Start building the string with the locatio information first
            results += m_location;
            foreach (Library.Models.Order order in m_location.OrderHistory)
            {
                // Then for each order add them to the string as well.
                results += $"\n{order}";
            }
            if (results == m_location.ToString())
            {
                // If the location exists but no orders have been placed let the user know
                results += "\nNo orders have been placed at this location.";
            }
            return(results);
        }
Пример #10
0
        public ActionResult PlaceOrder(string[] ToppingListForm, OrderWeb order)
        {
            string numP = (string)TempData.Peek("numP");
            string loc  = (string)TempData.Peek("loc");

            order.PizzaCountString = numP; //this is a string need to convert to int
            order.Address          = loc;
            order.PizzaCountInt    = Convert.ToInt32(numP);
            List <bool> ToppingList = new List <bool>();

            for (int i = 0; i < order.NumOfToppings; i++)
            {
                ToppingList.Add(false);
            }
            for (int i = 0; i < order.PizzaCountInt; i++)
            {
                order.PizzaDictionary.Add(i, ToppingList);
            }



            int boolIndex = 0;

            for (int x = 0; x < order.PizzaCountInt; x++)
            {
                List <bool> OrderToppingList = new List <bool>();
                for (int i = 0; i < order.NumOfToppings; i++)
                {
                    if (ToppingListForm[boolIndex] == "true")
                    {
                        OrderToppingList.Add(true);
                        boolIndex++;
                    }
                    else
                    {
                        OrderToppingList.Add(false);
                    }
                    boolIndex++;
                }
                order.PizzaDictionary[x] = OrderToppingList;
            }
            //inventory logic
            order.LocationId = Repo.GetLocationIdByName(order.Address);
            //get inventory from db by location id (for pizza and cheese)
            var PepperoniInventory = Repo.GetLocationPepperoniInventoryById(order.LocationId);
            var CheeseInventory    = Repo.GetLocationCheeseInventoryById(order.LocationId);

            for (int x = 0; x < order.PizzaCountInt; x++)
            {
                order.PizzaIDs.Add(Repo.FindPizzaIdByToppings(order.PizzaDictionary[x]));
            }
            //subtract for each topping on each pizza
            for (int x = 0; x < order.PizzaCountInt; x++) //pepperoni first
            {
                if (order.PizzaDictionary[x][0])
                {
                    PepperoniInventory -= 1;
                }
                if (order.PizzaDictionary[x][1])
                {
                    CheeseInventory -= 1;
                }
            }
            //check if less than 0
            if ((PepperoniInventory < 0) || (CheeseInventory < 0))
            {
                ModelState.AddModelError("", "Sorry we do not have enough inventory at this location to create your order");
                return(View(order));
            }
            //if less than 0 add it back and return view
            //ModelState.AddModelError("", "Sorry we do not have enough inventory at this location to create your order");
            //return View(order);


            order.OrderTime = DateTime.Now;
            order.UserId    = (int)TempData.Peek("id");

            decimal runningTotal = 0.00m;

            foreach (var id in order.PizzaIDs)
            {
                runningTotal += Repo.FindPriceByPizzaID(id);
            }
            order.TotalPrice = runningTotal;
            if ((Decimal.Compare(runningTotal, 500.00m)) > 0) //compare to $500
            {
                ModelState.AddModelError("", "Sorry we cannot accept your order, it exceeds $500");
                return(View(order));
            }
            var libOrder = MapperWeb.Map(order);
            var location = new Library.Models.Location
            {
                LocationID         = order.LocationId,
                Address            = Repo.GetLocationNameById(order.LocationId),
                PepperoniInventory = PepperoniInventory,
                CheeseInventory    = CheeseInventory
            };

            Repo.UpdateLocationInventory(location);
            Repo.Save();
            Repo.AddOrder(libOrder);
            Repo.Save();
            order.OrderId = Repo.GetOrderIdByDateTime(order.OrderTime);
            foreach (var id in order.PizzaIDs)
            {
                Repo.AddOrderPizzas(order.OrderId, id);
            }
            Repo.Save();


            return(RedirectToAction("OrderDetails", "Order", new { orderId = order.OrderId }));
        }
 public void UpdateLocation(Library.Models.Location location)
 {
     throw new NotImplementedException();
 }