public virtual ActionResult UpdateProduct(ProductEditViewModel model, string _PrimryImage) { var product = _productService.GetById(model.Id); if (model.Image != null) { if (model.Image.ContentLength > 0) { //delete with 3 address TODO.DeleteImage(Path.Combine(Server.MapPath("~/Content/Images/Product/MainSize/") + _PrimryImage)); TODO.DeleteImage(Path.Combine(Server.MapPath("~/Content/Images/Product/ThumbLine_New/") + _PrimryImage)); TODO.DeleteImage(Path.Combine(Server.MapPath("~/Content/Images/Product/ThumbLine_Offer") + _PrimryImage)); //add with 3 address using (var img = Image.FromStream(model.Image.InputStream)) { string fileName = TODO.CheckExistFile(Server.MapPath("~/Content/Images/Product/MainSize/"), model.Image.FileName); TODO.UploadImage(img, new Size(180, 180), Server.MapPath("~/Content/Images/Product/MainSize/"), fileName); TODO.UploadImage(img, new Size(70, 70), Server.MapPath("~/Content/Images/Product/ThumbLine_New/"), fileName); TODO.UploadImage(img, new Size(60, 75), Server.MapPath("~/Content/Images/Product/ThumbLine_Offer"), fileName); product.PrimryImage = fileName; } } } product.Explain = model.Explain; product.Name = model.Name; product.Time = model.Time; product.Date = model.Date; product.Price = model.Price; product.Company = _companyService.GetById(model.Company_Id); product.Category = _categoryService.GetById(model.Category_Id); product.Labels.Clear(); product.Labels = null; if (model.LabelsId != null) { var labels = _labelService.GetLabelsByIds(model.LabelsId); product.Labels = labels; } else { product.Labels = null; } product.ModifiedDate = DateTime.Now; _productService.Update(product); if (_unitOfWork.SaveAllChanges() > 0) { //Remove Id From Document LuceneProductSearch.ClearLuceneIndexRecord(model.Id); //Add New Document LuceneProductSearch.AddUpdateLuceneIndex(new LuceneProductModel { Product_Id = model.Id, Product_Explain = model.Explain.ToSafeHtml().RemoveHtmlTags(), Product_Name = model.Name }); return(RedirectToAction(actionName: MVC.admin.Product.ActionNames.DetailsProduct, routeValues: new { Id = model.Id })); } else { TempData["createProduct"] = Helperalert.alert(new AlertViewModel { Alert = AlertOperation.SurveyOperation(StatusOperation.FailUpdate), Status = AlertMode.warning }); return(RedirectToAction(actionName: MVC.admin.Product.ActionNames.UpdateProduct, routeValues: new { Id = model.Id })); } }
public virtual ActionResult CreateProduct(ProductViewModel model, IEnumerable <HttpPostedFileBase> ImageProductAdditinal) { var product = new Product() { Explain = model.Explain, Category = _categoryService.GetById(model.Category_Id), Company = _companyService.GetById(model.Company_Id), Name = model.Name, Labels = _labelService.GetLabelsByIds(model.Labels), Time = model.Time, Date = model.Date, VisitNumber = 0, Price = model.Price, Recomend = 1, ModifiedDate = DateTime.Now }; //save primary image with 3 size if (model.Image != null) { if (model.Image.ContentLength > 0) { using (var img = Image.FromStream(model.Image.InputStream)) { string fileName = TODO.CheckExistFile(Server.MapPath("~/Content/Images/Product/MainSize/"), model.Image.FileName); TODO.UploadImage(img, new Size(180, 180), Server.MapPath("~/Content/Images/Product/MainSize/"), fileName); TODO.UploadImage(img, new Size(70, 70), Server.MapPath("~/Content/Images/Product/ThumbLine_New/"), fileName); TODO.UploadImage(img, new Size(60, 75), Server.MapPath("~/Content/Images/Product/ThumbLine_Offer"), fileName); product.PrimryImage = fileName; } } } //save other image for this prduct var listImage = new List <ImageProduct>(); foreach (var item in ImageProductAdditinal) { if (item != null && item.ContentLength > 0) { string fileName = TODO.CheckExistFile(Server.MapPath("~/Content/Images/Product/GallerySize/"), model.Image.FileName); TODO.UploadImage(Image.FromStream(item.InputStream), new Size(450, 450), Server.MapPath("~/Content/Images/Product/GallerySize/"), fileName); listImage.Add(new ImageProduct { Image = fileName, Product = product }); } } product.ImageProducts = listImage; _productService.Create(product); if (_unitOfWork.SaveAllChanges() > 0) { //For Use Notification With SignalR //Response Redirect To Main Page In Site //return RedirectToAction(MVC.admin.Product.ActionNames.DetailsProduct, new { Id = product.Id }); if (Request.Cookies["Cart"] != null) { HttpCookie cookie = Request.Cookies["Cart"]; cookie.Value = "true"; cookie.Expires = DateTime.Now.AddMinutes(2); System.Web.HttpContext.Current.Response.Cookies.Set(cookie); } else { HttpCookie cookie = new HttpCookie("Alert"); cookie.Value = "true"; cookie.Expires = DateTime.Now.AddMinutes(2); System.Web.HttpContext.Current.Response.Cookies.Add(cookie); } LuceneProductSearch.AddUpdateLuceneIndex(new LuceneProductModel { Product_Id = product.Id, Product_Name = product.Name, Product_Explain = product.Explain.Length > 400 ? product.Explain.ToSafeHtml().RemoveHtmlTags().Substring(0, 400) : product.Explain.ToSafeHtml().RemoveHtmlTags() }); return(RedirectToRoute("DetailsProduct", new { productId = product.Id, productName = UrlExtensions.ResolveTitleForUrl(product.Name) })); } else { TempData["createProduct"] = Helperalert.alert(new AlertViewModel { Alert = AlertOperation.SurveyOperation(StatusOperation.FailInsert), Status = AlertMode.warning }); return(RedirectToAction(MVC.admin.Product.ActionNames.CreateProduct)); } }