Files
FictionArchive/FictionArchive.Service.NovelService/Consumers/ChapterPullRequestedConsumer.cs
2026-02-01 10:19:52 -05:00

50 lines
1.7 KiB
C#

using FictionArchive.Common.Enums;
using FictionArchive.Service.NovelService.Services;
using FictionArchive.Service.Shared.Contracts.Events;
using FictionArchive.Service.Shared.Extensions;
using MassTransit;
using Microsoft.Extensions.Logging;
namespace FictionArchive.Service.NovelService.Consumers;
public class ChapterPullRequestedConsumer : IConsumer<IChapterPullRequested>
{
private readonly ILogger<ChapterPullRequestedConsumer> _logger;
private readonly NovelUpdateService _novelUpdateService;
public ChapterPullRequestedConsumer(
ILogger<ChapterPullRequestedConsumer> logger,
NovelUpdateService novelUpdateService)
{
_logger = logger;
_novelUpdateService = novelUpdateService;
}
public async Task Consume(ConsumeContext<IChapterPullRequested> context)
{
var message = context.Message;
var chapterJobId = Guid.NewGuid();
await context.ReportJobStatus(
chapterJobId, "ChapterPull", $"Pull chapter {message.ChapterOrder}",
JobStatus.InProgress, parentJobId: message.ImportId);
var (chapter, imageCount) = await _novelUpdateService.PullChapterContents(
message.ImportId,
message.NovelId,
message.VolumeId,
message.ChapterOrder);
await context.Publish<IChapterPullCompleted>(new ChapterPullCompleted(
message.ImportId,
chapter.Id,
imageCount
));
await context.ReportJobStatus(
chapterJobId, "ChapterPull", $"Pull chapter {message.ChapterOrder}",
JobStatus.Completed, parentJobId: message.ImportId,
metadata: new Dictionary<string, string> { ["ChapterId"] = chapter.Id.ToString() });
}
}