Kavita/API/Extensions/VolumeListExtensions.cs
Joseph Milazzo 591b574706
Unit Tests & New Natural Sort (#941)
* Added a lot of tests

* More tests! Added a Parser.NormalizePath to normalize all paths within Kavita.

* Fixed a bug where MarkChaptersAsUnread implementation wasn't consistent between different files and lead to extra row generation for no reason.

* Added more unit tests

* Found a better implementation for Natural Sorting. Added tests and validate it works. Next commit will swap out natural Sort for new Extension.

* Replaced NaturalSortComparer with OrderByNatural.

* Drastically simplified and sped up FindFirstEntry for finding cover images in archives

* Initial fix for a epub bug where metadata defines key as absolute path but document uses a relative path. We now have a hack to correct for the epub.
2022-01-15 07:39:34 -08:00

41 lines
1.5 KiB
C#

using System.Collections.Generic;
using System.Linq;
using API.Comparators;
using API.Entities;
using API.Entities.Enums;
namespace API.Extensions
{
public static class VolumeListExtensions
{
public static Volume FirstWithChapters(this IEnumerable<Volume> volumes, bool inBookSeries)
{
return inBookSeries
? volumes.FirstOrDefault(v => v.Chapters.Any())
: volumes.OrderBy(v => v.Number, new ChapterSortComparer())
.FirstOrDefault(v => v.Chapters.Any());
}
/// <summary>
/// Selects the first Volume to get the cover image from. For a book with only a special, the special will be returned.
/// If there are both specials and non-specials, then the first non-special will be returned.
/// </summary>
/// <param name="volumes"></param>
/// <param name="seriesFormat"></param>
/// <returns></returns>
public static Volume GetCoverImage(this IList<Volume> volumes, MangaFormat seriesFormat)
{
if (seriesFormat is MangaFormat.Epub or MangaFormat.Pdf)
{
return volumes.OrderBy(x => x.Number).FirstOrDefault();
}
if (volumes.Any(x => x.Number != 0))
{
return volumes.OrderBy(x => x.Number).FirstOrDefault(x => x.Number != 0);
}
return volumes.OrderBy(x => x.Number).FirstOrDefault();
}
}
}