Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Chaptarr.Api.V1/Books/BookResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ public static BookResource ToResource(this Book model, BookResourceMappingOption
var hasFiles = options.Statistics != null
? options.Statistics.BookFileCount > 0
: model.HasFiles;
var mediaType = model.MediaType == BookMediaType.Audiobook ? "audiobook" : "ebook";

// Readarr facade compatibility: foreignBookId is bare only inside a path-declared
// provider dialect. Native Chaptarr responses keep provider prefixes.
Expand Down Expand Up @@ -290,7 +291,7 @@ public static BookResource ToResource(this Book model, BookResourceMappingOption
: (options.IncludeOverview ? (model.Overview ?? string.Empty) : null),
Images = CloneImages(selectedEdition != null ? selectedEdition.Images : model.Images),
Links = links,
Statistics = options.Statistics?.ToResource(),
Statistics = options.Statistics?.ToResource(reportFileAvailability: options.FacadeContext?.MediaType == mediaType),
AddOptions = model.AddOptions,
RemoteCover = string.Empty,
Grabbed = false,
Expand All @@ -299,7 +300,7 @@ public static BookResource ToResource(this Book model, BookResourceMappingOption
NarratorNames = GetNarratorNamesForDisplay(model, selectedEdition),
Duration = GetDuration(model, selectedEdition),
DurationMinutes = GetDurationMinutes(model, selectedEdition),
MediaType = model.MediaType == BookMediaType.Audiobook ? "audiobook" : "ebook",
MediaType = mediaType,
HasFiles = hasFiles,
IsOmnibus = model.IsOmnibus
};
Expand Down
9 changes: 8 additions & 1 deletion src/Chaptarr.Api.V1/Books/BookStatisticsResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ public class BookStatisticsResource
public int AvailableBookCount { get; set; }
public int TotalBookCount { get; set; }
public long SizeOnDisk { get; set; }
internal bool ReportFileAvailability { get; init; }

public decimal PercentOfBooks
{
get
{
if (ReportFileAvailability)
{
return BookFileCount > 0 ? 100 : 0;
}

if (BookCount == 0)
{
return 0;
Expand All @@ -26,7 +32,7 @@ public decimal PercentOfBooks

public static class BookStatisticsResourceMapper
{
public static BookStatisticsResource ToResource(this BookStatistics model)
public static BookStatisticsResource ToResource(this BookStatistics model, bool reportFileAvailability = false)
{
if (model == null)
{
Expand All @@ -35,6 +41,7 @@ public static BookStatisticsResource ToResource(this BookStatistics model)

return new BookStatisticsResource
{
ReportFileAvailability = reportFileAvailability,
BookFileCount = model.BookFileCount,
BookCount = model.BookCount,
AvailableBookCount = model.AvailableBookCount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,14 @@ INSERT INTO ""Editions"" VALUES
{
nameof(IBookService.FindAllByWorkProviderId) => Books.Where(book => book.MediaType == (BookMediaType)args[2]).ToList(),
nameof(IBookService.GetBooks) => Books.Where(book => ((IEnumerable<int>)args[0]).Contains(book.Id)).ToList(),
nameof(IBookService.GetBooksForDisplay) => Books.Where(book =>
(string)args[1] == (book.MediaType == BookMediaType.Audiobook ? "audiobook" : "ebook")).ToList(),
_ => throw new AssertionException(method.Name)
});
Controller = new BookController(null, bookService, null, null, null,
var authorService = Proxy<IAuthorService>((method, args) => method.Name == nameof(IAuthorService.GetAllAuthors)
? Books.Select(book => book.Author).DistinctBy(author => author.Id).ToList()
: throw new AssertionException(method.Name));
Controller = new BookController(authorService, bookService, null, null, null,
Proxy<ISeriesBookLinkService>((method, args) => new List<SeriesBookLink>()), Service,
Proxy<IMediaFileService>((method, args) =>
{
Expand Down Expand Up @@ -220,6 +225,54 @@ public void title_slug_candidates_should_load_only_their_authors_once(bool postg
Assert.That(harness.Results.Last().Select(stat => stat.AuthorId).Distinct(), Is.EqualTo(new[] { 1 }));
}

[TestCase(false)]
[TestCase(true)]
public void facade_book_list_should_report_existing_files_independently_of_monitoring(bool postgres)
{
using var harness = new Harness(postgres);
var controller = harness.Controller;
var items = controller.HttpContext.Items;
var audio = new ReadarrFacadeContext("hc", "audiobook", "/audiobook");
var ebook = new ReadarrFacadeContext("gr", "ebook", "/readarr/gr/ebook");
harness.Books.Single(book => book.Id == 1).AudiobookMonitored = false;
harness.Connection.Execute("UPDATE \"Books\" SET \"AudiobookMonitored\" = FALSE WHERE \"Id\" = 1");

BookResource ReadBook(ReadarrFacadeContext facade, int id, string mediaType)
{
items[ReadarrFacadeContext.ItemKey] = facade;
return controller.GetBooks(null, null, null, mediaType: mediaType).Single(book => book.Id == id);
}

var ownedAudio = ReadBook(audio, 1, "audiobook");
var ownedEbook = ReadBook(ebook, 2, "ebook");
items[ReadarrFacadeContext.ItemKey] = audio;
var wrongFormat = controller.GetBooks(null, new List<int> { 2 }, null, mediaType: "audiobook").Single();
Assert.Multiple(() =>
{
Assert.That(ownedAudio.Statistics.BookFileCount, Is.EqualTo(2));
Assert.That(ownedAudio.Statistics.BookCount, Is.Zero);
Assert.That(ownedAudio.Statistics.AvailableBookCount, Is.Zero);
Assert.That(ownedAudio.Statistics.TotalBookCount, Is.Zero);
Assert.That(ownedAudio.Statistics.PercentOfBooks, Is.EqualTo(100));
Assert.That(ownedAudio.Monitored, Is.False);
Assert.That(ownedEbook.Statistics.PercentOfBooks, Is.EqualTo(100));
Assert.That(ownedEbook.Monitored, Is.False);
Assert.That(wrongFormat.Statistics.PercentOfBooks, Is.Zero);
Assert.That(ReadBook(null, 1, "audiobook").Statistics.PercentOfBooks, Is.Zero);
});

harness.Connection.Execute("DELETE FROM \"BookFiles\" WHERE \"Id\" = 3");
harness.Service.InvalidateAuthorCache(1);
Assert.That(ReadBook(ebook, 2, "ebook").Statistics.PercentOfBooks, Is.Zero);
Assert.That(ReadBook(audio, 1, "audiobook").Statistics.PercentOfBooks, Is.EqualTo(100));

harness.Connection.Execute("DELETE FROM \"BookFiles\" WHERE \"Id\" IN (1, 2)");
harness.Service.InvalidateAuthorCache(1);
var missingAudio = ReadBook(audio, 1, "audiobook");
Assert.That(missingAudio.Statistics.PercentOfBooks, Is.Zero);
Assert.That(missingAudio.Monitored, Is.False);
}

[TestCase(false, true)]
[TestCase(true, true)]
[TestCase(false, null)]
Expand Down
Loading