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 Restock(int ProductCode, int Quantity) { ACMEDbContext context = new ACMEDbContext(); context.Stock.First(s => s.Product.ProductCode == ProductCode).Quantity += Quantity; context.SaveChanges(); return("Ok"); }
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"); }
// 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 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"); }
public ActionResult Register([Bind("Email,Password,Name,Surname")] User user, int UserType) { user.Email = user.Email.Trim(); user.Name = user.Name.Trim(); user.Surname = user.Surname.Trim(); user.Password = BCrypt.HashPassword(user.Password, BCrypt.GenerateSalt()); user.UserType = _context.UserTypes.First(u => u.UserTypeID == UserType); ShoppingCart shoppingCart = new ShoppingCart(); _context.ShoppingCarts.Add(shoppingCart); _context.SaveChanges(); user.ShoppingCart = shoppingCart; _context.Users.Add(user); _context.SaveChanges(); HttpContext.Session.SetString("Email", user.Email); HttpContext.Session.SetString("Name", user.Name); HttpContext.Session.SetInt32("UserType", (int)user.UserType.UserTypeID); return(View()); }
public virtual void Commit() { _context.SaveChanges(); }