Fixed a bug on getting next/previous chapters where if chapters were … (#281)

* Fixed a bug on getting next/previous chapters where if chapters were not inserted into the DB in the natural order, then the next/prev chapter would be skewed.

* Make GetNextChapterId static
This commit is contained in:
Joseph Milazzo 2021-06-07 14:49:38 -05:00 committed by GitHub
parent 5259a1484a
commit 41a5d1bf2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -272,20 +272,10 @@ namespace API.Controllers
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername());
var volumes = await _unitOfWork.SeriesRepository.GetVolumesDtoAsync(seriesId, user.Id);
var currentVolume = await _unitOfWork.SeriesRepository.GetVolumeAsync(volumeId);
var currentChapter = await _unitOfWork.VolumeRepository.GetChapterAsync(currentChapterId);
if (currentVolume.Number == 0)
{
var next = false;
foreach (var chapter in currentVolume.Chapters)
{
if (next)
{
return Ok(chapter.Id);
}
if (currentChapterId == chapter.Id) next = true;
}
var chapterId = GetNextChapterId(currentVolume.Chapters.OrderBy(x => double.Parse(x.Number), _chapterSortComparer), currentChapterId);
var chapterId = GetNextChapterId(currentVolume.Chapters.OrderBy(x => double.Parse(x.Number), _chapterSortComparer), currentChapter.Number);
if (chapterId > 0) return Ok(chapterId);
}
@ -293,7 +283,7 @@ namespace API.Controllers
{
if (volume.Number == currentVolume.Number && volume.Chapters.Count > 1)
{
var chapterId = GetNextChapterId(currentVolume.Chapters.OrderBy(x => double.Parse(x.Number), _chapterSortComparer), currentChapterId);
var chapterId = GetNextChapterId(currentVolume.Chapters.OrderBy(x => double.Parse(x.Number), _chapterSortComparer), currentChapter.Number);
if (chapterId > 0) return Ok(chapterId);
}
@ -305,7 +295,7 @@ namespace API.Controllers
return Ok(-1);
}
private int GetNextChapterId(IEnumerable<Chapter> chapters, int currentChapterId)
private static int GetNextChapterId(IEnumerable<Chapter> chapters, string currentChapterNumber)
{
var next = false;
foreach (var chapter in chapters)
@ -314,7 +304,7 @@ namespace API.Controllers
{
return chapter.Id;
}
if (currentChapterId == chapter.Id) next = true;
if (currentChapterNumber.Equals(chapter.Number)) next = true;
}
return -1;
@ -333,11 +323,11 @@ namespace API.Controllers
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername());
var volumes = await _unitOfWork.SeriesRepository.GetVolumesDtoAsync(seriesId, user.Id);
var currentVolume = await _unitOfWork.SeriesRepository.GetVolumeAsync(volumeId);
var currentChapter = await _unitOfWork.VolumeRepository.GetChapterAsync(currentChapterId);
if (currentVolume.Number == 0)
{
var chapterId = GetNextChapterId(currentVolume.Chapters.OrderBy(x => double.Parse(x.Number), _chapterSortComparer).Reverse(), currentChapterId);
var chapterId = GetNextChapterId(currentVolume.Chapters.OrderBy(x => double.Parse(x.Number), _chapterSortComparer).Reverse(), currentChapter.Number);
if (chapterId > 0) return Ok(chapterId);
}
@ -345,7 +335,7 @@ namespace API.Controllers
{
if (volume.Number == currentVolume.Number)
{
var chapterId = GetNextChapterId(currentVolume.Chapters.OrderBy(x => double.Parse(x.Number), _chapterSortComparer).Reverse(), currentChapterId);
var chapterId = GetNextChapterId(currentVolume.Chapters.OrderBy(x => double.Parse(x.Number), _chapterSortComparer).Reverse(), currentChapter.Number);
if (chapterId > 0) return Ok(chapterId);
}
if (volume.Number == currentVolume.Number - 1)