示例#1
0
        public ActionResult Inventory(int id)
        {
            P1B.Location currentLocation = LocRepo.GetLocationById(id);
            ViewData["currentLocation"] = currentLocation.Name;

            IEnumerable <P1B.LocationInventory> locInvs = LocationInventoryRepo.GetLocationInventoryByLocationId(id)
                                                          .OrderBy(li => li.IngredientId);
            List <P1B.Ingredient> ings = IngRepo.GetIngredients().ToList();

            var viewModels = locInvs.Select(li => new LocationInventoryViewModel
            {
                LocationId   = id,
                LocationName = currentLocation.Name,
                IngredientId = li.IngredientId,
                // https://stackoverflow.com/questions/272633/add-spaces-before-capital-letters
                IngredientType   = Regex.Replace(ings.Single(i => i.Id == li.IngredientId).Type, "([a-z])([A-Z])", "$1 $2"),
                IngredientUnits  = ings.Single(i => i.Id == li.IngredientId).Units,
                IngredientAmount = li.Amount
            }).ToList();

            return(View(viewModels));
        }
示例#2
0
        public ActionResult Create(Project1.ViewModels.LocationViewModel viewModel)
        {
            try
            {
                if (viewModel.LocationName.Length == 0)
                {
                    string message = "The location name cannot be empty.";
                    TempData["ErrorMessage"] = message;
                    _logger.LogWarning(message);
                    return(RedirectToAction("Error", "Home"));
                }
                if (LocRepo.CheckLocationNameExists(viewModel.LocationName))
                {
                    string message = "This location name has already been used in the database.";
                    TempData["ErrorMessage"] = message;
                    _logger.LogWarning(message);
                    return(RedirectToAction("Error", "Home"));
                }

                // TODO: Add insert logic here
                var newLocation = new P1B.Location
                {
                    Name = viewModel.LocationName
                };

                // TODO: Add insert logic here
                LocRepo.AddLocation(newLocation);
                int newLocationId = LocRepo.GetLastLocationAdded();
                LocationInventoryRepo.FillLocationInventory(newLocationId);
                return(RedirectToAction(nameof(Index)));
            }
            catch (Exception ex)
            {
                return(View());
            }
        }
        public ActionResult Create(Project1.ViewModels.OrderViewModel viewModel)
        {
            try
            {
                for (int i = 0; i < viewModel.OrderItems.Count; i++)
                {
                    viewModel.OrderItems[i].CupcakeId = i + 1;
                }
                List <Project1.BLL.OrderItem> newOrderItems = viewModel.OrderItems
                                                              .Where(oi => oi.Quantity != null).ToList();
                if (newOrderItems.Count == 0)
                {
                    string message = "The order must have at least one cupcake.";
                    TempData["ErrorMessage"] = message;
                    _logger.LogWarning(message);
                    return(RedirectToAction("Error", "Home"));
                }

                viewModel.Locations    = LocRepo.GetAllLocations().ToList();
                viewModel.Customers    = CustomerRepo.GetAllCustomers().ToList();
                viewModel.Cupcakes     = CupcakeRepo.GetAllCupcakes().ToList();
                viewModel.CustomerName = viewModel.Customers.Single(c => c.Id == viewModel.CustomerId).ReturnFullName();
                viewModel.LocationName = viewModel.Locations.Single(l => l.Id == viewModel.LocationId).Name;

                if (!CustomerRepo.CheckCustomerExists(viewModel.CustomerId))
                {
                    string message = $"Customer {viewModel.CustomerName} is not in the database.";
                    TempData["ErrorMessage"] = message;
                    _logger.LogWarning(message);
                    return(RedirectToAction("Error", "Home"));
                }
                if (!LocRepo.CheckLocationExists(viewModel.LocationId))
                {
                    string message = $"Location {viewModel.LocationName} is not in the list of stores.";
                    TempData["ErrorMessage"] = message;
                    _logger.LogWarning(message);
                    return(RedirectToAction("Error", "Home"));
                }
                // The following checks to see if the customer can order from this store location
                // If the customer has ordered at this store within the past 2 hours, than they shouldn't be
                // able to order again.
                var orders = OrderRepo.GetAllOrders().ToList();
                if (!Project1.BLL.Customer.CheckCustomerCanOrder(viewModel.CustomerId,
                                                                 viewModel.LocationId, orders))
                {
                    string message = "Customer can't place an order at this store because it hasn't been 2 hours \n" +
                                     "since there last order yet.";
                    TempData["ErrorMessage"] = message;
                    _logger.LogWarning(message);
                    return(RedirectToAction("Error", "Home"));
                }
                foreach (var cupcake in newOrderItems)
                {
                    if (!CupcakeRepo.CheckCupcakeExists(cupcake.CupcakeId))
                    {
                        string message = $"Cupcake {viewModel.Cupcakes.Single(c => c.Id == cupcake.CupcakeId).Type} " +
                                         $"is not in the database";
                        TempData["ErrorMessage"] = message;
                        _logger.LogWarning(message);
                        return(RedirectToAction("Error", "Home"));
                    }
                    int qnty = cupcake.Quantity ?? 0;
                    if (!Project1.BLL.Order.CheckCupcakeQuantity(qnty))
                    {
                        string message = $"Quantity for cupcake " +
                                         $"{viewModel.Cupcakes.Single(c => c.Id == cupcake.CupcakeId).Type} " +
                                         $"is out of range";
                        TempData["ErrorMessage"] = message;
                        _logger.LogWarning(message);
                        return(RedirectToAction("Error", "Home"));
                    }
                }
                // The following gets all orders and their associated order items in order
                // to validate that the store location's supply of the cupcakes in the customer's
                // requested order have not been exhausted.
                // Cupcake exhaustion happens when a store location has had more than 1000 cupcakes of one
                // type sold within 24 hours, at that point they cannot sell anymore.
                // This is arbitrary business logic that I added in order to satisfy the Project0
                // requirements.
                var orderItems = OrderItemRepo.GetAllOrderItems().ToList();
                if (!Project1.BLL.Location.CheckCanOrderCupcake(viewModel.LocationId,
                                                                orders, orderItems, newOrderItems))
                {
                    string message = $"This store has exhausted supply of one of those cupcakes. " +
                                     $"Try back in 24 hours.";
                    TempData["ErrorMessage"] = message;
                    _logger.LogWarning(message);
                    return(RedirectToAction("Error", "Home"));
                }
                // The following gets the recipes for the cupcakes in the customer's requested order
                // and checks to make sure that the store location's inventory can support the order.
                var recipes     = RecipeItemRepo.GetRecipes(newOrderItems);
                var locationInv = LocationInventoryRepo.GetLocationInventoryByLocationId(viewModel.LocationId).ToList();
                if (!Project1.BLL.Location.CheckOrderFeasible(recipes, locationInv, newOrderItems))
                {
                    string message = $"This store does not have enough ingredients to place " +
                                     $"the requested order.";
                    TempData["ErrorMessage"] = message;
                    _logger.LogWarning(message);
                    return(RedirectToAction("Error", "Home"));
                }

                var newOrder = new P1B.Order
                {
                    OrderLocation = viewModel.LocationId,
                    OrderCustomer = viewModel.CustomerId,
                    OrderTime     = DateTime.Now
                };
                OrderRepo.AddCupcakeOrder(newOrder);

                int newOrderId = OrderRepo.GetLastCupcakeOrderAdded();
                for (int i = 0; i < newOrderItems.Count; i++)
                {
                    newOrderItems[i].OrderId = newOrderId;
                }

                OrderItemRepo.AddCupcakeOrderItems(newOrderItems);
                LocationInventoryRepo.UpdateLocationInv(viewModel.LocationId, recipes, newOrderItems);

                return(RedirectToAction(nameof(Index)));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.ToString());
                return(RedirectToAction("Error", "Home"));
            }
        }