public IActionResult EditChapter(Nullable <int> id, int diaryId) { EditChapterViewModel viewModel; if (id == null) { viewModel = new EditChapterViewModel(); viewModel.DiaryId = diaryId; } else { var chapter = _chapterRepository.GetById(id.Value); viewModel = new EditChapterViewModel() { Content = chapter.Content, Title = chapter.Name, Location = chapter.Location, Date = chapter.Date.ToShortDateString(), IsPublic = chapter.IsPublic, Id = chapter.Id, DiaryId = diaryId }; } return(View(viewModel)); }
public IActionResult EditChapter(EditChapterViewModel editChapterViewModel) { if (ModelState.IsValid) { compositionService.EditChapter(editChapterViewModel); return(RedirectToAction("EditComposition", new { id = editChapterViewModel.CompositionId })); } return(View(editChapterViewModel)); }
public EditChapterViewModel GetEditChapterViewModel(Chapter chapter) { EditChapterViewModel editChapterViewModel = new EditChapterViewModel { ChapterId = chapter.Id, Name = chapter.Name, Content = chapter.Content }; return(editChapterViewModel); }
public IActionResult EditChapter(int chapterId, int compositionId) { var chapter = compositionService.GetChapter(chapterId); if (chapter == null) { ViewBag.ErrorMessage = "Chapter cannot be found"; return(View("NotFound")); } EditChapterViewModel editChapterViewModel = compositionService.GetEditChapterViewModel(chapter); editChapterViewModel.CompositionId = compositionId; return(View(editChapterViewModel)); }
public IActionResult SaveChapter(EditChapterViewModel viewModel) { if (!DateTime.TryParse(viewModel.Date, out var date)) { ModelState.AddModelError("Date", "Date is invalid"); } if (!ModelState.IsValid) { return(View("EditChapter", viewModel)); } Chapter chapter; if (viewModel.Id == null) { chapter = new Chapter(); chapter.DiaryId = viewModel.DiaryId; _chapterRepository.Add(chapter); } else { chapter = _chapterRepository.GetById(viewModel.Id.Value); } chapter.Name = viewModel.Title; chapter.Location = viewModel.Location; chapter.Date = date; chapter.IsPublic = viewModel.IsPublic; chapter.Content = viewModel.Content; if (viewModel.Image != null) { using var stream = viewModel.Image.OpenReadStream(); var imageBytes = new byte[stream.Length]; stream.Read(imageBytes); if (chapter.Image != null) { chapter.Image.Content = imageBytes; } else { chapter.Image = new AtwImage() { Content = imageBytes }; } } _chapterRepository.SaveChanges(); return(RedirectToAction("EditDiary", "Diary", new { id = chapter.DiaryId })); }
public void EditChapter(EditChapterViewModel editChapterViewModel) { var chapter = GetChapter(editChapterViewModel.ChapterId); chapter.CompositionId = editChapterViewModel.CompositionId; chapter.Name = editChapterViewModel.Name; chapter.Content = editChapterViewModel.Content; chapter.DateOfUpdate = DateTime.Now; if (editChapterViewModel.File != null) { chapter.Image = SaveImage(editChapterViewModel.File); } context.Chapters.Update(chapter); context.SaveChanges(); }