public ActionResult Delete(int id, int idFile) { bool isDeleted = false; Product product = _productManager.GetProductById(id); ProductFiles productFiles = _productManager.GetProductFilesById(idFile); if (product != null) { isDeleted = _productManager.Delete(product, productFiles); } if (isDeleted) { ViewBag.DMsg = "Deleted"; } else { ViewBag.FMsg = "not deleted"; } return(RedirectToAction("Add")); }
public ActionResult ProductCreate(Product product) { DefaultLoad(); try { if (product.UploadFiles != null && product.UploadFiles[0] != null) { var productFiles = new List <ProductFiles>(); foreach (var uploadFile in product.UploadFiles) { var productFile = new ProductFiles(); var fileByte = new byte[uploadFile.ContentLength]; uploadFile.InputStream.Read(fileByte, 0, uploadFile.ContentLength); productFile.File = fileByte; productFile.FileName = uploadFile.FileName; productFiles.Add(productFile); } product.ProductFileses = productFiles; } bool isSave; isSave = _productManager.SaveCategory(product); if (isSave) { ViewBag.SMsg = "Save Successfully !!"; } else { ViewBag.FMsg = "Save Failed !!"; } } catch (Exception e) { ViewBag.FMsg = e.Message; } ViewBag.CategoryDropdownList = new SelectList(_db.Categories, "Id", "Name", product.CategoryId); return(View()); }
public ActionResult ProductTemplate(int id, Product p, HttpPostedFileBase Picture = null, HttpPostedFileBase Manual = null) { string fullPicPath = null; string fullManPath = null; if (Picture != null) { var path = System.IO.Path.GetFileName(Picture.FileName); fullPicPath = "/Content/images/" + MD5Hasher.Hash(DateTime.Now.ToShortTimeString() + DateTime.Now.ToShortDateString()) + path; fullPicPath = fullPicPath.Replace(" ", "_"); Picture.SaveAs(Server.MapPath(fullPicPath)); } if (Manual != null) { var path = System.IO.Path.GetFileName(Manual.FileName); fullManPath = "/Content/manuals/" + MD5Hasher.Hash(DateTime.Now.ToShortTimeString() + DateTime.Now.ToShortDateString()) + path; fullManPath = fullManPath.Replace(" ", "_"); Manual.SaveAs(Server.MapPath(fullManPath)); } int uid; string utoken; HttpCookie cookie = Request.Cookies["User"]; if (cookie != null) { uid = int.Parse(cookie["id"]); utoken = cookie["token"]; } else { uid = 0; utoken = null; } if (auth.CheckAuthStatus(uid, utoken) && auth.GetCurrentUser(cookie)["Role"] == "1") { ViewBag.RoleNum = auth.GetCurrentUser(cookie)["Role"]; ViewBag.User = auth.GetCurrentUser(cookie)["User"]; ViewBag.Role = db.Roles.Find(int.Parse(auth.GetCurrentUser(cookie)["Role"])).Name; if (id == 0) { db.Product.Add(p); db.SaveChanges(); ProductFiles pf = new ProductFiles { Manual = fullManPath, Picture = fullPicPath, ProductID = p.ID }; db.ProductFiles.Add(pf); db.SaveChanges(); } else { db.Product.Find(id).Name = p.Name; db.Product.Find(id).ShortDescription = p.ShortDescription; db.Product.Find(id).FullDescription = p.FullDescription; if (Manual != null) { db.ProductFiles.Where(x => x.ProductID == p.ID).FirstOrDefault().Manual = fullManPath; } if (Picture != null) { db.ProductFiles.Where(x => x.ProductID == p.ID).FirstOrDefault().Picture = fullPicPath; } db.SaveChanges(); } return(Redirect("~/Products")); } else { return(Redirect("~/Login")); } }
//public bool Delete(Product product) //{ // bool isDeleted = _productRepository.Delete(product); // return isDeleted; //} public ProductFiles GetProductFilesById(int id) { ProductFiles productFiles = _productRepository.GetProductFilesById(id); return(productFiles); }
public bool Delete(Product product, ProductFiles productFiles) { bool isDeleted = _productRepository.Delete(product, productFiles); return(isDeleted); }
//public bool Delete(Product product) //{ // bool isDeleted = false; // db.Products.Remove(product); // int isChanged = db.SaveChanges(); // if (isChanged > 0) // { // isDeleted = true; // } // return isDeleted; //} public ProductFiles GetProductFilesById(int id) { ProductFiles productFiles = db.ProductFiles.Where(c => c.Id == id).FirstOrDefault(); return(productFiles); }