From 739dcfec9c7e5aea3dc1c8bc9810692bd751bc5b Mon Sep 17 00:00:00 2001 From: Christiaan Bloemendaal Date: Mon, 1 Jan 2024 22:19:05 +0100 Subject: [PATCH] refactor!: update data structures to have more data available to frontend --- .../Documents/ChapterProgressDocument.cs | 1 + .../Documents/RequestedChapterDocument.cs | 3 +++ .../Documents/RequestedMangaDocument.cs | 1 + .../Manga/Chapters/MangaChaptersEndpoint.cs | 5 +++- .../Manga/Details/MangaDetailsEndpoint.cs | 25 ++++++++++++++++--- .../Manga/Request/MangaRequestEndpoint.cs | 3 ++- .../Jobs/DownloadChapterSchedulerJob.cs | 1 + src/Mangarr.Backend/Jobs/IndexMangaJob.cs | 9 ++++--- .../Pages/Activity/ContentItem.razor | 2 +- .../Pages/Activity/ContentItem.razor.cs | 2 ++ .../Pages/Manga/Item/ContentHeader.razor | 3 +++ .../Pages/Manga/Item/ContentHeader.razor.cs | 1 + .../Pages/Manga/Item/ContentList.razor | 7 +++--- .../Pages/Manga/Item/ContentListItem.razor | 2 +- .../Pages/Manga/Item/ContentListItem.razor.cs | 1 + .../appsettings.Development.json | 3 +++ .../Models/ChapterProgressModel.cs | 1 + .../Models/MangaChapterModel.cs | 2 ++ .../Models/MangaDetailsModel.cs | 9 ++++--- 19 files changed, 62 insertions(+), 19 deletions(-) diff --git a/src/Mangarr.Backend/Database/Documents/ChapterProgressDocument.cs b/src/Mangarr.Backend/Database/Documents/ChapterProgressDocument.cs index 45bd021..04917ee 100644 --- a/src/Mangarr.Backend/Database/Documents/ChapterProgressDocument.cs +++ b/src/Mangarr.Backend/Database/Documents/ChapterProgressDocument.cs @@ -5,6 +5,7 @@ public class ChapterProgressDocument : DocumentBase public string MangaId { get; set; } = null!; public string ChapterId { get; set; } = null!; public string MangaTitle { get; set; } = null!; + public string ChapterTitle { get; set; } = null!; public double ChapterNumber { get; set; } public bool IsActive { get; set; } public int Progress { get; set; } diff --git a/src/Mangarr.Backend/Database/Documents/RequestedChapterDocument.cs b/src/Mangarr.Backend/Database/Documents/RequestedChapterDocument.cs index 21f879f..aae61d6 100644 --- a/src/Mangarr.Backend/Database/Documents/RequestedChapterDocument.cs +++ b/src/Mangarr.Backend/Database/Documents/RequestedChapterDocument.cs @@ -4,7 +4,10 @@ public class RequestedChapterDocument : DocumentBase { public required string RequestedMangaId { get; set; } public required string ChapterId { get; set; } = null!; + public required string ChapterName { get; set; } = null!; public required double ChapterNumber { get; set; } + public required DateTime ReleaseDate { get; set; } public required bool MarkedForDownload { get; set; } public bool Downloaded { get; set; } + public required DateTime CreationDate { get; set; } = DateTime.UtcNow; } diff --git a/src/Mangarr.Backend/Database/Documents/RequestedMangaDocument.cs b/src/Mangarr.Backend/Database/Documents/RequestedMangaDocument.cs index ce0277d..232d8b3 100644 --- a/src/Mangarr.Backend/Database/Documents/RequestedMangaDocument.cs +++ b/src/Mangarr.Backend/Database/Documents/RequestedMangaDocument.cs @@ -9,4 +9,5 @@ public class RequestedMangaDocument : DocumentBase public required string CoverUrl { get; set; } = null!; public required bool NewChaptersOnly { get; set; } public DateTime? LastScanDate { get; set; } + public required DateTime CreationDate { get; set; } = DateTime.UtcNow; } diff --git a/src/Mangarr.Backend/Endpoints/Manga/Chapters/MangaChaptersEndpoint.cs b/src/Mangarr.Backend/Endpoints/Manga/Chapters/MangaChaptersEndpoint.cs index 69a6a24..67f0c39 100644 --- a/src/Mangarr.Backend/Endpoints/Manga/Chapters/MangaChaptersEndpoint.cs +++ b/src/Mangarr.Backend/Endpoints/Manga/Chapters/MangaChaptersEndpoint.cs @@ -32,7 +32,10 @@ public override async Task HandleAsync(MangaChaptersRequest req, CancellationTok await SendOkAsync(new MangaChaptersResponse { - Data = documents.Select(x => _mapper.Map(x)).ToList() + Data = documents + .OrderByDescending(x => x.ChapterNumber) + .Select(x => _mapper.Map(x)) + .ToList() }, ct); } diff --git a/src/Mangarr.Backend/Endpoints/Manga/Details/MangaDetailsEndpoint.cs b/src/Mangarr.Backend/Endpoints/Manga/Details/MangaDetailsEndpoint.cs index 50b5d78..1a5c692 100644 --- a/src/Mangarr.Backend/Endpoints/Manga/Details/MangaDetailsEndpoint.cs +++ b/src/Mangarr.Backend/Endpoints/Manga/Details/MangaDetailsEndpoint.cs @@ -12,12 +12,18 @@ namespace Mangarr.Backend.Endpoints.Manga.Details; public class MangaDetailsEndpoint : Endpoint { private readonly AniListService _aniListService; - private readonly IMongoCollection _collection; + private readonly IMongoCollection _mangaCollection; + private readonly IMongoCollection _sourceCollection; - public MangaDetailsEndpoint(IMongoCollection collection, AniListService aniListService) + public MangaDetailsEndpoint( + IMongoCollection mangaCollection, + AniListService aniListService, + IMongoCollection sourceCollection + ) { - _collection = collection; + _mangaCollection = mangaCollection; _aniListService = aniListService; + _sourceCollection = sourceCollection; } public override void Configure() @@ -28,7 +34,7 @@ public override void Configure() public override async Task HandleAsync(MangaDetailsRequest req, CancellationToken ct) { - RequestedMangaDocument? document = await _collection + RequestedMangaDocument? document = await _mangaCollection .Find(x => x.Id == req.Id) .FirstOrDefaultAsync(ct); @@ -38,6 +44,16 @@ public override async Task HandleAsync(MangaDetailsRequest req, CancellationToke return; } + SourceDocument? source = await _sourceCollection + .Find(x => x.Identifier == document.SourceId) + .FirstOrDefaultAsync(ct); + + if (source == null) + { + await SendNotFoundAsync(ct); + return; + } + Result result = await _aniListService.GetMedia(document.SearchId); if (result.IsFailed) @@ -58,6 +74,7 @@ await SendOkAsync(new MangaDetailsResponse Data = new MangaDetailsModel { Title = result.Value.Title.English, + SourceName = source.Name, Description = result.Value.DescriptionHtml, CoverImage = result.Value.CoverImage.Large, BannerImage = result.Value.BannerImage diff --git a/src/Mangarr.Backend/Endpoints/Manga/Request/MangaRequestEndpoint.cs b/src/Mangarr.Backend/Endpoints/Manga/Request/MangaRequestEndpoint.cs index 87a8aba..8a58516 100644 --- a/src/Mangarr.Backend/Endpoints/Manga/Request/MangaRequestEndpoint.cs +++ b/src/Mangarr.Backend/Endpoints/Manga/Request/MangaRequestEndpoint.cs @@ -81,7 +81,8 @@ public override async Task HandleAsync(MangaRequestRequest req, CancellationToke SearchId = req.SearchId, Title = title, CoverUrl = media.CoverImage.ExtraLarge, - NewChaptersOnly = req.NewChaptersOnly + NewChaptersOnly = req.NewChaptersOnly, + CreationDate = DateTime.UtcNow }; await _collection.InsertOneAsync(requestedMangaDocument, null, ct); diff --git a/src/Mangarr.Backend/Jobs/DownloadChapterSchedulerJob.cs b/src/Mangarr.Backend/Jobs/DownloadChapterSchedulerJob.cs index be12872..4be6473 100644 --- a/src/Mangarr.Backend/Jobs/DownloadChapterSchedulerJob.cs +++ b/src/Mangarr.Backend/Jobs/DownloadChapterSchedulerJob.cs @@ -61,6 +61,7 @@ public async Task Execute(IJobExecutionContext context) MangaId = manga.Id, MangaTitle = manga.Title, ChapterId = chapter.Id, + ChapterTitle = chapter.ChapterName, ChapterNumber = chapter.ChapterNumber, Progress = 0 }; diff --git a/src/Mangarr.Backend/Jobs/IndexMangaJob.cs b/src/Mangarr.Backend/Jobs/IndexMangaJob.cs index 121dee3..914a61c 100644 --- a/src/Mangarr.Backend/Jobs/IndexMangaJob.cs +++ b/src/Mangarr.Backend/Jobs/IndexMangaJob.cs @@ -78,8 +78,8 @@ private async Task ProcessManga(RequestedMangaDocument manga, CancellationToken .ToListAsync(ct); List newChapters = new(); - - foreach (ChapterListItem chapterListItem in chapterListResult.Value.Items) + List chapterListItems = chapterListResult.Value.Items.OrderBy(x => x.Number).ToList(); + foreach (ChapterListItem chapterListItem in chapterListItems) { RequestedChapterDocument? existingChapter = requestedChapterDocuments .FirstOrDefault(x => x.ChapterId == chapterListItem.Id); @@ -100,9 +100,12 @@ private async Task ProcessManga(RequestedMangaDocument manga, CancellationToken { RequestedMangaId = manga.Id, ChapterId = chapterListItem.Id, + ChapterName = chapterListItem.Name, ChapterNumber = chapterListItem.Number, + ReleaseDate = chapterListItem.Date, MarkedForDownload = markedForDownload, - Downloaded = false + Downloaded = false, + CreationDate = DateTime.UtcNow }; await _chapterCollection.InsertOneAsync(requestedChapterDocument, null, ct); diff --git a/src/Mangarr.Frontend/Pages/Activity/ContentItem.razor b/src/Mangarr.Frontend/Pages/Activity/ContentItem.razor index 6e277b3..30a5051 100644 --- a/src/Mangarr.Frontend/Pages/Activity/ContentItem.razor +++ b/src/Mangarr.Frontend/Pages/Activity/ContentItem.razor @@ -3,7 +3,7 @@ @MangaTitle - Chapter - @ChapterNumber + @ChapterTitle @if (IsActive) { diff --git a/src/Mangarr.Frontend/Pages/Activity/ContentItem.razor.cs b/src/Mangarr.Frontend/Pages/Activity/ContentItem.razor.cs index f415e44..33ff40d 100644 --- a/src/Mangarr.Frontend/Pages/Activity/ContentItem.razor.cs +++ b/src/Mangarr.Frontend/Pages/Activity/ContentItem.razor.cs @@ -11,6 +11,8 @@ public partial class ContentItem private bool IsActive => Item!.IsActive; private string MangaId => Item!.MangaId; private string MangaTitle => Item!.MangaTitle; + private string ChapterId => Item!.ChapterId; + private string ChapterTitle => Item!.ChapterTitle; private double ChapterNumber => Item!.ChapterNumber; private int Progress => Item!.Progress; } diff --git a/src/Mangarr.Frontend/Pages/Manga/Item/ContentHeader.razor b/src/Mangarr.Frontend/Pages/Manga/Item/ContentHeader.razor index 6b8393f..c2617f5 100644 --- a/src/Mangarr.Frontend/Pages/Manga/Item/ContentHeader.razor +++ b/src/Mangarr.Frontend/Pages/Manga/Item/ContentHeader.razor @@ -8,6 +8,9 @@ cover

@Title

+

+ @SourceName +

@((MarkupString)Description)

diff --git a/src/Mangarr.Frontend/Pages/Manga/Item/ContentHeader.razor.cs b/src/Mangarr.Frontend/Pages/Manga/Item/ContentHeader.razor.cs index 4b5085b..9fad362 100644 --- a/src/Mangarr.Frontend/Pages/Manga/Item/ContentHeader.razor.cs +++ b/src/Mangarr.Frontend/Pages/Manga/Item/ContentHeader.razor.cs @@ -8,6 +8,7 @@ public partial class ContentHeader [Parameter] public MangaDetailsModel? Item { get; set; } private string Title => Item!.Title; + private string SourceName => Item!.SourceName; private string Description => Item!.Description; private string CoverImage => Item!.CoverImage; private string BannerImage => Item!.BannerImage; diff --git a/src/Mangarr.Frontend/Pages/Manga/Item/ContentList.razor b/src/Mangarr.Frontend/Pages/Manga/Item/ContentList.razor index eff055a..8a0b958 100644 --- a/src/Mangarr.Frontend/Pages/Manga/Item/ContentList.razor +++ b/src/Mangarr.Frontend/Pages/Manga/Item/ContentList.razor @@ -20,11 +20,10 @@ else - @for (int i = Items.Count - 1; i >= 0; i--) + @for (int i = 0; i < Items.Count; i++) { - int index = i; - MangaChapterModel item = Items[index]; - + MangaChapterModel item = Items[i]; + } diff --git a/src/Mangarr.Frontend/Pages/Manga/Item/ContentListItem.razor b/src/Mangarr.Frontend/Pages/Manga/Item/ContentListItem.razor index 61cbe70..491b9f2 100644 --- a/src/Mangarr.Frontend/Pages/Manga/Item/ContentListItem.razor +++ b/src/Mangarr.Frontend/Pages/Manga/Item/ContentListItem.razor @@ -14,7 +14,7 @@ } @Index - Chapter - @ChapterNumber + @ChapterName @if (Downloaded) { diff --git a/src/Mangarr.Frontend/Pages/Manga/Item/ContentListItem.razor.cs b/src/Mangarr.Frontend/Pages/Manga/Item/ContentListItem.razor.cs index 82a4d10..502389f 100644 --- a/src/Mangarr.Frontend/Pages/Manga/Item/ContentListItem.razor.cs +++ b/src/Mangarr.Frontend/Pages/Manga/Item/ContentListItem.razor.cs @@ -8,6 +8,7 @@ public partial class ContentListItem [Parameter] public MangaChapterModel? Item { get; set; } [Parameter] public int Index { get; set; } + private string ChapterName => Item!.ChapterName; private double ChapterNumber => Item!.ChapterNumber; private bool MarkedForDownload => Item!.MarkedForDownload; private bool Downloaded => Item!.Downloaded; diff --git a/src/Mangarr.Frontend/appsettings.Development.json b/src/Mangarr.Frontend/appsettings.Development.json index 9ca3729..ef444e2 100644 --- a/src/Mangarr.Frontend/appsettings.Development.json +++ b/src/Mangarr.Frontend/appsettings.Development.json @@ -5,5 +5,8 @@ "Default": "Information", "Microsoft.AspNetCore": "Warning" } + }, + "Backend": { + "Host": "https://localhost:7030" } } diff --git a/src/Mangarr.Shared/Models/ChapterProgressModel.cs b/src/Mangarr.Shared/Models/ChapterProgressModel.cs index afd6b4e..e2bedb1 100644 --- a/src/Mangarr.Shared/Models/ChapterProgressModel.cs +++ b/src/Mangarr.Shared/Models/ChapterProgressModel.cs @@ -5,6 +5,7 @@ public class ChapterProgressModel public string MangaId { get; set; } = null!; public string ChapterId { get; set; } = null!; public string MangaTitle { get; set; } = string.Empty; + public string ChapterTitle { get; set; } = string.Empty; public double ChapterNumber { get; set; } public bool IsActive { get; set; } = false; public int Progress { get; set; } diff --git a/src/Mangarr.Shared/Models/MangaChapterModel.cs b/src/Mangarr.Shared/Models/MangaChapterModel.cs index 1efe120..fe26590 100644 --- a/src/Mangarr.Shared/Models/MangaChapterModel.cs +++ b/src/Mangarr.Shared/Models/MangaChapterModel.cs @@ -3,7 +3,9 @@ public class MangaChapterModel { public string Id { get; set; } = null!; + public string ChapterName { get; set; } = null!; public double ChapterNumber { get; set; } + public DateTime ReleaseDate { get; set; } public bool MarkedForDownload { get; set; } public bool Downloaded { get; set; } } diff --git a/src/Mangarr.Shared/Models/MangaDetailsModel.cs b/src/Mangarr.Shared/Models/MangaDetailsModel.cs index b4fc3e9..862b51a 100644 --- a/src/Mangarr.Shared/Models/MangaDetailsModel.cs +++ b/src/Mangarr.Shared/Models/MangaDetailsModel.cs @@ -2,8 +2,9 @@ public class MangaDetailsModel { - public string Title { get; set; } - public string Description { get; set; } - public string CoverImage { get; set; } - public string BannerImage { get; set; } + public required string Title { get; set; } + public required string SourceName { get; set; } + public required string Description { get; set; } + public required string CoverImage { get; set; } + public required string BannerImage { get; set; } }