mirror of
https://github.com/C9Glax/tranga.git
synced 2025-05-22 06:03:01 +02:00
62 lines
2.2 KiB
C#
62 lines
2.2 KiB
C#
using System.ComponentModel.DataAnnotations;
|
|
using API.Schema.Contexts;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace API.Schema.Jobs;
|
|
|
|
public class RetrieveChaptersJob : Job
|
|
{
|
|
[StringLength(64)] [Required] public string MangaId { get; init; }
|
|
|
|
private Manga _manga = null!;
|
|
|
|
[JsonIgnore]
|
|
public Manga Manga
|
|
{
|
|
get => LazyLoader.Load(this, ref _manga);
|
|
init => _manga = value;
|
|
}
|
|
[StringLength(8)] [Required] public string Language { get; private set; }
|
|
|
|
public RetrieveChaptersJob(Manga manga, string language, ulong recurrenceMs, Job? parentJob = null, ICollection<Job>? dependsOnJobs = null)
|
|
: base(TokenGen.CreateToken(typeof(RetrieveChaptersJob)), JobType.RetrieveChaptersJob, recurrenceMs, parentJob, dependsOnJobs)
|
|
{
|
|
this.MangaId = manga.MangaId;
|
|
this.Manga = manga;
|
|
this.Language = language;
|
|
}
|
|
|
|
/// <summary>
|
|
/// EF ONLY!!!
|
|
/// </summary>
|
|
internal RetrieveChaptersJob(ILazyLoader lazyLoader, string mangaId, string language, ulong recurrenceMs, string? parentJobId)
|
|
: base(lazyLoader, TokenGen.CreateToken(typeof(RetrieveChaptersJob)), JobType.RetrieveChaptersJob, recurrenceMs, parentJobId)
|
|
{
|
|
this.MangaId = mangaId;
|
|
this.Language = language;
|
|
}
|
|
|
|
protected override IEnumerable<Job> RunInternal(PgsqlContext context)
|
|
{
|
|
context.Entry(Manga).Collection<Chapter>(m => m.Chapters).Load();
|
|
// This gets all chapters that are not downloaded
|
|
Chapter[] allChapters = Manga.MangaConnector.GetChapters(Manga, Language).DistinctBy(c => c.ChapterId).ToArray();
|
|
Chapter[] newChapters = allChapters.Where(chapter => Manga.Chapters.Select(c => c.ChapterId).Contains(chapter.ChapterId) == false).ToArray();
|
|
Log.Info($"{Manga.Chapters.Count} existing + {newChapters.Length} new chapters.");
|
|
|
|
try
|
|
{
|
|
foreach (Chapter newChapter in newChapters)
|
|
Manga.Chapters.Add(newChapter);
|
|
context.SaveChanges();
|
|
}
|
|
catch (DbUpdateException e)
|
|
{
|
|
Log.Error(e);
|
|
}
|
|
|
|
return [];
|
|
}
|
|
} |