private void LoadInfoData() { TracktorDb context = new TracktorDb(); InfoRepository infoRepo = new InfoRepository(context); List <InfoEntity> infos = infoRepo.GetAll().ToList(); AllInfos = new BindingList <InfoEntity>(infos); }
public ActionResult InfoPage() { InfoInfoPageVM model = new InfoInfoPageVM(); if (!ModelState.IsValid) { return View(model); } this.TryUpdateModel(model); InfoRepository infoRepository = new InfoRepository(); List<Info> filteredInfo = new List<Info>(); if (model.Search == null) { filteredInfo = infoRepository.GetAll(); } else { filteredInfo = infoRepository.GetAll(filter: x => x.Keywords.Any(k => k.Value.Contains(model.Search))); } model.Search = model.Search == null ? model.Search : model.Search.Trim(' '); model.Info = filteredInfo; if ((Models.AuthenticationManager.LoggedUser.GetType().Name).Split('_').GetValue(0).ToString() == "Administrator") { model.CanBeEdited = true; } return View(model); }
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"); }