public IActionResult Details(int id) { HikeVM travel = _db.Hikes.Where(x => x.Id == id) .Include(c => c.Files) .Select(x => new HikeVM(x)) .First(); if (_db.HikesHistory.Any(x => x.HikeId == travel.Id)) { IList <HistoryHikeDTO> travelHistoryDto = _db.HikesHistory.Where(x => x.HikeId == travel.Id) .Include(x => x.Hike) .OrderBy(x => x.ModifyTime) .ToList(); if (travel.History is null) { travel.History = new List <HistoryHikeVM>(); } foreach (HistoryHikeDTO hikeHistDto in travelHistoryDto) { travel.History.Add(new HistoryHikeVM(hikeHistDto)); } } return(View(travel)); }
public IActionResult Create() { HikeVM model = new HikeVM(); model.StartDate = DateTime.Now; model.EndDate = DateTime.Now.AddDays(1.0); return(View(model)); }
/* * Constructor allows mapping ViewModel on DataTransferObject * to avoid repetition of code */ public HikeDTO(HikeVM vModel) { Id = vModel.Id; Title = vModel.Title; StartDate = vModel.StartDate; EndDate = vModel.EndDate; StartPlace = vModel.StartPlace; EndPlace = vModel.EndPlace; Distance = vModel.Distance; }
public IActionResult Edit(int id) { HikeVM travel = _db.Hikes.Where(x => x.Id == id) .Include(c => c.Files) .Select(x => new HikeVM(x)) .First(); if (travel == null) { TempData["EditResult"] = "Nie istnieje taka wędrówka"; return(RedirectToAction("Index")); } return(View(travel)); }
public IActionResult Edit(HikeVM model, IEnumerable <IFormFile> files) { if (!ModelState.IsValid) { return(View(model)); } if (!_db.Hikes.Any(x => x.Id == model.Id)) { TempData["RecordNotExist"] = "Wyprawa którą próbujesz edytować nie istnieje, możesz ją dodać"; return(RedirectToAction("Create", new { model = model })); } History hist = new History(_db); hist.Update(model); #region Upload Image //create structure of directories var rootDir = _environment.ContentRootPath; var imagesDir = rootDir + "\\wwwroot\\Hikes\\Files\\"; var hikeDir = imagesDir + "\\Hike_" + model.Id; if (!Directory.Exists(imagesDir)) { Directory.CreateDirectory(imagesDir); } if (!Directory.Exists(hikeDir)) { Directory.CreateDirectory(hikeDir); } if (files != null && files.Any()) { foreach (IFormFile file in files) { //check file extension string ext = file.ContentType.ToLower(); if (ext != "image/jpg" && ext != "image/jpeg" && ext != "image/pjpeg" && ext != "image/gif" && ext != "image/png" && ext != "image/x-png") { ModelState.AddModelError("", "Obraz nie został przesłany - nieprawidłowe rozszerzenie obrazu."); return(View(model)); } using (var fileStream = new FileStream(Path.Combine(hikeDir, file.FileName), FileMode.Create, FileAccess.Write)) { file.CopyTo(fileStream); } string displayName = file.FileName; if (file.FileName.Contains(".")) { int index = displayName.IndexOf("."); displayName = displayName.Remove(0, index); } //save image name to db FileDTO fileDto = new FileDTO() { FileName = file.FileName, DisplayName = displayName, FileType = ext, HikeId = model.Id, UploadTime = DateTime.Now }; _db.Files.Add(fileDto); _db.SaveChanges(); } } #endregion TempData["EditSuccess"] = "Edycja zakończona sukcesem!"; return(RedirectToAction("Details", new { Id = model.Id })); }
public IActionResult Create(HikeVM model, IEnumerable <IFormFile> files) { if (!ModelState.IsValid) { return(View(model)); } if (_db.Hikes.Any(x => x.Title == model.Title)) { TempData["TitleExist"] = "Wędrówka o takim tytule już istnieje - zmień go"; return(View(model)); } HikeDTO toAdd = new HikeDTO(model); _db.Hikes.Add(toAdd); _db.SaveChanges(); #region Upload Image //create structure of directories var rootDir = _environment.ContentRootPath; var imagesDir = rootDir + "\\wwwroot\\Hikes\\Files\\"; var hikeDir = imagesDir + "\\Hike_" + toAdd.Id; if (!Directory.Exists(imagesDir)) { Directory.CreateDirectory(imagesDir); } if (!Directory.Exists(hikeDir)) { Directory.CreateDirectory(hikeDir); } if (files != null && files.Any()) { foreach (IFormFile file in files) { //check file extension string ext = file.ContentType.ToLower(); if (ext != "image/jpg" && ext != "image/jpeg" && ext != "image/pjpeg" && ext != "image/gif" && ext != "image/png" && ext != "image/x-png") { ModelState.AddModelError("", "Obraz nie został przesłany - nieprawidłowe rozszerzenie obrazu."); return(View(model)); } using (var fileStream = new FileStream(Path.Combine(hikeDir, file.FileName), FileMode.Create, FileAccess.Write)) { file.CopyTo(fileStream); } string displayName = file.FileName; if (file.FileName.Contains(".")) { int index = displayName.IndexOf("."); displayName = displayName.Remove(0, index); } //save image name to db FileDTO fileDto = new FileDTO() { FileName = file.FileName, DisplayName = displayName, FileType = ext, HikeId = toAdd.Id, UploadTime = DateTime.Now }; _db.Files.Add(fileDto); _db.SaveChanges(); } } #endregion return(RedirectToAction("Details", new{ Id = toAdd.Id })); }
public void Update(HikeVM toUpdate) { if (toUpdate.Id == null || toUpdate.Id == 0) { return; } HikeDTO travel = _db.Hikes.Find(toUpdate.Id); if (toUpdate.Title != travel.Title) { Attribute lblAttr = toUpdate.Title.GetType().IsDefined(typeof(DisplayAttribute))? toUpdate.Title.GetType().GetCustomAttribute(typeof(DisplayAttribute)): null; string fieldLbl = toUpdate.GetType().GetProperty("Title").Name; if (lblAttr != null) { fieldLbl = lblAttr.GetType().GetProperty("Name").GetValue(lblAttr).ToString(); } _db.HikesHistory.Add(new HistoryHikeDTO() { HikeId = travel.Id, ModifyTime = DateTime.Now, OldValue = travel.Title, NewValue = toUpdate.Title, Description = "Modyfikacja pola " + fieldLbl }); travel.Title = toUpdate.Title; } if (toUpdate.StartDate != travel.StartDate) { Attribute lblAttr = toUpdate.StartDate.GetType().IsDefined(typeof(DisplayAttribute)) ? toUpdate.StartDate.GetType().GetCustomAttribute(typeof(DisplayAttribute)) : null; string fieldLbl = toUpdate.GetType().GetProperty("StartDate").Name; if (lblAttr != null) { fieldLbl = lblAttr.GetType().GetProperty("Name").GetValue(lblAttr).ToString(); } _db.HikesHistory.Add(new HistoryHikeDTO() { HikeId = travel.Id, ModifyTime = DateTime.Now, OldValue = travel.StartDate.ToString(), NewValue = toUpdate.StartDate.ToString(), Description = "Modyfikacja pola " + fieldLbl }); travel.StartDate = toUpdate.StartDate; } if (toUpdate.EndDate != travel.EndDate) { Attribute lblAttr = toUpdate.EndDate.GetType().IsDefined(typeof(DisplayAttribute)) ? toUpdate.EndDate.GetType().GetCustomAttribute(typeof(DisplayAttribute)) : null; string fieldLbl = toUpdate.GetType().GetProperty("EndDate").Name; if (lblAttr != null) { fieldLbl = lblAttr.GetType().GetProperty("Name").GetValue(lblAttr).ToString(); } _db.HikesHistory.Add(new HistoryHikeDTO() { HikeId = travel.Id, ModifyTime = DateTime.Now, OldValue = travel.EndDate.ToString(), NewValue = toUpdate.EndDate.ToString(), Description = "Modyfikacja pola " + fieldLbl }); travel.EndDate = toUpdate.EndDate; } if (toUpdate.StartPlace != travel.StartPlace) { Attribute lblAttr = toUpdate.StartPlace.GetType().IsDefined(typeof(DisplayAttribute)) ? toUpdate.StartPlace.GetType().GetCustomAttribute(typeof(DisplayAttribute)) : null; string fieldLbl = toUpdate.GetType().GetProperty("StartPlace").Name; if (lblAttr != null) { fieldLbl = lblAttr.GetType().GetProperty("Name").GetValue(lblAttr).ToString(); } _db.HikesHistory.Add(new HistoryHikeDTO() { HikeId = travel.Id, ModifyTime = DateTime.Now, OldValue = travel.StartPlace, NewValue = toUpdate.StartPlace, Description = "Modyfikacja pola " + fieldLbl }); travel.StartPlace = toUpdate.StartPlace; } if (toUpdate.EndPlace != travel.EndPlace) { Attribute lblAttr = toUpdate.EndPlace.GetType().IsDefined(typeof(DisplayAttribute)) ? toUpdate.EndPlace.GetType().GetCustomAttribute(typeof(DisplayAttribute)) : null; string fieldLbl = toUpdate.GetType().GetProperty("EndPlace").Name; if (lblAttr != null) { fieldLbl = lblAttr.GetType().GetProperty("Name").GetValue(lblAttr).ToString(); } _db.HikesHistory.Add(new HistoryHikeDTO() { HikeId = travel.Id, ModifyTime = DateTime.Now, OldValue = travel.EndPlace, NewValue = toUpdate.EndPlace, Description = "Modyfikacja pola " + fieldLbl }); travel.EndPlace = toUpdate.EndPlace; } if (toUpdate.Distance != travel.Distance) { Attribute lblAttr = toUpdate.Distance.GetType().IsDefined(typeof(DisplayAttribute)) ? toUpdate.Distance.GetType().GetCustomAttribute(typeof(DisplayAttribute)) : null; string fieldLbl = toUpdate.GetType().GetProperty("Distance").Name; if (lblAttr != null) { fieldLbl = lblAttr.GetType().GetProperty("Name").GetValue(lblAttr).ToString(); } _db.HikesHistory.Add(new HistoryHikeDTO() { HikeId = travel.Id, ModifyTime = DateTime.Now, OldValue = travel.Distance.ToString(), NewValue = toUpdate.Distance.ToString(), Description = "Modyfikacja pola " + fieldLbl }); travel.Distance = toUpdate.Distance; } _db.Update(travel); _db.SaveChanges(); }