// GET: ShoppingCart/Details/5 public ActionResult Details() { ACMEDbContext context = new ACMEDbContext(); string email = HttpContext.Session.GetString("Email"); if (email == null) { return(RedirectToAction("Login", "Users")); } var user = context.Users.Where(u => u.Email == email).Include(u => u.ShoppingCart).First(); ShoppingCart cart = user.ShoppingCart; var cartItems = context.ProductShoppingCarts.Where(psc => psc.ShoppingCart.ShoppingCartID == cart.ShoppingCartID).Include(psc => psc.Product).ToList(); cartItems.ForEach(ci => { if (context.Stock.First(s => s.Product.ProductCode == ci.Product.ProductCode).Quantity <= 0) { ci.Quantity = 0; } }); var stockCounts = cartItems.Select(ci => ci.Product.ProductCode).Select(pc => context.Stock.First(s => s.Product.ProductCode == pc)).Select(s => s.Quantity).ToList(); ViewBag.StockCounts = stockCounts; ViewBag.TotalItems = cartItems.Sum(c => c.Quantity); ViewBag.GrandTotal = cartItems.Sum(c => (c.Quantity * c.Product.Price)); ViewBag.CartItems = cartItems; return(View()); }
public async Task <string> Create(string Name, string Description, string Price, IFormFile ProductImage, int CategoryID) { ACMEDbContext context = new ACMEDbContext(); Product product = new Product() { Name = Name, Description = Description, Price = Convert.ToDouble(Price.Replace('.', ',')), Category = context.Categories.First(c => c.CategoryID == CategoryID), Active = true }; //https://stackoverflow.com/questions/42741170/how-to-save-images-to-database-using-asp-net-core using (var ms = new MemoryStream()) { ProductImage.CopyTo(ms); product.Image = ms.ToArray(); } context.Add(product); await context.SaveChangesAsync(); Stock stock = new Stock { Product = product, Quantity = 5 }; context.Stock.Add(stock); await context.SaveChangesAsync(); return("OK"); }
public string Add(int ProductCode, int Quantity) { ACMEDbContext context = new ACMEDbContext(); string email = HttpContext.Session.GetString("Email"); var user = context.Users.Where(u => u.Email == email).Include(u => u.ShoppingCart).First(); ShoppingCart cart = user.ShoppingCart; //Check if user already has item in cart if (context.ProductShoppingCarts.Any(psc => psc.Product.ProductCode == ProductCode && psc.ShoppingCart.ShoppingCartID == cart.ShoppingCartID)) { //Add quantity to current product shopping cart entry ProductShoppingCart entry = context.ProductShoppingCarts.First(psc => psc.Product.ProductCode == ProductCode && psc.ShoppingCart.ShoppingCartID == cart.ShoppingCartID); entry.Quantity += Quantity; } else { // create new entry ProductShoppingCart productShoppingCart = new ProductShoppingCart { Product = context.Products.Find(ProductCode), ShoppingCart = cart, Quantity = Quantity }; context.ProductShoppingCarts.Add(productShoppingCart); } context.SaveChanges(); return("Ok"); }
public string MonthlyOrders(string Month, string Year) { ACMEDbContext context = new ACMEDbContext(); int month = DateTimeHelper.StringToInt(Month); int year = Convert.ToInt32(Year); // All orders for the specific month var orders = context.Orders.Where(o => o.OrderDate.Month == month && o.OrderDate.Year == year).ToList(); var productOrders = new List <ProductOrder>(); //orders.Select(o => context.ProductOrders.Where(po => po.Order == o)); orders.ForEach(o => { productOrders.AddRange(context.ProductOrders.Where(po => po.Order == o).Include(po => po.Product).ToList()); }); List <Category> categories = context.Categories.ToList(); List <int> orderTotals = new List <int>(); categories.ForEach(c => { var orderTotal = productOrders.Where(po => po.Product.Category == c).Sum(po => po.Quantity); orderTotals.Add(orderTotal); }); var xCategories = categories.Select(c => c.Name); var yOrders = orderTotals; return(JsonConvert.SerializeObject(new { xCategories, yOrders })); }
// GET: ProductsController/Create public ActionResult Create() { ACMEDbContext context = new ACMEDbContext(); ViewBag.Categories = context.Categories.OrderBy(c => c.Name).ToList(); return(View()); }
public async Task <string> Checkout(string StreetAddress, string?Complex, string Suburb, string City, string Province, string PostCode) { ACMEDbContext context = new ACMEDbContext(); string email = HttpContext.Session.GetString("Email"); var user = context.Users.Where(u => u.Email == email).Include(u => u.ShoppingCart).First(); ShoppingCart cart = user.ShoppingCart; var cartItems = context.ProductShoppingCarts.Where(psc => psc.ShoppingCart.ShoppingCartID == cart.ShoppingCartID).Include(psc => psc.Product).ToList(); string shippingAddress = StreetAddress + ", " + ((Complex != null) ? (Complex + ", ") : "") + Suburb + ", " + City + ", " + Province + ", " + PostCode; Order order = new Order(); order.OrderDate = DateTime.Now.Date; order.ETA = DateTime.Now.AddDays(2).Date; order.ShippingAddress = shippingAddress; order.User = user; context.Orders.Add(order); await context.SaveChangesAsync(); foreach (var item in cartItems) { ProductOrder po = new ProductOrder(); po.Order = order; po.Product = item.Product; po.Quantity = item.Quantity; context.Stock.First(s => s.Product == item.Product).Quantity -= item.Quantity; context.ProductOrders.Add(po); context.ProductShoppingCarts.Remove(item); } await context.SaveChangesAsync(); return("Ok"); }
public string Restock(int ProductCode, int Quantity) { ACMEDbContext context = new ACMEDbContext(); context.Stock.First(s => s.Product.ProductCode == ProductCode).Quantity += Quantity; context.SaveChanges(); return("Ok"); }
// GET: DummyController public string Product() { ACMEDbContext context = new ACMEDbContext(); Product product = DummyData.CreateProduct(); context.Products.Add(product); context.SaveChanges(); return("Dummy data added to DB"); }
public UserType GetUserType() { var httpContext = accessor.HttpContext; ACMEDbContext context = new ACMEDbContext(); int userTypeID = httpContext.Session.GetInt32("UserType") ?? -1; UserType userType = (userTypeID != -1) ? context.UserTypes.Find(userTypeID) : null; return(userType); }
public string Category() { ACMEDbContext context = new ACMEDbContext(); var categories = DummyData.CreateCategories(); categories.ForEach(category => { context.Categories.Add(category); }); context.SaveChanges(); return("Dummy data added to DB"); }
public async Task <IActionResult> DeleteConfirmed(int id) { ACMEDbContext context = new ACMEDbContext(); var product = await context.Products.FindAsync(id); product.Active = false; await context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }
public string Edit(int ProductCode, int Quantity) { ACMEDbContext context = new ACMEDbContext(); string email = HttpContext.Session.GetString("Email"); var user = context.Users.Where(u => u.Email == email).Include(u => u.ShoppingCart).First(); ShoppingCart cart = user.ShoppingCart; ProductShoppingCart entry = context.ProductShoppingCarts.First(psc => psc.ShoppingCart.ShoppingCartID == cart.ShoppingCartID && psc.Product.ProductCode == ProductCode); entry.Quantity = Quantity; context.SaveChanges(); return("Ok"); }
// GET: ProductsController/Edit/5 public async Task <ActionResult> Edit(int id) { ACMEDbContext context = new ACMEDbContext(); var product = await context.Products.FindAsync(id); var categories = context.Categories.OrderBy(c => c.Name).ToList(); if (product == null) { return(NotFound()); } ViewBag.Categories = categories; ViewBag.SelectedCategory = product.Category.CategoryID; return(View(product)); }
/*Separate validation method so that I can perform backend validation on create and edit forms as I am not using razor, I'm using XMLHttpRequest * so I need to validate manually*/ public string Validate([Bind("ProductID,Name,Description,Price")] Product product, int CategoryID, IFormFile ProductImage, bool Editing) { //https://stackoverflow.com/questions/42741170/how-to-save-images-to-database-using-asp-net-core if (ProductImage != null) { using (var ms = new MemoryStream()) { ProductImage.CopyTo(ms); product.Image = ms.ToArray(); } } //Manual checking because cannot bind IFormFile directly to Product object /*ProductImage can be null because on loading the edit page, the image display is set but the image input isn't, * therefore when editing, the product can still have a stored image even if one isn't posted with the form*/ if (product.Image == null && !Editing) { return("Please provide an image"); } string[] acceptedImageFileTypes = new string[] { "png", "jpg", "jpeg", "gif" }; if (ProductImage != null && !acceptedImageFileTypes.Contains(ProductImage.FileName.Split('.')[1])) { return("Please provide an image of type .png, .jpg, or .gif"); } if (CategoryID == -1) { return("Please provide a category"); } ACMEDbContext context = new ACMEDbContext(); product.Category = context.Categories.First(c => c.CategoryID == CategoryID); var validationResults = new List <ValidationResult>(); var validationContext = new ValidationContext(product, null, null); if (Validator.TryValidateObject(product, validationContext, validationResults, true)) { return("OK"); } return(ModelState.Values.SelectMany(v => v.Errors.Select(b => b.ErrorMessage)).ToList()[0]); }
// GET: ProductsController public ActionResult Index(int?category, string?q) { ACMEDbContext context = new ACMEDbContext(); int selectedCategory = category ?? -1; var productsWithCategories = context.Products.Include(p => p.Category).Where(p => p.Active == true); IQueryable <Product> productsKeywordFiltered; if (q != null) { productsKeywordFiltered = productsWithCategories.Where(p => p.Name.Contains(q)); } else { productsKeywordFiltered = productsWithCategories; } IQueryable <Product> productsCategoryFiltered; if (selectedCategory != -1) { productsCategoryFiltered = productsKeywordFiltered.Where(p => p.Category.CategoryID == selectedCategory); } else { productsCategoryFiltered = productsKeywordFiltered; } bool isLoggedIn = HttpContext.Session.GetString("Email") != null; if (isLoggedIn) { User user = context.Users.Where(u => u.Email.Equals(HttpContext.Session.GetString("Email"))).Include(u => u.UserType).First(); ViewBag.UserType = user.UserType.UserTypeID; } else { ViewBag.UserType = -1; } ViewBag.Categories = context.Categories.OrderBy(c => c.Name).ToList(); ViewBag.SelectedCategory = selectedCategory; return(View(productsCategoryFiltered.OrderBy(p => p.Name).ToList())); }
public ActionResult Index() { ACMEDbContext context = new ACMEDbContext(); string email = HttpContext.Session.GetString("Email"); if (email == null) { return(RedirectToAction("Login", "Users")); } var user = context.Users.Where(u => u.Email == email).Include(u => u.UserType).First(); if (user.UserType.UserTypeID == 1) { // Employee var orders = context.Orders; var dates = orders.Select(o => o.OrderDate); var monthStrings = dates.Select(d => DateTimeHelper.IntToString(d.Month) + " " + d.Year).Distinct(); ViewBag.HasOrders = context.Orders.Any(); ViewBag.Months = monthStrings; return(View("IndexEmployee")); } else { //Customer var orders = context.Orders.Where(o => o.User == user).OrderByDescending(o => o.OrderDate).ToList(); //var productOrders1 = orders.Select(o => context.ProductOrders.Where(po => po.Order == o)).ToList(); List <ProductOrder> productOrders = new List <ProductOrder>(); orders.ForEach(o => { foreach (ProductOrder po in context.ProductOrders.Where(p => p.Order == o).Include(p => p.Product)) { productOrders.Add(po); } }); ViewBag.Name = user.Name + " " + user.Surname; ViewBag.Orders = orders; ViewBag.ProductOrders = productOrders; return(View("IndexCustomer")); } }
// GET: ProductsController/Details/5 public ActionResult Details(int id) { ACMEDbContext context = new ACMEDbContext(); Product product = context.Products.Where(p => p.ProductCode == id).Include(p => p.Category).First(); int stockCount = context.Stock.First(s => s.Product.ProductCode == product.ProductCode).Quantity; bool isLoggedIn = HttpContext.Session.GetString("Email") != null; if (isLoggedIn) { User user = context.Users.Where(u => u.Email.Equals(HttpContext.Session.GetString("Email"))).Include(u => u.UserType).First(); ViewBag.UserType = user.UserType.UserTypeID; } else { ViewBag.UserType = -1; } ViewBag.IsLoggedIn = isLoggedIn; ViewBag.Stock = stockCount; return(View(product)); }
public async Task <string> Edit(int ProductID, string Name, string Description, string Price, IFormFile ProductImage, int CategoryID) { ACMEDbContext context = new ACMEDbContext(); Product product = context.Products.First(p => p.ProductCode == ProductID); product.Name = Name; product.Description = Description; product.Price = Convert.ToDouble(Price.Replace('.', ',')); product.Category = context.Categories.ToList().First(c => c.CategoryID == CategoryID); product.Active = true; //If image is null, means value of file select wasnt changed if (ProductImage != null) { using (var ms = new MemoryStream()) { ProductImage.CopyTo(ms); product.Image = ms.ToArray(); } } try { context.Update(product); await context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!context.Products.Any(e => e.ProductCode == ProductID)) { return("404. Product not found"); } else { throw; } } return("OK"); }
public IActionResult Checkout() { ACMEDbContext context = new ACMEDbContext(); string email = HttpContext.Session.GetString("Email"); var user = context.Users.Where(u => u.Email == email).Include(u => u.ShoppingCart).First(); ShoppingCart cart = user.ShoppingCart; var cartItems = context.ProductShoppingCarts.Where(psc => psc.ShoppingCart.ShoppingCartID == cart.ShoppingCartID).Include(psc => psc.Product).ToList(); List <string> provinces = new List <string> { "Eastern Cape", "Free State", "Gauteng", "KwaZulu-Natal", "Limpopo", "Mpumalanga", "Northern Cape", "North West", "Western Cape" }; ViewBag.TotalItems = cartItems.Sum(c => c.Quantity); ViewBag.GrandTotal = cartItems.Sum(c => (c.Quantity * c.Product.Price)); ViewBag.Provinces = provinces; return(View()); }
public OfferApplicationRepository(ACMEDbContext context) : base(context) { }
public CountryRepository(ACMEDbContext context) : base(context) { }
public PostcodeRepository(ACMEDbContext context) : base(context) { }
public EntityBaseRepository(ACMEDbContext context) { _context = context; }