// GET: Admin/Products public ActionResult Products(string id) { AdminProductModel model = new AdminProductModel(); var products = _repository.GetProducts(id); model.Products = products.ToList(); return(View(model)); }
public IActionResult ProductList(int page = 1) { List <AdminProductModel> productModels = new List <AdminProductModel>(); var products = unitOfWork.Products.GetAll(); var count = products.Count(); products = products.Skip((page - 1) * ProductPageSize).Take(ProductPageSize); products = products.Include(p => p.ProductCategories).ThenInclude(pC => pC.Category); foreach (var product in products) { var categoriesName = product.ProductCategories.Select(i => i.Category.CategoryName).ToList(); AdminProductModel productModel = new AdminProductModel() { ProductId = product.ProductId, ProductName = product.ProductName, Price = product.Price, ProfilImage = product.ProfilImage, AvailableAmount = product.AvailableAmount, IsHome = product.IsHome, Discount = product.Discount, Categories = String.Join(",", categoriesName) }; productModels.Add(productModel); } ViewBag.Action = "Orders"; var model = new AdminPagingProductModel() { Products = productModels, PagingDetails = new PagingDetails() { CurrentPage = page, ItemsPerPage = OrderPageSize, TotalItems = count } }; return(View(model)); }
public ActionResult AddProduct(AdminProductModel product) { if (Session["important"] != null || Session["juststaff"] != null) { DAO dao = new DAO(); DescriptionModel desc = new DescriptionModel(); List <DescriptionModel> descList = new List <DescriptionModel>(); descList.Add(desc); product.Details = descList; /** * 1. Upload fileto images dir and generate file name * 2. Create new category with that imgUrl file name */ string defaultImgUrlPath = Server.MapPath("~/Content/Images/no_image.png"); string newImgUrlPath = ""; string fileName = ""; int count = 0; string ext; string extractedName; var validImageTypes = new string[] { "image/gif", "image/jpeg", "image/pjpeg", "image/png" }; // Upload the image if (product.Image != null && product.Image.ContentLength > 0) { if (!validImageTypes.Contains(product.Image.ContentType)) { ViewBag.Message = "ERROR: Unknown image type"; } else { // Create new category if (ModelState.IsValid) { try { fileName = Path.GetFileName(product.Image.FileName); // Change file name // Extract name/extension extractedName = fileName.Split('.')[0]; ext = fileName.Split('.')[1]; // Create new name+ext fileName = product.Name.ToLower() + "." + ext; // Save to DB count = dao.InsertProduct(product, fileName); } catch (Exception ex) { ViewBag.Status = "DB ERROR!" + ex.Message; } if (count == 1) { // Upload the file newImgUrlPath = Path.Combine(Server.MapPath("~/Content/Images/products/"), fileName); product.Image.SaveAs(newImgUrlPath); ModelState.Clear(); return(RedirectToAction("Index", "AdminProduct")); } else { ViewBag.Status = "ERROR! Product create fail"; } return(View()); // Display modal to say category created } else { ViewBag.Status = "ERROR! Model Invalid"; } } } else { // No image file uploaded ViewBag.Status = "ERROR: No image selected"; } return(View()); } else { return(RedirectToAction("Index", "Home")); } }
public ActionResult UpdateProduct(AdminProductModel product, string id) { if (Session["important"] != null || Session["juststaff"] != null) { DAO dao = new DAO(); /** * 1. Upload fileto images dir and generate file name * 2. Create new category with that imgUrl file name */ string defaultImgUrlPath = Server.MapPath("~/Content/Images/no_image.png"); string newImgUrlPath = ""; string fileName = ""; int count = 0; string ext; string extractedName; var validImageTypes = new string[] { "image/gif", "image/jpeg", "image/pjpeg", "image/png" }; // Upload the image if (product.Image != null && product.Image.ContentLength > 0) { if (!validImageTypes.Contains(product.Image.ContentType)) { ViewBag.Status = "ERROR: Unknown image type"; } else { // Create new category if (ModelState.IsValid) { try { fileName = Path.GetFileName(product.Image.FileName); // Change file name // Extract name/extension extractedName = fileName.Split('.')[0]; ext = fileName.Split('.')[1]; // Create new name+ext fileName = product.Name.ToLower() + "." + ext; // Update product in DB count = dao.UpdateProduct(product, fileName, id); } catch (Exception ex) { ViewBag.Status = "DB ERROR!" + ex.Message; } if (count == 1) { ModelState.Clear(); // Get full path newImgUrlPath = Path.Combine(Server.MapPath("~/Content/Images/products"), fileName); // Delete old image string fullPath = Server.MapPath("~/Content/Images/products/" + TempData["oldImage"].ToString()); if (System.IO.File.Exists(fullPath)) { System.IO.File.Delete(fullPath); } // Upload the file product.Image.SaveAs(newImgUrlPath); return(RedirectToAction("Index")); } else { ViewBag.Status = "ERROR! Product create fail"; } return(View()); // Display modal to say category created } else { ViewBag.Status = "ERROR! Model Invalid"; } } } else { // No image file uploaded ViewBag.Status = "ERROR: No image selected"; } return(View()); } else { return(RedirectToAction("Index", "Home")); } }