public ActionResult EditInfo(InfoEditInfoVM model) { UnitOfWork unitOfWork = new UnitOfWork(); InfoRepository infoRepository = new InfoRepository(unitOfWork); Info info = new Info(); if (infoRepository.GetAll(filter: x => x.Title == model.Title && x.ID != model.ID).FirstOrDefault() != null) { ModelState.AddModelError("Title", "Article with same name exist"); } model.Title = model.Title.Trim(); string message = "Successfully created "; if (model.ID > 0) { info = infoRepository.GetByID(model.ID); message = "Successfully edited "; if (info.Title != model.Title) { string filesLocalPath = "~/Content/Files/" + model.Title; if (Directory.Exists(HostingEnvironment.MapPath(filesLocalPath + info.Title))) { Directory.Move(HostingEnvironment.MapPath(filesLocalPath + info.Title), HostingEnvironment.MapPath(filesLocalPath + model.Title)); } } } model.Content = model.Content.Trim(); info.Content = model.Content; info.Title = model.Title; infoRepository.Save(info); InfoImageRepository infoImageRepository = new InfoImageRepository(unitOfWork); HttpFileCollection UploadedFiles = System.Web.HttpContext.Current.Request.Files; string filePath = "~/Content/Files/" + model.Title + "/"; Regex imageRegex = new Regex(ConfigurationManager.AppSettings["ImageRestriction"]); Regex fileRegex = new Regex(ConfigurationManager.AppSettings["FileRestriction"]); for (int i = 0; i < UploadedFiles.Count; i++) { if (imageRegex.Match(UploadedFiles[i].FileName).Success) { InfoImage infoImage = new InfoImage(); infoImage.Name = UploadedFiles[i].FileName; if (infoImageRepository.GetAll(filter: x => x.Name == infoImage.Name && x.InfoID == info.ID).FirstOrDefault() == null) { info.Images = info.Images == null ? info.Images = new List<InfoImage>() : info.Images; info.Images.Add(infoImage); //Create folder if (!Directory.Exists(HostingEnvironment.MapPath(filePath))) Directory.CreateDirectory(HostingEnvironment.MapPath(filePath)); UploadedFiles[i].SaveAs(HostingEnvironment.MapPath(filePath) + UploadedFiles[i].FileName); } } else if (fileRegex.Match(UploadedFiles[i].FileName).Success) { var file = UploadedFiles[i]; //Create folder if (!Directory.Exists(HostingEnvironment.MapPath(filePath))) Directory.CreateDirectory(HostingEnvironment.MapPath(filePath)); //If other file is uploaded we delete the old if (info.FileName != null && System.IO.File.Exists(HostingEnvironment.MapPath(filePath + info.FileName)) && file.FileName != info.FileName) System.IO.File.Delete(HostingEnvironment.MapPath(filePath + info.FileName)); info.FileName = file.FileName; file.SaveAs(HostingEnvironment.MapPath(filePath) + file.FileName); } } infoRepository.Save(info); unitOfWork.Commit(); AddRemoveKeywords(model.KeywordsInput, info.ID); return RedirectToAction("InfoPage", "Info"); }
public ActionResult EditInfo(int? id) { InfoEditInfoVM model = new InfoEditInfoVM(); InfoRepository infoRepository = new InfoRepository(); Info info = new Info(); model.InfoImages = new List<InfoImage>(); if (id != null) { info = infoRepository.GetByID(id.Value); if (info == null) { return RedirectToAction("InfoPage", "Info"); } model.KeywordsInput = string.Empty; if (info.Keywords.Count() > 0) { model.KeywordsInput = string.Join(", ", info.Keywords.OrderBy(o => o.Value).Select(x => x.Value)); } model.ID = id.Value; model.Title = info.Title; model.Content = info.Content; model.KeywordsInput = string.Join(", ", info.Keywords.OrderBy(o => o.Value).Select(x => x.Value)); model.InfoImages = info.Images; model.FilePath = "/Content/Files/" + model.Title; } return View(model); }