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 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"));
 }
        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"));
        }
Exemple #5
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"));
        }
Exemple #6
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"));
 }