public async Task <ActionResult> ProductAdd(ShopProductVM model) { if (ModelState.IsValid) { using (Db db = new Db()) { if (db.ShopProducts.Any(m => m.Name == model.Name)) { ModelState.AddModelError("", "This name product alredy exist"); return(View(model)); } ShopProductDB productAdd = new ShopProductDB { Name = model.Name, Description = model.Description, Price = model.Price, ShopCategoryId = model.ShopCategoryId, ShopCategoryName = db.ShopCategories.Find(model.ShopCategoryId).Name }; db.ShopProducts.Add(productAdd); await db.SaveChangesAsync(); } } return(RedirectToAction("Index")); }
public ShopProductVM(ShopProductDB model) { Id = model.Id; Name = model.Name; Price = model.Price; Description = model.Description; ShopCategoryId = model.ShopCategoryId; ShopCategoryName = model.ShopCategoryName; ImageName = model.ImageName; }
public async Task <ActionResult> ProductDelete(int id) { ViewBag.Menu = "Shop"; using (Db db = new Db()) { ShopProductDB productDb = await db.ShopProducts.FindAsync(id); if (productDb != null) { return(View(new ShopProductVM(productDb))); } } return(RedirectToAction("Index")); }
public async Task <ActionResult> ProductDelete(ShopProductVM model) { using (Db db = new Db()) { ShopProductDB productDb = await db.ShopProducts.FindAsync(model.Id); if (productDb != null) { db.ShopProducts.Remove(productDb); db.SaveChanges(); if (!db.ShopProducts.Any(m => m.Id == model.Id)) { } } } return(RedirectToAction("Index")); }
public async Task <ActionResult> ProductEdit(ShopProductVM model) { using (Db db = new Db()) { if (ModelState.IsValid) { ShopProductDB productDb = await db.ShopProducts.FindAsync(model.Id); if (productDb != null) { if (db.ShopProducts.Where(m => m.Id != model.Id).Any(m => m.Name == model.Name)) { ModelState.AddModelError("", "This name product already exist"); return(View(model)); } productDb.Name = model.Name; productDb.Price = model.Price; productDb.ImageName = model.ImageName; productDb.Description = model.Description; ShopCategoryDB category = await db.ShopCategories.FindAsync(model.ShopCategoryId); if (category != null) { productDb.ShopCategoryId = category.Id; productDb.ShopCategoryName = category.Name; } else { return(View(model.Id)); } db.Entry(productDb).State = System.Data.Entity.EntityState.Modified; await db.SaveChangesAsync(); return(RedirectToAction("Index")); } } return(View(model.Id)); } }
//IAuthenticationManager AuthenticationManager { get { return HttpContext.GetOwinContext().Authentication; } } // GET: Cart public async Task <ActionResult> Index(int?id) { ViewBag.Menu = "Shop"; if (id != null) { using (Db db = new Db()) { ShopProductDB productDb = await db.ShopProducts.FindAsync(id); if (productDb != null) { // sdd to Session Cart bool checkFirst = true; var listCart = (List <ShopCartVM>)Session["Cart"]; if (Session["Cart"] != null) { for (int i = 0; i < listCart.Count; i++) { if (listCart[i].ProductId == productDb.Id) { listCart[i].AmountProduct++; Session["Cart"] = listCart; checkFirst = false; break; } } } // add to ShopCartDB string userId = (string)Session["UserId"]; if (checkFirst) { ShopCartDB addProduct = new ShopCartDB { UserId = (string)Session["UserId"], ProductId = productDb.Id, ProductName = productDb.Name, Description = productDb.Description, Price = productDb.Price, AmountProduct = 1, Image = productDb.ImageName }; db.ShopCarts.Add(addProduct); db.SaveChanges(); addProduct = db.ShopCarts.Where(m => m.ProductId == addProduct.ProductId && m.UserId == userId).FirstOrDefault(); if (addProduct != null) { listCart.Add(new ShopCartVM(addProduct)); Session["Cart"] = listCart; } } else { ShopCartDB addProduct = db.ShopCarts.Where(m => m.ProductName == productDb.Name && m.UserId == userId).FirstOrDefault(); addProduct.AmountProduct++; db.Entry(addProduct).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } } else { //ViewBag. = "Product don't add to cart"; return(RedirectToAction("Index", "Shop")); } } } if (((List <ShopCartVM>)Session["Cart"]).Count == 0) { ViewBag.Message = "Cart is empty"; } var cart = Session["Cart"] as List <ShopCartVM> ?? new List <ShopCartVM>(); decimal total = 0m; foreach (var item in cart) { total += item.Total; } ViewBag.TotalPriceCart = total; return(View(cart)); }