Refactored the setContinuePoint code from the UI to the backend. Now, through an API, we can get the chapter to restart from. (#973)

This commit is contained in:
Joseph Milazzo 2022-01-20 15:49:26 -08:00 committed by GitHub
parent 3eb494dff4
commit 218f642870
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 281 additions and 59 deletions

View file

@ -356,6 +356,19 @@ namespace API.Controllers
return BadRequest("Could not save progress");
}
/// <summary>
/// Continue point is the chapter which you should start reading again from. If there is no progress on a series, then the first chapter will be returned (non-special unless only specials).
/// Otherwise, loop through the chapters and volumes in order to find the next chapter which has progress.
/// </summary>
/// <returns></returns>
[HttpGet("continue-point")]
public async Task<ActionResult<ChapterDto>> GetContinuePoint(int seriesId)
{
var userId = await _unitOfWork.UserRepository.GetUserIdByUsernameAsync(User.GetUsername());
return Ok(await _readerService.GetContinuePoint(seriesId, userId));
}
/// <summary>
/// Returns a list of bookmarked pages for a given Chapter
/// </summary>

View file

@ -22,6 +22,7 @@ public interface IReaderService
Task<int> CapPageToChapter(int chapterId, int page);
Task<int> GetNextChapterIdAsync(int seriesId, int volumeId, int currentChapterId, int userId);
Task<int> GetPrevChapterIdAsync(int seriesId, int volumeId, int currentChapterId, int userId);
Task<ChapterDto> GetContinuePoint(int seriesId, int userId);
}
public class ReaderService : IReaderService
@ -305,6 +306,39 @@ public class ReaderService : IReaderService
return -1;
}
public async Task<ChapterDto> GetContinuePoint(int seriesId, int userId)
{
// Loop through all chapters that are not in volume 0
var volumes = (await _unitOfWork.VolumeRepository.GetVolumesDtoAsync(seriesId, userId)).ToList();
var nonSpecialChapters = volumes
.Where(v => v.Number != 0)
.SelectMany(v => v.Chapters)
.OrderBy(c => float.Parse(c.Number))
.ToList();
var currentlyReadingChapter = nonSpecialChapters.FirstOrDefault(chapter => chapter.PagesRead < chapter.Pages);
// Check if there are any specials
if (currentlyReadingChapter == null)
{
var volume = volumes.SingleOrDefault(v => v.Number == 0);
if (volume == null) return nonSpecialChapters.First();
foreach (var chapter in volume.Chapters.OrderBy(c => float.Parse(c.Number)))
{
if (chapter.PagesRead < chapter.Pages)
{
currentlyReadingChapter = chapter;
break;
}
}
}
return currentlyReadingChapter ?? nonSpecialChapters.First();
}
private static int GetNextChapterId(IEnumerable<ChapterDto> chapters, string currentChapterNumber)
{