[FA-misc] Initial MassTransit implementation seems to work
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
using FictionArchive.Service.NovelService.Services;
|
||||
using FictionArchive.Service.Shared.Contracts.Events;
|
||||
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;
|
||||
await _novelUpdateService.PullChapterContents(message.NovelId, message.VolumeId, message.ChapterOrder);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using FictionArchive.Common.Enums;
|
||||
using FictionArchive.Service.NovelService.Services;
|
||||
using FictionArchive.Service.Shared.Contracts.Events;
|
||||
using MassTransit;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace FictionArchive.Service.NovelService.Consumers;
|
||||
|
||||
public class FileUploadRequestStatusUpdateConsumer : IConsumer<IFileUploadRequestStatusUpdate>
|
||||
{
|
||||
private readonly ILogger<FileUploadRequestStatusUpdateConsumer> _logger;
|
||||
private readonly NovelServiceDbContext _dbContext;
|
||||
private readonly NovelUpdateService _novelUpdateService;
|
||||
|
||||
public FileUploadRequestStatusUpdateConsumer(
|
||||
ILogger<FileUploadRequestStatusUpdateConsumer> logger,
|
||||
NovelServiceDbContext dbContext,
|
||||
NovelUpdateService novelUpdateService)
|
||||
{
|
||||
_logger = logger;
|
||||
_dbContext = dbContext;
|
||||
_novelUpdateService = novelUpdateService;
|
||||
}
|
||||
|
||||
public async Task Consume(ConsumeContext<IFileUploadRequestStatusUpdate> context)
|
||||
{
|
||||
var message = context.Message;
|
||||
|
||||
var image = await _dbContext.Images.FindAsync(message.RequestId);
|
||||
if (image == null)
|
||||
{
|
||||
// Not a request we care about.
|
||||
return;
|
||||
}
|
||||
|
||||
if (message.Status == RequestStatus.Failed)
|
||||
{
|
||||
_logger.LogError("Image upload failed for image with id {imageId}", image.Id);
|
||||
return;
|
||||
}
|
||||
else if (message.Status == RequestStatus.Success)
|
||||
{
|
||||
_logger.LogInformation("Image upload succeeded for image with id {imageId}", image.Id);
|
||||
await _novelUpdateService.UpdateImage(image.Id, message.FileAccessUrl);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using FictionArchive.Service.NovelService.Services;
|
||||
using FictionArchive.Service.Shared.Contracts.Events;
|
||||
using MassTransit;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace FictionArchive.Service.NovelService.Consumers;
|
||||
|
||||
public class NovelUpdateRequestedConsumer : IConsumer<INovelUpdateRequested>
|
||||
{
|
||||
private readonly ILogger<NovelUpdateRequestedConsumer> _logger;
|
||||
private readonly NovelUpdateService _novelUpdateService;
|
||||
|
||||
public NovelUpdateRequestedConsumer(
|
||||
ILogger<NovelUpdateRequestedConsumer> logger,
|
||||
NovelUpdateService novelUpdateService)
|
||||
{
|
||||
_logger = logger;
|
||||
_novelUpdateService = novelUpdateService;
|
||||
}
|
||||
|
||||
public async Task Consume(ConsumeContext<INovelUpdateRequested> context)
|
||||
{
|
||||
var message = context.Message;
|
||||
await _novelUpdateService.ImportNovel(message.NovelUrl);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using FictionArchive.Service.NovelService.Models.Localization;
|
||||
using FictionArchive.Service.NovelService.Services;
|
||||
using FictionArchive.Service.Shared.Contracts.Events;
|
||||
using MassTransit;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace FictionArchive.Service.NovelService.Consumers;
|
||||
|
||||
public class TranslationRequestCompletedConsumer : IConsumer<ITranslationRequestCompleted>
|
||||
{
|
||||
private readonly ILogger<TranslationRequestCompletedConsumer> _logger;
|
||||
private readonly NovelServiceDbContext _dbContext;
|
||||
|
||||
public TranslationRequestCompletedConsumer(
|
||||
ILogger<TranslationRequestCompletedConsumer> logger,
|
||||
NovelServiceDbContext dbContext)
|
||||
{
|
||||
_logger = logger;
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
public async Task Consume(ConsumeContext<ITranslationRequestCompleted> context)
|
||||
{
|
||||
var message = context.Message;
|
||||
|
||||
var localizationRequest = await _dbContext.LocalizationRequests
|
||||
.Include(r => r.KeyRequestedForTranslation)
|
||||
.ThenInclude(lk => lk.Texts)
|
||||
.FirstOrDefaultAsync(lk => lk.Id == message.TranslationRequestId);
|
||||
|
||||
if (localizationRequest == null)
|
||||
{
|
||||
// Not one of our requests, discard it
|
||||
return;
|
||||
}
|
||||
|
||||
localizationRequest.KeyRequestedForTranslation.Texts.Add(new LocalizationText
|
||||
{
|
||||
Language = localizationRequest.TranslateTo,
|
||||
Text = message.TranslatedText,
|
||||
TranslationEngine = localizationRequest.Engine
|
||||
});
|
||||
|
||||
_dbContext.LocalizationRequests.Remove(localizationRequest);
|
||||
await _dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user