public IActionResult NewOrder(CommerceWrapper Wrapper) { int?LoggedInUserID = HttpContext.Session.GetInt32("LoggedInUserID"); if (LoggedInUserID == null) { return(RedirectToAction("Index")); } Wrapper.CurrentOrder.UserId = (int)LoggedInUserID; Product PurchasedProduct = dbContext.Products.FirstOrDefault(product => product.ProductId == Wrapper.CurrentOrder.ProductId); if (PurchasedProduct == null) { ModelState.AddModelError("CurrentOrder.ProductId", "We don't have that item in stock."); return(Orders()); } if (Wrapper.CurrentOrder.Quantity > PurchasedProduct.Quantity) { ModelState.AddModelError("CurrentOrder.Quantity", $"Sorry, we only have {PurchasedProduct.Quantity} {PurchasedProduct.ItemName} available."); } if (ModelState.IsValid) { PurchasedProduct.Quantity -= Wrapper.CurrentOrder.Quantity; dbContext.Update(PurchasedProduct); dbContext.Entry(PurchasedProduct).Property("CreatedAt").IsModified = false; dbContext.Orders.Add(Wrapper.CurrentOrder); dbContext.SaveChanges(); return(RedirectToAction("Orders")); } return(Orders()); }
public IActionResult IndividualUser(CommerceWrapper Wrapper) { int?LoggedInUserID = HttpContext.Session.GetInt32("LoggedInUserID"); if (LoggedInUserID == null) { return(RedirectToAction("Index")); } Wrapper.CurrentUser = dbContext.Users.Include(order => order.Orders).ThenInclude(item => item.Product).FirstOrDefault(user => user.UserId == Wrapper.ThingsShown); return(View("IndividualUser", Wrapper)); }
public IActionResult Products(int NumOfProducts = 15) { int?LoggedInUserID = HttpContext.Session.GetInt32("LoggedInUserID"); if (LoggedInUserID == null) { return(RedirectToAction("Index")); } CommerceWrapper Wrapper = new CommerceWrapper(); Wrapper.AllProducts = dbContext.Products.Take(NumOfProducts).ToList(); return(View("Products", Wrapper)); }
public IActionResult Customers() { int?LoggedInUserID = HttpContext.Session.GetInt32("LoggedInUserID"); if (LoggedInUserID == null) { return(RedirectToAction("Index")); } CommerceWrapper Wrapper = new CommerceWrapper(); Wrapper.CurrentUser = dbContext.Users.FirstOrDefault(user => user.UserId == (int)LoggedInUserID); Wrapper.AllUsers = dbContext.Users.ToList(); return(View("Customers", Wrapper)); }
public IActionResult Orders() { int?LoggedInUserID = HttpContext.Session.GetInt32("LoggedInUserID"); if (LoggedInUserID == null) { return(RedirectToAction("Index")); } CommerceWrapper Wrapper = new CommerceWrapper(); Wrapper.AllProducts = dbContext.Products.ToList(); Wrapper.AllOrders = dbContext.Orders.Include(product => product.Product).Include(user => user.User).ToList(); return(View("Orders", Wrapper)); }
public IActionResult Dashboard() { int?LoggedInUserID = HttpContext.Session.GetInt32("LoggedInUserID"); if (LoggedInUserID == null) { return(RedirectToAction("Index")); } CommerceWrapper Wrapper = new CommerceWrapper(); Wrapper.CurrentUser = dbContext.Users.FirstOrDefault(user => user.UserId == (int)LoggedInUserID); Wrapper.AllUsers = dbContext.Users.OrderByDescending(user => user.CreatedAt).Take(3).ToList(); Wrapper.AllProducts = dbContext.Products.ToList(); Wrapper.AllOrders = dbContext.Orders.OrderByDescending(order => order.CreatedAt).Take(3).Include(User => User.User).Include(Order => Order.Product).ToList(); return(View("Dashboard", Wrapper)); }
public IActionResult NewProduct(CommerceWrapper Wrapper) { int?LoggedInUserID = HttpContext.Session.GetInt32("LoggedInUserID"); if (LoggedInUserID == null) { return(RedirectToAction("Index")); } if (ModelState.IsValid) { dbContext.Products.Add(Wrapper.CurrentProduct); dbContext.SaveChanges(); return(RedirectToAction("Products")); } return(Products()); }
public IActionResult DeleteUser(CommerceWrapper Wrapper) { int?LoggedInUserID = HttpContext.Session.GetInt32("LoggedInUserID"); if (LoggedInUserID == null) { return(RedirectToAction("Index")); } int UserToDeleteID = Wrapper.ThingsShown; User CurrentUser = dbContext.Users.FirstOrDefault(user => user.UserId == (int)LoggedInUserID); User UserToDelete = dbContext.Users.FirstOrDefault(user => user.UserId == UserToDeleteID); if (CurrentUser.IsAdmin == true || CurrentUser.UserId == UserToDelete.UserId) { dbContext.Users.Remove(UserToDelete); dbContext.SaveChanges(); } return(RedirectToAction("Customers")); }
public IActionResult SeeMoreProducts(CommerceWrapper Wrapper) { Wrapper.ThingsShown += 15; return(Products(Wrapper.ThingsShown)); }