Exemplo n.º 1
0
        public async Task <ActionResult> Order(OrderListModel listModel)
        {
            try
            {///Checks if Model state is valid
                if (ModelState.IsValid)
                {
                    for (var i = 0; i < listModel.order.Count; i++)
                    {
                        if (listModel.order[i].Quantity < listModel.order[i].OrderAmount)
                        {
                            ViewBag.Message = string.Format("You have chosen more than the available inventory on One or more Items");
                            await inventory.DecreaseInventory(listModel.order[i].ProductName, listModel.order[i].StoreName, listModel.order[i].OrderAmount);

                            _logger.LogInformation("You have tried to Order too much");
                            return(RedirectToAction("Inv", "Customer"));
                        }
                    }
                    ///Sets Current order view model info
                    #region Set Current Order View Model Info
                    var viewModel = new OrderViewModel
                    {
                        CustomerName        = HttpContext.Session.GetString("CustName"),
                        StoreName           = listModel.order.Select(x => x.StoreName).FirstOrDefault(),
                        CustomerPhoneNumber = HttpContext.Session.GetString("CustPhoneNumber"),
                        ManaPotionsBought   = listModel.order
                                              .Where(x => x.ProductName == "Mana Potion").Select(x => x.OrderAmount).FirstOrDefault(),
                        ClericsTalismanBought = listModel.order
                                                .Where(x => x.ProductName == "Clerics Talisman").Select(x => x.OrderAmount).FirstOrDefault(),
                        HealthPotionsBought = listModel.order
                                              .Where(x => x.ProductName == "Health Potion").Select(x => x.OrderAmount).FirstOrDefault(),
                        StaminaPotionsBought = listModel.order
                                               .Where(x => x.ProductName == "Stamina Potion").Select(x => x.OrderAmount).FirstOrDefault(),
                        MagicWandsBought = listModel.order
                                           .Where(x => x.ProductName == "Magic Wand").Select(x => x.OrderAmount).FirstOrDefault(),
                        Date = DateTime.Today,
                    };
                    #endregion
                    /// Creates a new order to save to db
                    #region Set New Order
                    var order = new Orders
                    {
                        ///Sets the values to be saved as an order
                        CustomerID            = viewModel.CustomerPhoneNumber,
                        StoreName             = viewModel.StoreName,
                        ManaPotionsBought     = viewModel.ManaPotionsBought,
                        StaminaPotionsBought  = viewModel.StaminaPotionsBought,
                        HealthPotionsBought   = viewModel.HealthPotionsBought,
                        ClericsTalismanBought = viewModel.ClericsTalismanBought,
                        MagicWandsBought      = viewModel.ClericsTalismanBought,
                        Date = DateTime.Today,
                    };
                    ///Add Order to db
                    await ordersRepository.AddOrdersAsync(order);

                    ///return created viewmodel
                    return(View(viewModel));

                    #endregion
                }
                else
                {/// Don't save and redirect back to inventory
                    ModelState.AddModelError("InvalidOrderInfo", "You most likely tried to order more than what is in Stock of an item, please try again");
                    return(RedirectToAction("Inv", "Customer"));
                }
            }///catch exception
            catch (InvalidOperationException e)
            {
                ModelState.AddModelError("InvalidOrderInfo", "You most likely tried to order more than what is in Stock of an item, please try again");
                return(View());
            }
        }