Added a conditional check on bookmark API to ensure we don't over or under set progress vs chapter's pages.

This commit is contained in:
Joseph Milazzo 2021-02-11 15:49:58 -06:00
parent 7eb62376eb
commit a6b49052b9

View File

@ -33,7 +33,7 @@ namespace API.Controllers
// Temp let's iterate the directory each call to get next image // Temp let's iterate the directory each call to get next image
var chapter = await _cacheService.Ensure(chapterId); var chapter = await _cacheService.Ensure(chapterId);
if (chapter == null) return BadRequest("There was an issue finding image file for reading."); if (chapter == null) return BadRequest("There was an issue finding image file for reading");
var (path, mangaFile) = await _cacheService.GetCachedPagePath(chapter, page); var (path, mangaFile) = await _cacheService.GetCachedPagePath(chapter, page);
if (string.IsNullOrEmpty(path)) return BadRequest($"No such image for page {page}"); if (string.IsNullOrEmpty(path)) return BadRequest($"No such image for page {page}");
@ -51,25 +51,33 @@ namespace API.Controllers
if (user.Progresses == null) return Ok(0); if (user.Progresses == null) return Ok(0);
var progress = user.Progresses.SingleOrDefault(x => x.AppUserId == user.Id && x.ChapterId == chapterId); var progress = user.Progresses.SingleOrDefault(x => x.AppUserId == user.Id && x.ChapterId == chapterId);
if (progress != null) return Ok(progress.PagesRead); return Ok(progress?.PagesRead ?? 0);
return Ok(0);
} }
[HttpPost("bookmark")] [HttpPost("bookmark")]
public async Task<ActionResult> Bookmark(BookmarkDto bookmarkDto) public async Task<ActionResult> Bookmark(BookmarkDto bookmarkDto)
{ {
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername()); var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername());
_logger.LogInformation($"Saving {user.UserName} progress for Chapter {bookmarkDto.ChapterId} to page {bookmarkDto.PageNum}"); _logger.LogInformation("Saving {UserName} progress for Chapter {ChapterId} to page {PageNum}", user.UserName, bookmarkDto.ChapterId, bookmarkDto.PageNum);
// TODO: Don't let user bookmark past total pages. // Don't let user bookmark past total pages.
var chapter = await _unitOfWork.VolumeRepository.GetChapterAsync(bookmarkDto.ChapterId);
if (bookmarkDto.PageNum > chapter.Pages)
{
return BadRequest("Can't bookmark past max pages");
}
if (bookmarkDto.PageNum < 0)
{
return BadRequest("Can't bookmark less than 0");
}
user.Progresses ??= new List<AppUserProgress>(); user.Progresses ??= new List<AppUserProgress>();
var userProgress = user.Progresses.SingleOrDefault(x => x.ChapterId == bookmarkDto.ChapterId && x.AppUserId == user.Id); var userProgress = user.Progresses.SingleOrDefault(x => x.ChapterId == bookmarkDto.ChapterId && x.AppUserId == user.Id);
if (userProgress == null) if (userProgress == null)
{ {
user.Progresses.Add(new AppUserProgress user.Progresses.Add(new AppUserProgress
{ {
PagesRead = bookmarkDto.PageNum, PagesRead = bookmarkDto.PageNum,