using System.ComponentModel.DataAnnotations; using API.Schema.Contexts; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Newtonsoft.Json; namespace API.Schema.Jobs; public class UpdateSingleChapterDownloadedJob : Job { [StringLength(64)] [Required] public string ChapterId { get; init; } private Chapter _chapter = null!; [JsonIgnore] public Chapter Chapter { get => LazyLoader.Load(this, ref _chapter); init => _chapter = value; } public UpdateSingleChapterDownloadedJob(Chapter chapter, Job? parentJob = null, ICollection? dependsOnJobs = null) : base(TokenGen.CreateToken(typeof(UpdateSingleChapterDownloadedJob)), JobType.UpdateSingleChapterDownloadedJob, 0, parentJob, dependsOnJobs) { this.ChapterId = chapter.ChapterId; this.Chapter = chapter; } /// /// EF ONLY!!! /// internal UpdateSingleChapterDownloadedJob(ILazyLoader lazyLoader, string chapterId, string? parentJobId) : base(lazyLoader, TokenGen.CreateToken(typeof(UpdateSingleChapterDownloadedJob)), JobType.UpdateSingleChapterDownloadedJob, 0, parentJobId) { this.ChapterId = chapterId; } protected override IEnumerable RunInternal(PgsqlContext context) { context.Attach(Chapter); Chapter.Downloaded = Chapter.CheckDownloaded(); try { context.SaveChanges(); } catch (DbUpdateException e) { Log.Error(e); } return []; } }