public IActionResult EditOrderItem(int itemID, int itemQuantity)
 {
     if (itemID > 0 && itemQuantity > 0)
     {
         string Session = HttpContext.Session.GetString("PubSession"); //store session id
         if (Session != null)                                          //if a session has been created
         {
             PubSessions pubSession = _context.GetPubSession(Session); //get session info
             if (pubSession != null && pubSession.OrderBasketId != 0)  //session and basket exists
             {
                 int                 OrderBasketID = pubSession.OrderBasketId;
                 PubItems            PubItem       = _context.GetPubItem(itemID);
                 PubOrderBasketItems item          = _context.GetPubOrderBasketItem(pubSession.OrderBasketId, itemID);
                 if (item != null && PubItem != null)
                 {
                     //edit item
                     if (itemQuantity <= PubItem.ItemStock)
                     {
                         if (itemQuantity <= GlobalConstants.MaxItemsPerOrder)
                         {
                             //edit
                             if (_context.EditPubOrderBasketItem(OrderBasketID, itemID, itemQuantity))
                             {
                                 return(PartialView("ToastMessage", ToastAlert.Toast("Item Updated", string.Format("Changed {0} quantity to: {1}.", PubItem.ItemName, itemQuantity), PubItem.ItemImagePath)));
                             }
                             else
                             {
                                 return(PartialView("ToastMessage", ToastAlert.DefaultError()));
                                 //error item could not be updated
                             }
                         }
                         else
                         {
                             //max items error
                             return(PartialView("ToastMessage", ToastAlert.ItemQuantityMaxError(PubItem.ItemImagePath)));
                         }
                     }
                     else
                     {
                         return(PartialView("ToastMessage", ToastAlert.ItemQuantityStockError(PubItem.ItemImagePath)));
                         //not enoguh in stock error
                     }
                 }
             }
         }
     }
     return(null);
 }
        public IActionResult AddOrderItemPartial(int id, int?itemQuantity)  //add order using an ajax request and return a partial view with toast message
        {
            if (id > 0)
            {
                int quantity = itemQuantity ?? 1;
                if (quantity != 0)
                {
                    PubItems Item = _context.GetPubItem(id);
                    if (Item != null)
                    {
                        if (Item.ItemOnSale)
                        {
                            if (quantity <= Item.ItemStock)
                            {
                                if (quantity > 0 && quantity <= GlobalConstants.MaxItemsPerOrder)       //compare against max items per order only rather than item stock as well as the item stock may change when the user actually makes their order.
                                {
                                    string Session       = HttpContext.Session.GetString("PubSession"); //store session id
                                    int    OrderBasketID = 0;
                                    if (Session != null)                                                //if a session has been created
                                    {
                                        PubSessions pubSession = _context.GetPubSession(Session);       //get session info
                                        if (pubSession != null)                                         //session exists
                                        {
                                            if (pubSession.OrderBasketId != 0)
                                            {
                                                //add item
                                                OrderBasketID = pubSession.OrderBasketId;
                                                PubOrderBasketItems item = _context.GetPubOrderBasketItem(pubSession.OrderBasketId, id);
                                                if (item != null)
                                                {
                                                    //edit item
                                                    int newQuantity = quantity + item.ItemQuantity;
                                                    if (newQuantity <= Item.ItemStock)
                                                    {
                                                        if (newQuantity <= GlobalConstants.MaxItemsPerOrder)
                                                        {
                                                            //edit
                                                            if (_context.EditPubOrderBasketItem(OrderBasketID, id, newQuantity))
                                                            {
                                                                return(PartialView("ToastMessage", ToastAlert.Toast("Item Added", String.Format("{0}x {1} was added to your basket. Total Quantity: {2}", quantity, Item.ItemName, newQuantity), Item.ItemImagePath)));
                                                            }
                                                            else
                                                            {
                                                                return(PartialView("ToastMessage", ToastAlert.DefaultError()));
                                                                //error item could not be updated
                                                            }
                                                        }
                                                        else
                                                        {
                                                            //max items error
                                                            return(PartialView("ToastMessage", ToastAlert.ItemQuantityMaxError(Item.ItemImagePath)));
                                                        }
                                                    }
                                                    else
                                                    {
                                                        return(PartialView("ToastMessage", ToastAlert.ItemQuantityStockError(Item.ItemImagePath)));
                                                        //not enoguh in stock error
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                //create new orderbasket and add item
                                                OrderBasketID = _context.CreatePubSessionOrderBasket(Session, null);
                                                if (pubSession.UserId != 0)
                                                {
                                                    if (_context.GetPubUser(pubSession.UserId) != null)
                                                    {
                                                        _context.UpdatePubUserOrderBasket(pubSession.UserId, OrderBasketID);
                                                    }
                                                }
                                            }
                                        }
                                        else
                                        {
                                            //create a new session and orderbasket and add item
                                            Session = _context.AddPubSession(null, null);
                                            if (Session != null)
                                            {
                                                HttpContext.Session.SetString("PubSession", Session);
                                                OrderBasketID = _context.CreatePubSessionOrderBasket(Session, null);
                                            }
                                        }
                                    }
                                    else //create a new session and orderbasket and add item
                                    {
                                        Session = _context.AddPubSession(null, null);
                                        if (Session != null)
                                        {
                                            HttpContext.Session.SetString("PubSession", Session);
                                            OrderBasketID = _context.CreatePubSessionOrderBasket(Session, null);
                                        }
                                    }

                                    if (OrderBasketID != 0)
                                    {
                                        if (_context.AddPubOrderBasketItem(OrderBasketID, id, quantity))
                                        {
                                            return(PartialView("ToastMessage", ToastAlert.Toast("Item Added", String.Format("{0}x {1} was added to your basket", quantity, Item.ItemName), Item.ItemImagePath)));
                                        }
                                    }
                                }
                                else
                                {
                                    return(PartialView("ToastMessage", ToastAlert.ItemQuantityMaxError(Item.ItemImagePath)));
                                }
                            }
                            else
                            {
                                return(PartialView("ToastMessage", ToastAlert.ItemQuantityStockError(Item.ItemImagePath)));
                            }
                        }
                        else
                        {
                            //error item not on sale
                        }
                    }
                }
            }
            return(PartialView("ToastMessage", ToastAlert.DefaultError()));
        }
        public IActionResult AddOrderItem(AddOrderItemViewModel model) //add order using view model request and redirect to index page of items with a toast message
        {
            if (ModelState.IsValid)
            {
                PubItems Item = _context.GetPubItem(model.ItemId);
                if (Item != null)
                { //Item exists in database.
                    if (Item.ItemOnSale)
                    {
                        if (model.ItemQuantity <= Item.ItemStock)
                        {
                            if (model.ItemQuantity > 0 && model.ItemQuantity <= GlobalConstants.MaxItemsPerOrder) //compare against max items per order only rather than item stock as well as the item stock may change when the user actually makes their order.
                            {
                                string Session       = HttpContext.Session.GetString("PubSession");               //store session id
                                int    OrderBasketID = 0;
                                if (Session != null)                                                              //if a session has been created
                                {
                                    PubSessions pubSession = _context.GetPubSession(Session);                     //get session info
                                    if (pubSession != null)                                                       //session exists
                                    {
                                        if (pubSession.OrderBasketId != 0)
                                        {
                                            //add item
                                            OrderBasketID = pubSession.OrderBasketId;
                                            PubOrderBasketItems item = _context.GetPubOrderBasketItem(pubSession.OrderBasketId, model.ItemId);
                                            if (item != null)
                                            {
                                                //edit item
                                                int newQuantity = model.ItemQuantity + item.ItemQuantity;
                                                if (newQuantity <= Item.ItemStock)
                                                {
                                                    if (newQuantity <= GlobalConstants.MaxItemsPerOrder)
                                                    {
                                                        //edit
                                                        if (_context.EditPubOrderBasketItem(OrderBasketID, model.ItemId, newQuantity))
                                                        {
                                                            TempData["ToastMessage"] = JsonSerializer.Serialize(ToastAlert.Toast("Item Added", String.Format("{0}x {1} was added to your basket. Total Quantity: {2}", model.ItemQuantity, Item.ItemName, newQuantity), Item.ItemImagePath));
                                                            return(RedirectToAction("Index"));
                                                        }
                                                        else
                                                        {
                                                            TempData["ToastMessage"] = JsonSerializer.Serialize(ToastAlert.DefaultError());
                                                            return(RedirectToAction("Index"));
                                                            //error item could not be updated
                                                        }
                                                    }
                                                    else
                                                    {
                                                        //max items error
                                                        TempData["ToastMessage"] = JsonSerializer.Serialize(ToastAlert.ItemQuantityMaxError(Item.ItemImagePath));
                                                        return(RedirectToAction("Index"));
                                                    }
                                                }
                                                else
                                                {
                                                    TempData["ToastMessage"] = JsonSerializer.Serialize(ToastAlert.ItemQuantityStockError(Item.ItemImagePath));
                                                    return(RedirectToAction("Index"));
                                                    //not enoguh in stock error
                                                }
                                            }
                                        }
                                        else
                                        {
                                            //create new orderbasket and add item
                                            OrderBasketID = _context.CreatePubSessionOrderBasket(Session, null);
                                            if (pubSession.UserId != 0)
                                            {
                                                if (_context.GetPubUser(pubSession.UserId) != null)
                                                {
                                                    _context.UpdatePubUserOrderBasket(pubSession.UserId, OrderBasketID);
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {
                                        //create a new session and orderbasket and add item
                                        Session = _context.AddPubSession(null, null);
                                        if (Session != null)
                                        {
                                            HttpContext.Session.SetString("PubSession", Session);
                                            OrderBasketID = _context.CreatePubSessionOrderBasket(Session, null);
                                        }
                                    }
                                }
                                else //create a new session and orderbasket and add item
                                {
                                    Session = _context.AddPubSession(null, null);
                                    if (Session != null)
                                    {
                                        HttpContext.Session.SetString("PubSession", Session);
                                        OrderBasketID = _context.CreatePubSessionOrderBasket(Session, null);
                                    }
                                }

                                if (OrderBasketID != 0)
                                {
                                    if (_context.AddPubOrderBasketItem(OrderBasketID, model.ItemId, model.ItemQuantity))
                                    {
                                        TempData["ToastMessage"] = JsonSerializer.Serialize(ToastAlert.Toast("Item Added", String.Format("{0}x {1} was added to your basket", model.ItemQuantity, Item.ItemName), Item.ItemImagePath));
                                        return(RedirectToAction("Index"));
                                    }
                                }
                            }
                            else
                            {
                                TempData["ToastMessage"] = JsonSerializer.Serialize(ToastAlert.ItemQuantityMaxError(Item.ItemImagePath));
                                return(RedirectToAction("Index"));
                            }
                        }
                        else
                        {
                            TempData["ToastMessage"] = JsonSerializer.Serialize(ToastAlert.ItemQuantityStockError(Item.ItemImagePath));
                            return(RedirectToAction("Index"));
                        }
                    }
                    else
                    {
                        //error item not on sale
                    }
                }
            }
            TempData["ToastMessage"] = JsonSerializer.Serialize(ToastAlert.DefaultError());
            return(RedirectToAction("Index"));
        }