public async Task <IActionResult> Add(ChapterMainViewModel model) { if (await _rightService.CheckRights(model.CompositionId, await _userManager.GetUserAsync(HttpContext.User))) { var chapters = _chapterService.FindByCompositionId(model.CompositionId); int lastNumber = 0; foreach (var chapter in chapters) { if (chapter.Number > lastNumber) { lastNumber = chapter.Number; } } lastNumber++; Chapter newChapter = new Chapter() { CompositionId = model.CompositionId, Number = lastNumber, Text = "Put your text here" }; _chapterService.Add(newChapter); _compositionService.UpdateLastPublication(model.CompositionId); var dbChapter = _chapterService.FindByCompositionIdAndNumber(model.CompositionId, lastNumber); var url = Url.Action("Index", "Chapter", new { compositionId = model.CompositionId, chapterId = dbChapter.Id }); return(Redirect(url)); } else { return(RedirectToAction("AccessDenied", "Notifications")); } }
public async Task <IActionResult> Index(long compositionId, long?chapterId) { IActionResult result = BadRequest(); var composition = _dbCompositionService.FindById(compositionId); if (composition != null) { long chapterIdCurrent = chapterId ?? 0; Chapter chapter = null; if (chapterIdCurrent > 0) { chapter = await _dbChapterService.FindByIdAsync(chapterIdCurrent); } else { var firstChapter = _dbChapterService.FindByCompositionIdAndNumber(compositionId, 1); if (firstChapter != null) { chapter = firstChapter; } } if (chapter != null) { List <ChapterNumberAndId> chapterNumberAndIds = new List <ChapterNumberAndId>(); var chapters = _dbChapterService.FindByCompositionId(compositionId); foreach (var ch in chapters) { chapterNumberAndIds.Add(new ChapterNumberAndId() { ChapterId = ch.Id, Number = ch.Number }); } byte rating = 1; if (_dbChapterService.LastChapter(chapter)) { var user = await _userManager.GetUserAsync(HttpContext.User); if (user != null) { var rate = _dbRatingService.FindByUserIdAndCompositionId(user.Id, composition.Id); if (rate != null) { rating = rate.Mark; } } } var likes = _dbLikeService.GetLikesByChapterId(chapter.Id); var likesUserIds = new List <string>(); foreach (var like in likes) { likesUserIds.Add(like.UserId); } result = View(new ReaderChapterViewModel() { ChapterId = chapterIdCurrent, ChapterNumbersAndIds = chapterNumberAndIds, CompositionId = compositionId, CompositionName = composition.Name, Text = chapter.Text, ChapterNumber = chapter.Number, LikeUserIds = likesUserIds, Rating = rating }); } else { result = RedirectToAction("EmptyComposition", "Notifications"); } } return(result); }