public IActionResult Items(int id, int quantity) { if (ModelState.IsValid) { //checks to make sure user selected order quantity is below the database inventory if (quantity > _repoStoreItem.GetStoreItemByStoreItemId(id).StoreItemInventory.itemInventory || quantity <= 0) { HttpContext.Session.SetString("itemError", "Selected amount is invalid, please try again"); //need to make it so that this can direct user to view with error message displayed return(RedirectToAction("Items", new { id = _repoStoreLocation.GetStoreLocationFromItem(id).StoreLocationId })); } bool x = false; if (HttpContext.Session.GetInt32("currentOrder") == null) { _logger.LogInformation(string.Format("User added item id: {0} and quantity: {1} to order", id, quantity)); //adds user order to the database var userName = User.FindFirstValue(ClaimTypes.Name); _repoUserOrder.AddUserOrder(userName, id); //stores the order id that was just added to the database to session HttpContext.Session.SetInt32("currentOrder", _repoUserOrder.GetAllOrders().Last().UserOrderId); x = true; } var orderId = HttpContext.Session.GetInt32("currentOrder"); if (x) { //creates an instance that contain information of the item id, order id, and quantity var listOfItemsOrdered = _serviceHome.ServItemPost(id, orderId, quantity); //listOfItemsOrdered is saved into session as cookie until user decides to purchase HttpContext.Session.SetComplexData("listOfItems", listOfItemsOrdered); } else { //creates a list that includes the instance of UserOrderItemStoredList(contains information of item ordered) //this list are made so that the new ordered item adds onto the session that holds the items ordered. List <UserOrderItemStoredList> listOfItemsOrdered = HttpContext.Session .GetComplexData <List <UserOrderItemStoredList> >("listOfItems"); //creates an instance that contain information of the item id, order id, and quantity var storedList = _serviceHome.ServItemPostElse(id, orderId, quantity); //add additionally ordered item into the ordered item list listOfItemsOrdered.Add(storedList); //stored into session as a cookie until user is ready to purchase HttpContext.Session.SetComplexData("listOfItems", listOfItemsOrdered); } //erases the error message HttpContext.Session.SetString("itemError", "Item added to the cart"); //direct user back to the store item view return(RedirectToAction("Items", new { id = _repoStoreLocation.GetStoreLocationFromUserOrder(orderId).StoreLocationId })); } return(RedirectToAction("Items", new { id = HttpContext.Session.GetInt32("selectedLocationId") })); }
//store all location and orders instantiating them into an object that will display orders by location public OrdersByLocationModel ServOrderHistoryByLocation(string location) { //storing all location names var locations = _repoStoreLocation.GetAllStoreLocations().Select(x => x.Location); //storing all orders var userOrders = _repoUserOrder.GetAllOrders(); if (!string.IsNullOrEmpty(location)) { //if user selects a location, get all the order of that location userOrders = _repoUserOrder.GetAllOrderByLocation(location); } //this model helps the view to make a order by location filter OrdersByLocationModel order = new OrdersByLocationModel { //displays all the location in a drop down list storeLocations = new SelectList(locations.ToList()), //display user order in a list userOrders = userOrders.ToList() }; return(order); }