public IActionResult RemoveBasketItem(int id) //add order using an ajax request and return a partial view with toast message
 {
     if (id > 0)
     {
         PubItems Item = _context.GetPubItem(id);
         if (Item != null)
         {
             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)                                   //session exists
                 {
                     if (pubSession.OrderBasketId != 0)
                     {
                         //add item
                         PubOrderBasketItems basketItem = _context.GetPubOrderBasketItem(pubSession.OrderBasketId, id);
                         if (basketItem != null)
                         {
                             if (_context.RemoveOrderBasketItem(pubSession.OrderBasketId, basketItem.ItemId))
                             {
                                 TempData["ToastMessage"] = JsonSerializer.Serialize(ToastAlert.Toast("Item Removed", String.Format("{0}x {1} has been removed from your basket", basketItem.ItemQuantity, Item.ItemName), Item.ItemImagePath));
                                 return(RedirectToAction("OrderBasket"));
                             }
                         }
                     }
                 }
             }
         }
     }
     TempData["ToastMessage"] = JsonSerializer.Serialize(ToastAlert.DefaultError());
     return(RedirectToAction("OrderBasket"));
 }
示例#2
0
        public IActionResult Login()
        {
            var Session = HttpContext.Session.GetString("PubSession"); //user must be logged in to view orders

            if (Session != null)
            {
                PubSessions pubSession = _context.GetPubSession(Session); //get session info
                if (pubSession != null)                                   //session exists
                {
                    if (pubSession.UserId == 0)                           //if a user is logged in...
                    {
                        return(View());
                    }
                }
                else
                {
                    return(View());
                }
            }
            else
            {
                return(View());
            }
            return(RedirectToAction("Index", "Home"));
        }
        public IActionResult OrderBasket() //
        {
            TempData.TryGetValue("ToastMessage", out object data);
            if (data != null)
            {
                ViewBag.Toast = JsonSerializer.Deserialize <ToastAlertViewModel>(data as string);
            }

            var Session = HttpContext.Session.GetString("PubSession");

            if (Session != null)
            {
                PubSessions pubSession = _context.GetPubSession(Session); //get session info
                if (pubSession != null)                                   //session exists
                {
                    if (pubSession.OrderBasketId != 0)                    //if basket exists
                    {
                        List <OrderBasketViewModel> BasketItemList = _context.GetPubOrderBasketItems(pubSession.OrderBasketId);
                        if (BasketItemList != null) //if fetched list of items in basket
                        {
                            return(View(BasketItemList));
                        }
                    }
                }
            }
            return(View());
        }
 public IActionResult Details(int?id)
 {
     if (id != null && id > 0)
     {
         PubItems Item = _context.GetPubItem((int)id);
         if (Item != null)
         {
             UserAccessRank userAccessRank = UserAccessRank.Customer;
             var            Session        = HttpContext.Session.GetString("PubSession"); //user must be logged in to view orders
             if (Session != null)
             {
                 PubSessions pubSession = _context.GetPubSession(Session); //get session info
                 if (pubSession != null)                                   //session exists
                 {
                     if (pubSession.UserId > 0)                            //if a user is logged in...
                     {
                         PubUsers pubUser = _context.GetPubUser(pubSession.UserId);
                         if (pubUser != null) //if user exists
                         {
                             userAccessRank = pubUser.UserAccessRank;
                         }
                     }
                 }
             }
             if (Item.ItemOnSale || userAccessRank == UserAccessRank.Admin)
             {
                 ViewBag.UserAccessRank = userAccessRank; //get access rank as admins will be able be able to press a button to go to edit item page.
                 ViewBag.Item           = Item;
                 return(View(new AddOrderItemViewModel()));
             }
         }
     }
     return(RedirectToAction("Index"));
 }
        public IActionResult Edit(EditItemViewModel model)
        {
            var Session = HttpContext.Session.GetString("PubSession"); //user must be logged in to view orders

            if (Session != null)
            {
                PubSessions pubSession = _context.GetPubSession(Session); //get session info
                if (pubSession != null)                                   //session exists
                {
                    if (pubSession.UserId > 0)                            //if a user is logged in...
                    {
                        PubUsers pubUser = _context.GetPubUser(pubSession.UserId);
                        if (pubUser != null && pubUser.UserAccessRank == UserAccessRank.Admin) //if user exists
                        {
                            if (ModelState.IsValid)                                            //user is an admin, allow access to editing.
                            {
                                PubItems EditedItem = new PubItems()
                                {
                                    ItemId          = model.ItemID,
                                    ItemDescription = model.ItemDescription,
                                    ItemName        = model.ItemName,
                                    ItemOnSale      = model.ItemOnSale,
                                    ItemPrice       = model.ItemPrice,
                                    ItemStock       = model.ItemStock,
                                    ItemType        = model.ItemType,
                                    ItemImagePath   = model.ItemExistingImagePath
                                };

                                if (model.ItemImage != null)
                                {
                                    if (model.ItemExistingImagePath != null)
                                    {
                                        string existingFilePath = Path.Combine(webHostEnvironment.WebRootPath, "images", model.ItemExistingImagePath);
                                        System.IO.File.Delete(existingFilePath);
                                    }
                                    EditedItem.ItemImagePath = ProcessUploadedFile(model);
                                }

                                //int result = _context.AddPubItem();

                                if (_context.EditPubItem(EditedItem) == true)
                                {
                                    return(RedirectToAction("Details", new { id = EditedItem.ItemId }));
                                }
                                else
                                {
                                    ModelState.AddModelError("", "An error occured, could not create a new item.");
                                    //ERROR: item was not added
                                }
                            }
                            return(View(model));
                        }
                    }
                }
            }
            return(RedirectToAction("Index", "Home"));
        }
示例#6
0
        public IActionResult Logout()
        {
            var SessionString = HttpContext.Session.GetString("PubSession");

            if (SessionString != null)
            {
                PubSessions pubSessions = _context.GetPubSession(SessionString);
                if (pubSessions != null)
                {
                    _context.RemovePubSession(pubSessions.SessionId);
                    HttpContext.Session.Remove("PubSession");
                }
            }
            return(RedirectToAction("Index", "Home"));
        }
 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);
 }
示例#8
0
        private bool LogInUser(PubUsers User)
        {
            //string UniqueFileName = Guid.NewGuid().ToString();
            string SessionString = HttpContext.Session.GetString("PubSession");

            if (SessionString != null) //session already exists, put user in existing session
            {
                PubSessions Session = _context.GetPubSession(SessionString);
                if (Session != null)
                {
                    int OrderBasketID;
                    if (User.UserOrderBasketID > 0)
                    {
                        OrderBasketID = User.UserOrderBasketID;
                    }
                    else
                    {
                        OrderBasketID = Session.OrderBasketId;
                        if (_context.UpdatePubUserOrderBasket(User.UserId, OrderBasketID) == false)
                        {
                            return(false);
                        }
                    }
                    if (_context.UpdatePubSession(SessionString, User.UserId, OrderBasketID))
                    {
                        return(true);
                    }
                    else
                    {
                        //error
                        return(false);
                    }
                }
            }
            //create session with user
            SessionString = _context.AddPubSession(User.UserId, User.UserOrderBasketID);
            if (SessionString != null)
            {
                HttpContext.Session.SetString("PubSession", SessionString);
                return(true);
            }
            else
            {
                ModelState.AddModelError("ERROR", "An Error has occoured, you could not be logged in.");
                return(false);
            }
        }
示例#9
0
        public IActionResult Order()
        {
            var Session = HttpContext.Session.GetString("PubSession");

            if (Session != null)
            {
                PubSessions pubSession = _context.GetPubSession(Session); //get session info
                if (pubSession != null)                                   //session exists
                {
                    if (pubSession.UserId > 0)                            //if user is logged in
                    {
                        if (pubSession.OrderBasketId > 0)                 //if basket exists
                        {
                            List <OrderBasketViewModel> BasketItemList = _context.GetPubOrderBasketItems(pubSession.OrderBasketId);
                            if (BasketItemList != null) //if fetched list of items in basket exists
                            {
                                foreach (OrderBasketViewModel basketitem in BasketItemList)
                                {
                                    if (basketitem.ItemQuantity > basketitem.PubItem.ItemStock)
                                    {
                                        TempData["ToastMessage"] = JsonSerializer.Serialize(ToastAlert.Toast("Error", "Item request quantity cannot exceede item stock.", basketitem.PubItem.ItemImagePath));
                                        return(RedirectToAction("OrderBasket", "Items"));
                                    }
                                }
                                int OrderID = _context.CreateOrder(pubSession.OrderBasketId, pubSession.UserId, pubSession.SessionId);
                                if (OrderID > 0)
                                {
                                    TempData["ToastMessage"] = JsonSerializer.Serialize(ToastAlert.Toast("New Order", "Your order has been created! ID: " + OrderID));
                                    return(RedirectToAction("Order", new { id = OrderID }));
                                }
                                else
                                {
                                    TempData["ToastMessage"] = JsonSerializer.Serialize(ToastAlert.DefaultError());
                                }
                            }
                        }
                    }
                    else
                    {
                        return(RedirectToAction("Login", "Account"));
                    }
                }
            }
            return(RedirectToAction("OrderBasket", "Items"));
        }
示例#10
0
        public IViewComponentResult Invoke()
        {
            string SessionString = HttpContext.Session.GetString("PubSession");

            if (SessionString != null) //session already exists, put user in existing session
            {
                PubSessions Session = _context.GetPubSession(SessionString);
                if (Session != null && Session.UserId > 0)
                {
                    PubUserDetailsViewModel UserDetails = _context.GetPubUserDetails(Session.UserId);
                    if (UserDetails != null)
                    {
                        ViewBag.UserDetails = UserDetails;
                        return(View());
                    }
                }
            }
            return(View());
        }
示例#11
0
        public IActionResult Index(int?UserID, int?PageNumber)         //get user id
        {
            var Session = HttpContext.Session.GetString("PubSession"); //user must be logged in to view orders

            if (Session != null)
            {
                PubSessions pubSession = _context.GetPubSession(Session); //get session info
                if (pubSession != null)                                   //session exists
                {
                    if (pubSession.UserId > 0)                            //if a user is logged in...
                    {
                        PubUsers pubUser = _context.GetPubUser(pubSession.UserId);
                        if (pubUser != null) //if user exists
                        {
                            int?ID;
                            if (pubUser.UserAccessRank == Models.Enum.UserAccessRank.Admin)
                            {
                                if (UserID != null && UserID > 0)
                                {
                                    ID = UserID;
                                }
                                else
                                {
                                    ID = null;
                                }
                            }
                            else
                            {
                                ID = pubUser.UserId;
                            }
                            int pageNumber = PageNumber ?? 0;
                            ViewBag.UserAccessRank     = pubUser.UserAccessRank;
                            ViewBag.PubOrdersViewModel = _context.GetPubOrders(ID, pageNumber);
                            return(View(new FilterOrdersViewModel {
                                UserID = ID, PageNumber = pageNumber
                            }));
                        }
                    }
                }
            }
            return(RedirectToAction("Index", "Home"));
        }
示例#12
0
        public IActionResult Order(int?id)
        {
            object data;

            TempData.TryGetValue("ToastMessage", out data);
            if (data != null)
            {
                ViewBag.Toast = JsonSerializer.Deserialize <ToastAlertViewModel>(data as string);
            }
            if (id != null && id > 0)                                      //check that user has given an id and that the ID given is bigger than 0 (all ids are bigger than 0)
            {
                var Session = HttpContext.Session.GetString("PubSession"); //user must be logged in to view an order
                if (Session != null)
                {
                    PubSessions pubSession = _context.GetPubSession(Session); //get session info
                    if (pubSession != null)                                   //session exists
                    {
                        if (pubSession.UserId > 0)                            //if a user is logged in...
                        {
                            PubUsers pubUsers = _context.GetPubUser(pubSession.UserId);
                            if (pubUsers != null)
                            {
                                Get_PubOrderViewModel OrderDetails = _context.GetPubOrder((int)id);
                                if (OrderDetails != null && (OrderDetails.UserId == pubUsers.UserId || pubUsers.UserAccessRank == Models.Enum.UserAccessRank.Admin)) //only logged in user and admins are allowed to view.
                                {
                                    OrderViewModel orderViewModel = new OrderViewModel
                                    {
                                        OrderDetails = OrderDetails,
                                        OrderItems   = _context.GetPubOrderItems((int)id)
                                    };
                                    ViewBag.LoggedInUserID = pubUsers.UserId;
                                    ViewBag.UserAccessRank = pubUsers.UserAccessRank;
                                    return(View(orderViewModel));
                                }
                            }
                        }
                    }
                }
            }
            return(RedirectToAction("Index", "Home"));
        }
        public IActionResult Create()
        {
            var Session = HttpContext.Session.GetString("PubSession"); //user must be logged in to view orders

            if (Session != null)
            {
                PubSessions pubSession = _context.GetPubSession(Session); //get session info
                if (pubSession != null)                                   //session exists
                {
                    if (pubSession.UserId > 0)                            //if a user is logged in...
                    {
                        PubUsers pubUser = _context.GetPubUser(pubSession.UserId);
                        if (pubUser != null && pubUser.UserAccessRank == UserAccessRank.Admin) //if user exists and is an admin
                        {
                            return(View(new CreateItemViewModel()));
                        }
                    }
                }
            }
            return(RedirectToAction("Index", "Home"));
        }
        public IActionResult Edit(int?id)
        {
            if (id != null)
            {
                var Session = HttpContext.Session.GetString("PubSession"); //user must be logged in to view orders
                if (Session != null)
                {
                    PubSessions pubSession = _context.GetPubSession(Session); //get session info
                    if (pubSession != null)                                   //session exists
                    {
                        if (pubSession.UserId > 0)                            //if a user is logged in...
                        {
                            PubUsers pubUser = _context.GetPubUser(pubSession.UserId);
                            if (pubUser != null && pubUser.UserAccessRank == UserAccessRank.Admin) //if user exists and is an admin then allow access to edit page.
                            {
                                int      itemID = (int)id;
                                PubItems item   = _context.GetPubItem(itemID);

                                if (item != null)
                                {
                                    EditItemViewModel model = new EditItemViewModel
                                    {
                                        ItemID = itemID,
                                        ItemExistingImagePath = item.ItemImagePath,
                                        ItemDescription       = item.ItemDescription,
                                        ItemName   = item.ItemName,
                                        ItemOnSale = item.ItemOnSale,
                                        ItemPrice  = item.ItemPrice,
                                        ItemStock  = item.ItemStock,
                                        ItemType   = item.ItemType,
                                    };
                                    return(View(model));
                                }
                            }
                        }
                    }
                }
            }
            return(RedirectToAction("Index", "Home"));
        }
示例#15
0
 public IActionResult CancelOrder(int id)
 {
     if (id > 0)                                                    //all ids are bigger than 0
     {
         var Session = HttpContext.Session.GetString("PubSession"); //user must be logged in to view an order
         if (Session != null)
         {
             PubSessions pubSession = _context.GetPubSession(Session);           //get session info
             if (pubSession != null)                                             //session exists
             {
                 if (pubSession.UserId > 0)                                      //if a user is logged in...
                 {
                     PubUsers pubUsers = _context.GetPubUser(pubSession.UserId); //fetch logged in user....
                     if (pubUsers != null)                                       //if user exists...
                     {
                         Get_PubOrderViewModel Order = _context.GetPubOrder(id);
                         if (Order != null)
                         {
                             if (Order.UserId == pubUsers.UserId)
                             {
                                 if (_context.RemovePubOrder(id))
                                 {
                                     ViewBag.RemovedOrderID = id;
                                     return(View());
                                 }
                             }
                             TempData["ToastMessage"] = JsonSerializer.Serialize(ToastAlert.Toast("Error", "Error: Could not cancel order."));
                             return(RedirectToAction("Order", new { id }));
                         }
                     }
                 }
             }
         }
     }
     TempData["ToastMessage"] = JsonSerializer.Serialize(ToastAlert.DefaultError());
     return(RedirectToAction("Index", "Home"));
 }
        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 Index(string ItemName, ItemTypes?ItemType, bool?ItemOnSale, int?PageNumber)    //as this is the default view, only show items on sale
        {
            UserAccessRank userAccessRank = UserAccessRank.Customer;
            var            Session        = HttpContext.Session.GetString("PubSession"); //user must be logged in to view orders

            if (Session != null)
            {
                PubSessions pubSession = _context.GetPubSession(Session); //get session info
                if (pubSession != null)                                   //session exists
                {
                    if (pubSession.UserId > 0)                            //if a user is logged in...
                    {
                        PubUsers pubUser = _context.GetPubUser(pubSession.UserId);
                        if (pubUser != null) //if user exists
                        {
                            userAccessRank = pubUser.UserAccessRank;
                        }
                    }
                }
            }
            ViewBag.UserAccessRank = userAccessRank;
            PubItemsViewModel model;

            if (userAccessRank == UserAccessRank.Admin) //if admin then admin can specify if items are on sale or not.
            {
                if (ItemType == null)
                {
                    model = _context.GetPubItems(ItemName, ItemOnSale ?? true, PageNumber ?? 0); //true - get only items on sale
                }
                else
                {
                    model = _context.GetPubItems(ItemName, ItemOnSale ?? true, PageNumber ?? 0, (ItemTypes)ItemType); //true - get only items on sale
                }

                if (model != null)
                {
                    ViewBag.PubItemsViewModel = model;
                }
            }
            else //if not admin items will always be on sale
            {
                if (ItemType == null)
                {
                    model = _context.GetPubItems(ItemName, true, PageNumber ?? 0); //true - get only items on sale
                }
                else
                {
                    model = _context.GetPubItems(ItemName, true, PageNumber ?? 0, (ItemTypes)ItemType); //true - get only items on sale
                }

                if (model != null)
                {
                    ViewBag.PubItemsViewModel = model;
                }
            }

            TempData.TryGetValue("ToastMessage", out object data);
            if (data != null)
            {
                ViewBag.Toast = JsonSerializer.Deserialize <ToastAlertViewModel>(data as string);
            }
            return(View(new FilterItemsViewModel()
            {
                ItemName = ItemName, ItemOnSale = ItemOnSale ?? true, ItemType = ItemType ?? null, PageNumber = PageNumber ?? 0
            }));
        }
        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"));
        }