Compare commits
1 Commits
v1.4.0
...
feature/FA
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f88f340d0a |
@@ -0,0 +1,35 @@
|
|||||||
|
using FictionArchive.Service.AuthenticationService.Models.Requests;
|
||||||
|
using FictionArchive.Service.Shared.MassTransit.Contracts.Events;
|
||||||
|
using MassTransit;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace FictionArchive.Service.AuthenticationService.Controllers
|
||||||
|
{
|
||||||
|
[Route("api/[controller]")]
|
||||||
|
[ApiController]
|
||||||
|
public class AuthenticationWebhookController : ControllerBase
|
||||||
|
{
|
||||||
|
private readonly IPublishEndpoint _publishEndpoint;
|
||||||
|
|
||||||
|
public AuthenticationWebhookController(IPublishEndpoint publishEndpoint)
|
||||||
|
{
|
||||||
|
_publishEndpoint = publishEndpoint;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost(nameof(UserRegistered))]
|
||||||
|
public async Task<ActionResult> UserRegistered([FromBody] UserRegisteredWebhookPayload payload)
|
||||||
|
{
|
||||||
|
var authUserAddedEvent = new AuthUserAddedEvent
|
||||||
|
{
|
||||||
|
OAuthProviderId = payload.OAuthProviderId,
|
||||||
|
InviterOAuthProviderId = payload.InviterOAuthProviderId,
|
||||||
|
EventUserEmail = payload.EventUserEmail,
|
||||||
|
EventUserUsername = payload.EventUserUsername
|
||||||
|
};
|
||||||
|
|
||||||
|
await _publishEndpoint.Publish(authUserAddedEvent);
|
||||||
|
|
||||||
|
return Ok();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
23
FictionArchive.Service.AuthenticationService/Dockerfile
Normal file
23
FictionArchive.Service.AuthenticationService/Dockerfile
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
|
||||||
|
USER $APP_UID
|
||||||
|
WORKDIR /app
|
||||||
|
EXPOSE 8080
|
||||||
|
EXPOSE 8081
|
||||||
|
|
||||||
|
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
|
||||||
|
ARG BUILD_CONFIGURATION=Release
|
||||||
|
WORKDIR /src
|
||||||
|
COPY ["FictionArchive.Service.AuthenticationService/FictionArchive.Service.AuthenticationService.csproj", "FictionArchive.Service.AuthenticationService/"]
|
||||||
|
RUN dotnet restore "FictionArchive.Service.AuthenticationService/FictionArchive.Service.AuthenticationService.csproj"
|
||||||
|
COPY . .
|
||||||
|
WORKDIR "/src/FictionArchive.Service.AuthenticationService"
|
||||||
|
RUN dotnet build "./FictionArchive.Service.AuthenticationService.csproj" -c $BUILD_CONFIGURATION -o /app/build
|
||||||
|
|
||||||
|
FROM build AS publish
|
||||||
|
ARG BUILD_CONFIGURATION=Release
|
||||||
|
RUN dotnet publish "./FictionArchive.Service.AuthenticationService.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
|
||||||
|
|
||||||
|
FROM base AS final
|
||||||
|
WORKDIR /app
|
||||||
|
COPY --from=publish /app/publish .
|
||||||
|
ENTRYPOINT ["dotnet", "FictionArchive.Service.AuthenticationService.dll"]
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="8.0.7" />
|
||||||
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2"/>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Content Include="..\.dockerignore">
|
||||||
|
<Link>.dockerignore</Link>
|
||||||
|
</Content>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\FictionArchive.Service.Shared\FictionArchive.Service.Shared.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Folder Include="Controllers\" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
@FictionArchive.Service.AuthenticationService_HostAddress = http://localhost:5091
|
||||||
|
|
||||||
|
GET {{FictionArchive.Service.AuthenticationService_HostAddress}}/weatherforecast/
|
||||||
|
Accept: application/json
|
||||||
|
|
||||||
|
###
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
namespace FictionArchive.Service.AuthenticationService.Models.Requests;
|
||||||
|
|
||||||
|
public class UserRegisteredWebhookPayload
|
||||||
|
{
|
||||||
|
// The body of the notification message
|
||||||
|
public string Body { get; set; }
|
||||||
|
|
||||||
|
public string OAuthProviderId { get; set; }
|
||||||
|
|
||||||
|
public string InviterOAuthProviderId { get; set; }
|
||||||
|
|
||||||
|
// The email of the user that created the event
|
||||||
|
public string EventUserEmail { get; set; }
|
||||||
|
|
||||||
|
// The username of the user that created the event
|
||||||
|
public string EventUserUsername { get; set; }
|
||||||
|
}
|
||||||
45
FictionArchive.Service.AuthenticationService/Program.cs
Normal file
45
FictionArchive.Service.AuthenticationService/Program.cs
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
using FictionArchive.Service.Shared.MassTransit;
|
||||||
|
|
||||||
|
namespace FictionArchive.Service.AuthenticationService;
|
||||||
|
|
||||||
|
public class Program
|
||||||
|
{
|
||||||
|
public static void Main(string[] args)
|
||||||
|
{
|
||||||
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
|
// Add services to the container.
|
||||||
|
|
||||||
|
builder.Services.AddControllers();
|
||||||
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||||
|
builder.Services.AddEndpointsApiExplorer();
|
||||||
|
builder.Services.AddSwaggerGen();
|
||||||
|
|
||||||
|
#region MassTransit
|
||||||
|
|
||||||
|
builder.Services.AddFictionArchiveMassTransit(builder.Configuration);
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
builder.Services.AddHealthChecks();
|
||||||
|
|
||||||
|
var app = builder.Build();
|
||||||
|
|
||||||
|
// Configure the HTTP request pipeline.
|
||||||
|
if (app.Environment.IsDevelopment())
|
||||||
|
{
|
||||||
|
app.UseSwagger();
|
||||||
|
app.UseSwaggerUI();
|
||||||
|
}
|
||||||
|
|
||||||
|
app.UseHttpsRedirection();
|
||||||
|
|
||||||
|
app.MapHealthChecks("/healthz");
|
||||||
|
|
||||||
|
app.UseAuthorization();
|
||||||
|
|
||||||
|
app.MapControllers();
|
||||||
|
|
||||||
|
app.Run();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
{
|
||||||
|
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||||
|
"iisSettings": {
|
||||||
|
"windowsAuthentication": false,
|
||||||
|
"anonymousAuthentication": true,
|
||||||
|
"iisExpress": {
|
||||||
|
"applicationUrl": "http://localhost:23522",
|
||||||
|
"sslPort": 44397
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"profiles": {
|
||||||
|
"http": {
|
||||||
|
"commandName": "Project",
|
||||||
|
"dotnetRunMessages": true,
|
||||||
|
"launchBrowser": true,
|
||||||
|
"launchUrl": "swagger",
|
||||||
|
"applicationUrl": "http://localhost:5091",
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"https": {
|
||||||
|
"commandName": "Project",
|
||||||
|
"dotnetRunMessages": true,
|
||||||
|
"launchBrowser": true,
|
||||||
|
"launchUrl": "swagger",
|
||||||
|
"applicationUrl": "https://localhost:7223;http://localhost:5091",
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"IIS Express": {
|
||||||
|
"commandName": "IISExpress",
|
||||||
|
"launchBrowser": true,
|
||||||
|
"launchUrl": "swagger",
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft.AspNetCore": "Warning"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft.AspNetCore": "Warning"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"RabbitMQ": {
|
||||||
|
"Host": "localhost",
|
||||||
|
"VirtualHost": "/",
|
||||||
|
"Username": "guest",
|
||||||
|
"Password": "guest"
|
||||||
|
},
|
||||||
|
"AllowedHosts": "*"
|
||||||
|
}
|
||||||
@@ -1,75 +0,0 @@
|
|||||||
using Amazon.S3;
|
|
||||||
using Amazon.S3.Model;
|
|
||||||
using FictionArchive.Common.Enums;
|
|
||||||
using FictionArchive.Service.FileService.Models;
|
|
||||||
using FictionArchive.Service.Shared.Contracts.Events;
|
|
||||||
using MassTransit;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using Microsoft.Extensions.Options;
|
|
||||||
|
|
||||||
namespace FictionArchive.Service.FileService.Consumers;
|
|
||||||
|
|
||||||
public class FileUploadRequestCreatedConsumer : IConsumer<IFileUploadRequestCreated>
|
|
||||||
{
|
|
||||||
private readonly ILogger<FileUploadRequestCreatedConsumer> _logger;
|
|
||||||
private readonly AmazonS3Client _amazonS3Client;
|
|
||||||
private readonly IPublishEndpoint _publishEndpoint;
|
|
||||||
private readonly S3Configuration _s3Configuration;
|
|
||||||
private readonly ProxyConfiguration _proxyConfiguration;
|
|
||||||
|
|
||||||
public FileUploadRequestCreatedConsumer(
|
|
||||||
ILogger<FileUploadRequestCreatedConsumer> logger,
|
|
||||||
AmazonS3Client amazonS3Client,
|
|
||||||
IPublishEndpoint publishEndpoint,
|
|
||||||
IOptions<S3Configuration> s3Configuration,
|
|
||||||
IOptions<ProxyConfiguration> proxyConfiguration)
|
|
||||||
{
|
|
||||||
_logger = logger;
|
|
||||||
_amazonS3Client = amazonS3Client;
|
|
||||||
_publishEndpoint = publishEndpoint;
|
|
||||||
_s3Configuration = s3Configuration.Value;
|
|
||||||
_proxyConfiguration = proxyConfiguration.Value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task Consume(ConsumeContext<IFileUploadRequestCreated> context)
|
|
||||||
{
|
|
||||||
var message = context.Message;
|
|
||||||
|
|
||||||
var putObjectRequest = new PutObjectRequest
|
|
||||||
{
|
|
||||||
BucketName = _s3Configuration.Bucket,
|
|
||||||
Key = message.FilePath
|
|
||||||
};
|
|
||||||
|
|
||||||
using var memoryStream = new MemoryStream(message.FileData);
|
|
||||||
putObjectRequest.InputStream = memoryStream;
|
|
||||||
|
|
||||||
var s3Response = await _amazonS3Client.PutObjectAsync(putObjectRequest);
|
|
||||||
|
|
||||||
if (s3Response.HttpStatusCode != System.Net.HttpStatusCode.OK)
|
|
||||||
{
|
|
||||||
_logger.LogError("Failed to upload file {FilePath} to S3", message.FilePath);
|
|
||||||
|
|
||||||
await _publishEndpoint.Publish<IFileUploadRequestStatusUpdate>(
|
|
||||||
new FileUploadRequestStatusUpdate(
|
|
||||||
ImportId: message.ImportId,
|
|
||||||
RequestId: message.RequestId,
|
|
||||||
Status: RequestStatus.Failed,
|
|
||||||
FileAccessUrl: null,
|
|
||||||
ErrorMessage: "An error occurred while uploading file to S3."));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var fileAccessUrl = _proxyConfiguration.BaseUrl + "/" + message.FilePath;
|
|
||||||
|
|
||||||
_logger.LogInformation("Successfully uploaded file {FilePath} to S3", message.FilePath);
|
|
||||||
|
|
||||||
await _publishEndpoint.Publish<IFileUploadRequestStatusUpdate>(
|
|
||||||
new FileUploadRequestStatusUpdate(
|
|
||||||
ImportId: message.ImportId,
|
|
||||||
RequestId: message.RequestId,
|
|
||||||
Status: RequestStatus.Success,
|
|
||||||
FileAccessUrl: fileAccessUrl,
|
|
||||||
ErrorMessage: null));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,9 +1,10 @@
|
|||||||
using Amazon.Runtime;
|
using Amazon.Runtime;
|
||||||
using Amazon.S3;
|
using Amazon.S3;
|
||||||
using FictionArchive.Common.Extensions;
|
using FictionArchive.Common.Extensions;
|
||||||
using FictionArchive.Service.FileService.Consumers;
|
|
||||||
using FictionArchive.Service.FileService.Models;
|
using FictionArchive.Service.FileService.Models;
|
||||||
|
using FictionArchive.Service.FileService.Services.EventHandlers;
|
||||||
using FictionArchive.Service.Shared.Extensions;
|
using FictionArchive.Service.Shared.Extensions;
|
||||||
|
using FictionArchive.Service.Shared.MassTransit;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
namespace FictionArchive.Service.FileService;
|
namespace FictionArchive.Service.FileService;
|
||||||
@@ -28,7 +29,7 @@ public class Program
|
|||||||
builder.Configuration,
|
builder.Configuration,
|
||||||
x =>
|
x =>
|
||||||
{
|
{
|
||||||
x.AddConsumer<FileUploadRequestCreatedConsumer>();
|
x.AddConsumer<UploadFileCommandConsumer>();
|
||||||
});
|
});
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|||||||
@@ -0,0 +1,63 @@
|
|||||||
|
using Amazon.S3;
|
||||||
|
using Amazon.S3.Model;
|
||||||
|
using FictionArchive.Common.Enums;
|
||||||
|
using FictionArchive.Service.FileService.Models;
|
||||||
|
using FictionArchive.Service.Shared.MassTransit.Contracts.Commands;
|
||||||
|
using FictionArchive.Service.Shared.MassTransit.Contracts.Events;
|
||||||
|
using MassTransit;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
|
namespace FictionArchive.Service.FileService.Services.EventHandlers;
|
||||||
|
|
||||||
|
public class UploadFileCommandConsumer : IConsumer<UploadFileCommand>
|
||||||
|
{
|
||||||
|
private readonly ILogger<UploadFileCommandConsumer> _logger;
|
||||||
|
private readonly AmazonS3Client _amazonS3Client;
|
||||||
|
private readonly S3Configuration _s3Configuration;
|
||||||
|
private readonly ProxyConfiguration _proxyConfiguration;
|
||||||
|
|
||||||
|
public UploadFileCommandConsumer(
|
||||||
|
ILogger<UploadFileCommandConsumer> logger,
|
||||||
|
AmazonS3Client amazonS3Client,
|
||||||
|
IOptions<S3Configuration> s3Configuration,
|
||||||
|
IOptions<ProxyConfiguration> proxyConfiguration)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
_amazonS3Client = amazonS3Client;
|
||||||
|
_proxyConfiguration = proxyConfiguration.Value;
|
||||||
|
_s3Configuration = s3Configuration.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Consume(ConsumeContext<UploadFileCommand> context)
|
||||||
|
{
|
||||||
|
var command = context.Message;
|
||||||
|
|
||||||
|
var putObjectRequest = new PutObjectRequest();
|
||||||
|
putObjectRequest.BucketName = _s3Configuration.Bucket;
|
||||||
|
putObjectRequest.Key = command.FilePath;
|
||||||
|
putObjectRequest.UseChunkEncoding = false; // Needed to avoid an error with Garage
|
||||||
|
|
||||||
|
using MemoryStream memoryStream = new MemoryStream(command.FileData);
|
||||||
|
putObjectRequest.InputStream = memoryStream;
|
||||||
|
|
||||||
|
var s3Response = await _amazonS3Client.PutObjectAsync(putObjectRequest);
|
||||||
|
if (s3Response.HttpStatusCode != System.Net.HttpStatusCode.OK)
|
||||||
|
{
|
||||||
|
_logger.LogError("An error occurred while uploading file to S3. Response code: {responsecode}", s3Response.HttpStatusCode);
|
||||||
|
await context.Publish(new FileUploadCompletedEvent
|
||||||
|
{
|
||||||
|
RequestId = command.RequestId,
|
||||||
|
Status = RequestStatus.Failed,
|
||||||
|
ErrorMessage = "An error occurred while uploading file to S3."
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await context.Publish(new FileUploadCompletedEvent
|
||||||
|
{
|
||||||
|
Status = RequestStatus.Success,
|
||||||
|
RequestId = command.RequestId,
|
||||||
|
FileAccessUrl = _proxyConfiguration.BaseUrl + "/" + command.FilePath
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,16 +2,17 @@
|
|||||||
"Logging": {
|
"Logging": {
|
||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
"Default": "Information",
|
"Default": "Information",
|
||||||
"Microsoft.AspNetCore": "Warning",
|
"Microsoft.AspNetCore": "Warning"
|
||||||
"Microsoft.EntityFrameworkCore": "Warning"
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"ProxyConfiguration": {
|
"ProxyConfiguration": {
|
||||||
"BaseUrl": "https://localhost:7247/api"
|
"BaseUrl": "https://localhost:7247/api"
|
||||||
},
|
},
|
||||||
"RabbitMQ": {
|
"RabbitMQ": {
|
||||||
"ConnectionString": "amqp://localhost",
|
"Host": "localhost",
|
||||||
"ClientIdentifier": "FileService"
|
"VirtualHost": "/",
|
||||||
|
"Username": "guest",
|
||||||
|
"Password": "guest"
|
||||||
},
|
},
|
||||||
"S3": {
|
"S3": {
|
||||||
"Url": "https://s3.orfl.xyz",
|
"Url": "https://s3.orfl.xyz",
|
||||||
|
|||||||
@@ -9,10 +9,8 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="FluentAssertions" Version="6.12.0" />
|
<PackageReference Include="FluentAssertions" Version="6.12.0" />
|
||||||
<PackageReference Include="MassTransit" Version="8.5.7" />
|
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.11" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.11" />
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
|
||||||
<PackageReference Include="NodaTime.Testing" Version="3.3.0" />
|
|
||||||
<PackageReference Include="NSubstitute" Version="5.1.0" />
|
<PackageReference Include="NSubstitute" Version="5.1.0" />
|
||||||
<PackageReference Include="xunit" Version="2.9.2" />
|
<PackageReference Include="xunit" Version="2.9.2" />
|
||||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
|
||||||
|
|||||||
@@ -7,14 +7,13 @@ using FictionArchive.Service.NovelService.Models.Novels;
|
|||||||
using FictionArchive.Service.NovelService.Models.SourceAdapters;
|
using FictionArchive.Service.NovelService.Models.SourceAdapters;
|
||||||
using FictionArchive.Service.NovelService.Services;
|
using FictionArchive.Service.NovelService.Services;
|
||||||
using FictionArchive.Service.NovelService.Services.SourceAdapters;
|
using FictionArchive.Service.NovelService.Services.SourceAdapters;
|
||||||
using FictionArchive.Service.Shared.Contracts.Events;
|
using FictionArchive.Service.Shared.MassTransit.Contracts.Commands;
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
using HtmlAgilityPack;
|
|
||||||
using MassTransit;
|
using MassTransit;
|
||||||
|
using HtmlAgilityPack;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Logging.Abstractions;
|
using Microsoft.Extensions.Logging.Abstractions;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using NodaTime;
|
|
||||||
using NSubstitute;
|
using NSubstitute;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
@@ -74,6 +73,7 @@ public class NovelUpdateServiceTests
|
|||||||
NovelServiceDbContext dbContext,
|
NovelServiceDbContext dbContext,
|
||||||
ISourceAdapter adapter,
|
ISourceAdapter adapter,
|
||||||
IPublishEndpoint publishEndpoint,
|
IPublishEndpoint publishEndpoint,
|
||||||
|
ISendEndpointProvider sendEndpointProvider,
|
||||||
string pendingImageUrl = "https://pending/placeholder.jpg")
|
string pendingImageUrl = "https://pending/placeholder.jpg")
|
||||||
{
|
{
|
||||||
var options = Options.Create(new NovelUpdateServiceConfiguration
|
var options = Options.Create(new NovelUpdateServiceConfiguration
|
||||||
@@ -81,10 +81,7 @@ public class NovelUpdateServiceTests
|
|||||||
PendingImageUrl = pendingImageUrl
|
PendingImageUrl = pendingImageUrl
|
||||||
});
|
});
|
||||||
|
|
||||||
var clock = Substitute.For<IClock>();
|
return new NovelUpdateService(dbContext, NullLogger<NovelUpdateService>.Instance, new[] { adapter }, publishEndpoint, sendEndpointProvider, options);
|
||||||
clock.GetCurrentInstant().Returns(Instant.FromUnixTimeSeconds(0));
|
|
||||||
|
|
||||||
return new NovelUpdateService(dbContext, NullLogger<NovelUpdateService>.Instance, new[] { adapter }, publishEndpoint, options, clock);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -106,18 +103,18 @@ public class NovelUpdateServiceTests
|
|||||||
ImageData = new List<ImageData> { image1, image2 }
|
ImageData = new List<ImageData> { image1, image2 }
|
||||||
}));
|
}));
|
||||||
|
|
||||||
var publishedEvents = new List<IFileUploadRequestCreated>();
|
var publishedCommands = new List<UploadFileCommand>();
|
||||||
var publishEndpoint = Substitute.For<IPublishEndpoint>();
|
var publishEndpoint = Substitute.For<IPublishEndpoint>();
|
||||||
publishEndpoint.Publish(Arg.Do<IFileUploadRequestCreated>(e => publishedEvents.Add(e)), Arg.Any<CancellationToken>())
|
var sendEndpointProvider = Substitute.For<ISendEndpointProvider>();
|
||||||
.Returns(Task.CompletedTask);
|
var sendEndpoint = Substitute.For<ISendEndpoint>();
|
||||||
|
sendEndpointProvider.GetSendEndpoint(Arg.Any<Uri>()).Returns(Task.FromResult(sendEndpoint));
|
||||||
|
sendEndpoint.Send(Arg.Do<UploadFileCommand>(publishedCommands.Add), Arg.Any<CancellationToken>()).Returns(Task.CompletedTask);
|
||||||
|
|
||||||
var pendingImageUrl = "https://pending/placeholder.jpg";
|
var pendingImageUrl = "https://pending/placeholder.jpg";
|
||||||
var service = CreateService(dbContext, adapter, publishEndpoint, pendingImageUrl);
|
var service = CreateService(dbContext, adapter, publishEndpoint, sendEndpointProvider, pendingImageUrl);
|
||||||
|
|
||||||
var importId = Guid.NewGuid();
|
var updatedChapter = await service.PullChapterContents(novel.Id, volume.Id, chapter.Order);
|
||||||
var (updatedChapter, imageCount) = await service.PullChapterContents(importId, novel.Id, volume.Id, chapter.Order);
|
|
||||||
|
|
||||||
imageCount.Should().Be(2);
|
|
||||||
updatedChapter.Images.Should().HaveCount(2);
|
updatedChapter.Images.Should().HaveCount(2);
|
||||||
updatedChapter.Images.Select(i => i.OriginalPath).Should().BeEquivalentTo(new[] { image1.Url, image2.Url });
|
updatedChapter.Images.Select(i => i.OriginalPath).Should().BeEquivalentTo(new[] { image1.Url, image2.Url });
|
||||||
updatedChapter.Images.All(i => i.Id != Guid.Empty).Should().BeTrue();
|
updatedChapter.Images.All(i => i.Id != Guid.Empty).Should().BeTrue();
|
||||||
@@ -133,11 +130,10 @@ public class NovelUpdateServiceTests
|
|||||||
.Should()
|
.Should()
|
||||||
.BeEquivalentTo(updatedChapter.Images.Select(img => img.Id.ToString()));
|
.BeEquivalentTo(updatedChapter.Images.Select(img => img.Id.ToString()));
|
||||||
|
|
||||||
publishedEvents.Should().HaveCount(2);
|
publishedCommands.Should().HaveCount(2);
|
||||||
publishedEvents.Should().OnlyContain(e => e.ImportId == importId);
|
publishedCommands.Select(e => e.RequestId).Should().BeEquivalentTo(updatedChapter.Images.Select(i => i.Id));
|
||||||
publishedEvents.Select(e => e.RequestId).Should().BeEquivalentTo(updatedChapter.Images.Select(i => i.Id));
|
publishedCommands.Select(e => e.FileData).Should().BeEquivalentTo(new[] { image1.Data, image2.Data });
|
||||||
publishedEvents.Select(e => e.FileData).Should().BeEquivalentTo(new[] { image1.Data, image2.Data });
|
publishedCommands.Should().OnlyContain(e => e.FilePath.StartsWith($"{novel.Id}/Images/Chapter-{updatedChapter.Id}/"));
|
||||||
publishedEvents.Should().OnlyContain(e => e.FilePath.StartsWith($"Novels/{novel.Id}/Images/Chapter-{updatedChapter.Id}/"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -159,13 +155,14 @@ public class NovelUpdateServiceTests
|
|||||||
}));
|
}));
|
||||||
|
|
||||||
var publishEndpoint = Substitute.For<IPublishEndpoint>();
|
var publishEndpoint = Substitute.For<IPublishEndpoint>();
|
||||||
|
var sendEndpointProvider = Substitute.For<ISendEndpointProvider>();
|
||||||
|
var sendEndpoint = Substitute.For<ISendEndpoint>();
|
||||||
|
sendEndpointProvider.GetSendEndpoint(Arg.Any<Uri>()).Returns(Task.FromResult(sendEndpoint));
|
||||||
|
|
||||||
var service = CreateService(dbContext, adapter, publishEndpoint);
|
var service = CreateService(dbContext, adapter, publishEndpoint, sendEndpointProvider);
|
||||||
|
|
||||||
var importId = Guid.NewGuid();
|
var updatedChapter = await service.PullChapterContents(novel.Id, volume.Id, chapter.Order);
|
||||||
var (updatedChapter, imageCount) = await service.PullChapterContents(importId, novel.Id, volume.Id, chapter.Order);
|
|
||||||
|
|
||||||
imageCount.Should().Be(1);
|
|
||||||
var storedHtml = updatedChapter.Body.Texts.Single().Text;
|
var storedHtml = updatedChapter.Body.Texts.Single().Text;
|
||||||
var doc = new HtmlDocument();
|
var doc = new HtmlDocument();
|
||||||
doc.LoadHtml(storedHtml);
|
doc.LoadHtml(storedHtml);
|
||||||
@@ -194,7 +191,8 @@ public class NovelUpdateServiceTests
|
|||||||
|
|
||||||
var adapter = Substitute.For<ISourceAdapter>();
|
var adapter = Substitute.For<ISourceAdapter>();
|
||||||
var publishEndpoint = Substitute.For<IPublishEndpoint>();
|
var publishEndpoint = Substitute.For<IPublishEndpoint>();
|
||||||
var service = CreateService(dbContext, adapter, publishEndpoint);
|
var sendEndpointProvider = Substitute.For<ISendEndpointProvider>();
|
||||||
|
var service = CreateService(dbContext, adapter, publishEndpoint, sendEndpointProvider);
|
||||||
|
|
||||||
var newUrl = "https://cdn.example.com/uploaded/cover.jpg";
|
var newUrl = "https://cdn.example.com/uploaded/cover.jpg";
|
||||||
|
|
||||||
@@ -236,7 +234,8 @@ public class NovelUpdateServiceTests
|
|||||||
|
|
||||||
var adapter = Substitute.For<ISourceAdapter>();
|
var adapter = Substitute.For<ISourceAdapter>();
|
||||||
var publishEndpoint = Substitute.For<IPublishEndpoint>();
|
var publishEndpoint = Substitute.For<IPublishEndpoint>();
|
||||||
var service = CreateService(dbContext, adapter, publishEndpoint, pendingUrl);
|
var sendEndpointProvider = Substitute.For<ISendEndpointProvider>();
|
||||||
|
var service = CreateService(dbContext, adapter, publishEndpoint, sendEndpointProvider, pendingUrl);
|
||||||
|
|
||||||
var newUrl = "https://cdn.example.com/uploaded/image.jpg";
|
var newUrl = "https://cdn.example.com/uploaded/image.jpg";
|
||||||
|
|
||||||
@@ -285,7 +284,8 @@ public class NovelUpdateServiceTests
|
|||||||
|
|
||||||
var adapter = Substitute.For<ISourceAdapter>();
|
var adapter = Substitute.For<ISourceAdapter>();
|
||||||
var publishEndpoint = Substitute.For<IPublishEndpoint>();
|
var publishEndpoint = Substitute.For<IPublishEndpoint>();
|
||||||
var service = CreateService(dbContext, adapter, publishEndpoint, pendingUrl);
|
var sendEndpointProvider = Substitute.For<ISendEndpointProvider>();
|
||||||
|
var service = CreateService(dbContext, adapter, publishEndpoint, sendEndpointProvider, pendingUrl);
|
||||||
|
|
||||||
var newUrl = "https://cdn.example.com/uploaded/img1.jpg";
|
var newUrl = "https://cdn.example.com/uploaded/img1.jpg";
|
||||||
|
|
||||||
|
|||||||
@@ -1,95 +0,0 @@
|
|||||||
using FictionArchive.Common.Enums;
|
|
||||||
using FictionArchive.Service.NovelService.Sagas;
|
|
||||||
using FictionArchive.Service.Shared.Contracts.Events;
|
|
||||||
using FluentAssertions;
|
|
||||||
using MassTransit;
|
|
||||||
using MassTransit.Testing;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using NodaTime;
|
|
||||||
using NodaTime.Testing;
|
|
||||||
using Xunit;
|
|
||||||
|
|
||||||
namespace FictionArchive.Service.NovelService.Tests.Sagas;
|
|
||||||
|
|
||||||
public class NovelImportSagaTests
|
|
||||||
{
|
|
||||||
private readonly FakeClock _clock = new(Instant.FromUtc(2026, 1, 27, 12, 0, 0));
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task Should_transition_to_importing_on_import_requested()
|
|
||||||
{
|
|
||||||
await using var provider = CreateTestProvider();
|
|
||||||
var harness = provider.GetRequiredService<ITestHarness>();
|
|
||||||
await harness.Start();
|
|
||||||
|
|
||||||
var importId = Guid.NewGuid();
|
|
||||||
await harness.Bus.Publish<INovelImportRequested>(new NovelImportRequested(importId, "https://example.com/novel"));
|
|
||||||
|
|
||||||
var sagaHarness = harness.GetSagaStateMachineHarness<NovelImportSaga, NovelImportSagaState>();
|
|
||||||
(await sagaHarness.Exists(importId, x => x.Importing)).HasValue.Should().BeTrue();
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task Should_transition_to_completed_when_no_chapters()
|
|
||||||
{
|
|
||||||
await using var provider = CreateTestProvider();
|
|
||||||
var harness = provider.GetRequiredService<ITestHarness>();
|
|
||||||
await harness.Start();
|
|
||||||
|
|
||||||
var importId = Guid.NewGuid();
|
|
||||||
await harness.Bus.Publish<INovelImportRequested>(new NovelImportRequested(importId, "https://example.com/novel"));
|
|
||||||
await harness.Bus.Publish<INovelMetadataImported>(new NovelMetadataImported(importId, 1, 0));
|
|
||||||
|
|
||||||
var sagaHarness = harness.GetSagaStateMachineHarness<NovelImportSaga, NovelImportSagaState>();
|
|
||||||
(await sagaHarness.Exists(importId, x => x.Completed)).HasValue.Should().BeTrue();
|
|
||||||
|
|
||||||
(await harness.Published.Any<INovelImportCompleted>(x =>
|
|
||||||
x.Context.Message.ImportId == importId && x.Context.Message.Success)).Should().BeTrue();
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task Should_transition_to_processing_when_chapters_pending()
|
|
||||||
{
|
|
||||||
await using var provider = CreateTestProvider();
|
|
||||||
var harness = provider.GetRequiredService<ITestHarness>();
|
|
||||||
await harness.Start();
|
|
||||||
|
|
||||||
var importId = Guid.NewGuid();
|
|
||||||
await harness.Bus.Publish<INovelImportRequested>(new NovelImportRequested(importId, "https://example.com/novel"));
|
|
||||||
await harness.Bus.Publish<INovelMetadataImported>(new NovelMetadataImported(importId, 1, 2));
|
|
||||||
|
|
||||||
var sagaHarness = harness.GetSagaStateMachineHarness<NovelImportSaga, NovelImportSagaState>();
|
|
||||||
(await sagaHarness.Exists(importId, x => x.Processing)).HasValue.Should().BeTrue();
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task Should_complete_when_all_chapters_pulled_and_images_uploaded()
|
|
||||||
{
|
|
||||||
await using var provider = CreateTestProvider();
|
|
||||||
var harness = provider.GetRequiredService<ITestHarness>();
|
|
||||||
await harness.Start();
|
|
||||||
|
|
||||||
var importId = Guid.NewGuid();
|
|
||||||
await harness.Bus.Publish<INovelImportRequested>(new NovelImportRequested(importId, "https://example.com/novel"));
|
|
||||||
await harness.Bus.Publish<INovelMetadataImported>(new NovelMetadataImported(importId, 1, 2));
|
|
||||||
await harness.Bus.Publish<IChapterPullCompleted>(new ChapterPullCompleted(importId, 1, 1));
|
|
||||||
await harness.Bus.Publish<IChapterPullCompleted>(new ChapterPullCompleted(importId, 2, 0));
|
|
||||||
await harness.Bus.Publish<IFileUploadRequestStatusUpdate>(new FileUploadRequestStatusUpdate(
|
|
||||||
importId, Guid.NewGuid(), RequestStatus.Success, "https://cdn.example.com/image.jpg", null));
|
|
||||||
|
|
||||||
var sagaHarness = harness.GetSagaStateMachineHarness<NovelImportSaga, NovelImportSagaState>();
|
|
||||||
(await sagaHarness.Exists(importId, x => x.Completed)).HasValue.Should().BeTrue();
|
|
||||||
}
|
|
||||||
|
|
||||||
private ServiceProvider CreateTestProvider()
|
|
||||||
{
|
|
||||||
return new ServiceCollection()
|
|
||||||
.AddSingleton<IClock>(_clock)
|
|
||||||
.AddMassTransitTestHarness(cfg =>
|
|
||||||
{
|
|
||||||
cfg.AddSagaStateMachine<NovelImportSaga, NovelImportSagaState>()
|
|
||||||
.InMemoryRepository();
|
|
||||||
})
|
|
||||||
.BuildServiceProvider(true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
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;
|
|
||||||
|
|
||||||
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
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,47 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
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 NovelImportCompletedConsumer : IConsumer<INovelImportCompleted>
|
|
||||||
{
|
|
||||||
private readonly ILogger<NovelImportCompletedConsumer> _logger;
|
|
||||||
private readonly NovelServiceDbContext _dbContext;
|
|
||||||
|
|
||||||
public NovelImportCompletedConsumer(
|
|
||||||
ILogger<NovelImportCompletedConsumer> logger,
|
|
||||||
NovelServiceDbContext dbContext)
|
|
||||||
{
|
|
||||||
_logger = logger;
|
|
||||||
_dbContext = dbContext;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task Consume(ConsumeContext<INovelImportCompleted> context)
|
|
||||||
{
|
|
||||||
var message = context.Message;
|
|
||||||
|
|
||||||
_logger.LogInformation(
|
|
||||||
"Novel import {ImportId} completed. Success: {Success}, NovelId: {NovelId}, Error: {Error}",
|
|
||||||
message.ImportId,
|
|
||||||
message.Success,
|
|
||||||
message.NovelId,
|
|
||||||
message.ErrorMessage);
|
|
||||||
|
|
||||||
// Remove from ActiveImports to allow future imports
|
|
||||||
var activeImport = await _dbContext.ActiveImports
|
|
||||||
.FirstOrDefaultAsync(a => a.ImportId == message.ImportId);
|
|
||||||
|
|
||||||
if (activeImport != null)
|
|
||||||
{
|
|
||||||
_dbContext.ActiveImports.Remove(activeImport);
|
|
||||||
await _dbContext.SaveChangesAsync();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
using FictionArchive.Service.NovelService.Services;
|
|
||||||
using FictionArchive.Service.Shared.Contracts.Events;
|
|
||||||
using MassTransit;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
|
|
||||||
namespace FictionArchive.Service.NovelService.Consumers;
|
|
||||||
|
|
||||||
public class NovelImportRequestedConsumer : IConsumer<INovelImportRequested>
|
|
||||||
{
|
|
||||||
private readonly ILogger<NovelImportRequestedConsumer> _logger;
|
|
||||||
private readonly NovelUpdateService _novelUpdateService;
|
|
||||||
|
|
||||||
public NovelImportRequestedConsumer(
|
|
||||||
ILogger<NovelImportRequestedConsumer> logger,
|
|
||||||
NovelUpdateService novelUpdateService)
|
|
||||||
{
|
|
||||||
_logger = logger;
|
|
||||||
_novelUpdateService = novelUpdateService;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task Consume(ConsumeContext<INovelImportRequested> context)
|
|
||||||
{
|
|
||||||
var message = context.Message;
|
|
||||||
_logger.LogInformation("Starting novel import for {NovelUrl} with ImportId {ImportId}",
|
|
||||||
message.NovelUrl, message.ImportId);
|
|
||||||
|
|
||||||
await _novelUpdateService.ImportNovel(message.ImportId, message.NovelUrl);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,48 +0,0 @@
|
|||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
using FictionArchive.Service.Shared.Contracts.Events;
|
|
||||||
|
|
||||||
namespace FictionArchive.Service.NovelService.Contracts;
|
|
||||||
|
|
||||||
public record ChapterCreated(
|
|
||||||
uint ChapterId,
|
|
||||||
uint NovelId,
|
|
||||||
uint VolumeId,
|
|
||||||
uint VolumeOrder,
|
|
||||||
uint ChapterOrder,
|
|
||||||
string ChapterTitle) : IChapterCreated;
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
using FictionArchive.Common.Enums;
|
|
||||||
using FictionArchive.Service.Shared.Contracts.Events;
|
|
||||||
|
|
||||||
namespace FictionArchive.Service.NovelService.Contracts;
|
|
||||||
|
|
||||||
public record NovelCreated(
|
|
||||||
uint NovelId,
|
|
||||||
string Title,
|
|
||||||
Language OriginalLanguage,
|
|
||||||
string Source,
|
|
||||||
string AuthorName) : INovelCreated;
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
using FictionArchive.Common.Enums;
|
|
||||||
using FictionArchive.Service.Shared.Contracts.Events;
|
|
||||||
|
|
||||||
namespace FictionArchive.Service.NovelService.Contracts;
|
|
||||||
|
|
||||||
public record TranslationRequestCreated(
|
|
||||||
Guid TranslationRequestId,
|
|
||||||
Language From,
|
|
||||||
Language To,
|
|
||||||
string Body,
|
|
||||||
string TranslationEngineKey) : ITranslationRequestCreated;
|
|
||||||
@@ -10,7 +10,6 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="HotChocolate.AspNetCore.CommandLine" Version="15.1.11" />
|
<PackageReference Include="HotChocolate.AspNetCore.CommandLine" Version="15.1.11" />
|
||||||
<PackageReference Include="HtmlAgilityPack" Version="1.12.4" />
|
<PackageReference Include="HtmlAgilityPack" Version="1.12.4" />
|
||||||
<PackageReference Include="MassTransit.EntityFrameworkCore" Version="8.5.7" />
|
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.11">
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.11">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
|||||||
@@ -1,35 +1,26 @@
|
|||||||
using FictionArchive.Service.NovelService.Contracts;
|
|
||||||
using FictionArchive.Service.NovelService.Models.Enums;
|
|
||||||
using FictionArchive.Service.NovelService.Models.Localization;
|
|
||||||
using FictionArchive.Service.NovelService.Models.Novels;
|
|
||||||
using FictionArchive.Service.NovelService.Models.SourceAdapters;
|
|
||||||
using FictionArchive.Service.NovelService.Services;
|
using FictionArchive.Service.NovelService.Services;
|
||||||
using FictionArchive.Service.NovelService.Services.SourceAdapters;
|
using FictionArchive.Service.Shared.MassTransit.Contracts.Commands;
|
||||||
using FictionArchive.Service.Shared.Contracts.Events;
|
|
||||||
using HotChocolate.Authorization;
|
using HotChocolate.Authorization;
|
||||||
using HotChocolate.Types;
|
using HotChocolate.Types;
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
|
|
||||||
namespace FictionArchive.Service.NovelService.GraphQL;
|
namespace FictionArchive.Service.NovelService.GraphQL;
|
||||||
|
|
||||||
public class Mutation
|
public class Mutation
|
||||||
{
|
{
|
||||||
[Error<InvalidOperationException>]
|
|
||||||
[Authorize]
|
[Authorize]
|
||||||
public async Task<NovelImportRequested> ImportNovel(string novelUrl, NovelUpdateService service)
|
public async Task<ImportNovelCommand> ImportNovel(string novelUrl, NovelUpdateService service)
|
||||||
{
|
{
|
||||||
return await service.QueueNovelImport(novelUrl);
|
return await service.QueueNovelImport(novelUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Authorize]
|
[Authorize]
|
||||||
public async Task<ChapterPullRequested> FetchChapterContents(
|
public async Task<PullChapterContentCommand> FetchChapterContents(
|
||||||
Guid importId,
|
|
||||||
uint novelId,
|
uint novelId,
|
||||||
uint volumeId,
|
uint volumeId,
|
||||||
uint chapterOrder,
|
uint chapterOrder,
|
||||||
NovelUpdateService service)
|
NovelUpdateService service)
|
||||||
{
|
{
|
||||||
return await service.QueueChapterPull(importId, novelId, volumeId, chapterOrder);
|
return await service.QueueChapterPull(novelId, volumeId, chapterOrder);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Error<KeyNotFoundException>]
|
[Error<KeyNotFoundException>]
|
||||||
|
|||||||
@@ -1,673 +0,0 @@
|
|||||||
// <auto-generated />
|
|
||||||
using System;
|
|
||||||
using FictionArchive.Service.NovelService.Services;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
|
||||||
using Microsoft.EntityFrameworkCore.Migrations;
|
|
||||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
|
||||||
using NodaTime;
|
|
||||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
|
||||||
|
|
||||||
#nullable disable
|
|
||||||
|
|
||||||
namespace FictionArchive.Service.NovelService.Migrations
|
|
||||||
{
|
|
||||||
[DbContext(typeof(NovelServiceDbContext))]
|
|
||||||
[Migration("20260127161500_AddNovelImportSaga")]
|
|
||||||
partial class AddNovelImportSaga
|
|
||||||
{
|
|
||||||
/// <inheritdoc />
|
|
||||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
|
||||||
{
|
|
||||||
#pragma warning disable 612, 618
|
|
||||||
modelBuilder
|
|
||||||
.HasAnnotation("ProductVersion", "9.0.11")
|
|
||||||
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
|
||||||
|
|
||||||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
|
||||||
|
|
||||||
modelBuilder.Entity("FictionArchive.Service.NovelService.Models.ActiveImport", b =>
|
|
||||||
{
|
|
||||||
b.Property<Guid>("ImportId")
|
|
||||||
.ValueGeneratedOnAdd()
|
|
||||||
.HasColumnType("uuid");
|
|
||||||
|
|
||||||
b.Property<string>("NovelUrl")
|
|
||||||
.IsRequired()
|
|
||||||
.HasColumnType("text");
|
|
||||||
|
|
||||||
b.Property<Instant>("StartedAt")
|
|
||||||
.HasColumnType("timestamp with time zone");
|
|
||||||
|
|
||||||
b.HasKey("ImportId");
|
|
||||||
|
|
||||||
b.HasIndex("NovelUrl")
|
|
||||||
.IsUnique();
|
|
||||||
|
|
||||||
b.ToTable("ActiveImports");
|
|
||||||
});
|
|
||||||
|
|
||||||
modelBuilder.Entity("FictionArchive.Service.NovelService.Models.Images.Image", b =>
|
|
||||||
{
|
|
||||||
b.Property<Guid>("Id")
|
|
||||||
.ValueGeneratedOnAdd()
|
|
||||||
.HasColumnType("uuid");
|
|
||||||
|
|
||||||
b.Property<long?>("ChapterId")
|
|
||||||
.HasColumnType("bigint");
|
|
||||||
|
|
||||||
b.Property<Instant>("CreatedTime")
|
|
||||||
.HasColumnType("timestamp with time zone");
|
|
||||||
|
|
||||||
b.Property<Instant>("LastUpdatedTime")
|
|
||||||
.HasColumnType("timestamp with time zone");
|
|
||||||
|
|
||||||
b.Property<string>("NewPath")
|
|
||||||
.HasColumnType("text");
|
|
||||||
|
|
||||||
b.Property<string>("OriginalPath")
|
|
||||||
.IsRequired()
|
|
||||||
.HasColumnType("text");
|
|
||||||
|
|
||||||
b.HasKey("Id");
|
|
||||||
|
|
||||||
b.HasIndex("ChapterId");
|
|
||||||
|
|
||||||
b.ToTable("Images");
|
|
||||||
});
|
|
||||||
|
|
||||||
modelBuilder.Entity("FictionArchive.Service.NovelService.Models.Localization.LocalizationKey", b =>
|
|
||||||
{
|
|
||||||
b.Property<Guid>("Id")
|
|
||||||
.ValueGeneratedOnAdd()
|
|
||||||
.HasColumnType("uuid");
|
|
||||||
|
|
||||||
b.Property<Instant>("CreatedTime")
|
|
||||||
.HasColumnType("timestamp with time zone");
|
|
||||||
|
|
||||||
b.Property<Instant>("LastUpdatedTime")
|
|
||||||
.HasColumnType("timestamp with time zone");
|
|
||||||
|
|
||||||
b.HasKey("Id");
|
|
||||||
|
|
||||||
b.ToTable("LocalizationKeys");
|
|
||||||
});
|
|
||||||
|
|
||||||
modelBuilder.Entity("FictionArchive.Service.NovelService.Models.Localization.LocalizationRequest", b =>
|
|
||||||
{
|
|
||||||
b.Property<Guid>("Id")
|
|
||||||
.ValueGeneratedOnAdd()
|
|
||||||
.HasColumnType("uuid");
|
|
||||||
|
|
||||||
b.Property<Instant>("CreatedTime")
|
|
||||||
.HasColumnType("timestamp with time zone");
|
|
||||||
|
|
||||||
b.Property<long>("EngineId")
|
|
||||||
.HasColumnType("bigint");
|
|
||||||
|
|
||||||
b.Property<Guid>("KeyRequestedForTranslationId")
|
|
||||||
.HasColumnType("uuid");
|
|
||||||
|
|
||||||
b.Property<Instant>("LastUpdatedTime")
|
|
||||||
.HasColumnType("timestamp with time zone");
|
|
||||||
|
|
||||||
b.Property<int>("TranslateTo")
|
|
||||||
.HasColumnType("integer");
|
|
||||||
|
|
||||||
b.HasKey("Id");
|
|
||||||
|
|
||||||
b.HasIndex("EngineId");
|
|
||||||
|
|
||||||
b.HasIndex("KeyRequestedForTranslationId");
|
|
||||||
|
|
||||||
b.ToTable("LocalizationRequests");
|
|
||||||
});
|
|
||||||
|
|
||||||
modelBuilder.Entity("FictionArchive.Service.NovelService.Models.Localization.LocalizationText", b =>
|
|
||||||
{
|
|
||||||
b.Property<Guid>("Id")
|
|
||||||
.ValueGeneratedOnAdd()
|
|
||||||
.HasColumnType("uuid");
|
|
||||||
|
|
||||||
b.Property<Instant>("CreatedTime")
|
|
||||||
.HasColumnType("timestamp with time zone");
|
|
||||||
|
|
||||||
b.Property<int>("Language")
|
|
||||||
.HasColumnType("integer");
|
|
||||||
|
|
||||||
b.Property<Instant>("LastUpdatedTime")
|
|
||||||
.HasColumnType("timestamp with time zone");
|
|
||||||
|
|
||||||
b.Property<Guid?>("LocalizationKeyId")
|
|
||||||
.HasColumnType("uuid");
|
|
||||||
|
|
||||||
b.Property<string>("Text")
|
|
||||||
.IsRequired()
|
|
||||||
.HasColumnType("text");
|
|
||||||
|
|
||||||
b.Property<long?>("TranslationEngineId")
|
|
||||||
.HasColumnType("bigint");
|
|
||||||
|
|
||||||
b.HasKey("Id");
|
|
||||||
|
|
||||||
b.HasIndex("LocalizationKeyId");
|
|
||||||
|
|
||||||
b.HasIndex("TranslationEngineId");
|
|
||||||
|
|
||||||
b.ToTable("LocalizationText");
|
|
||||||
});
|
|
||||||
|
|
||||||
modelBuilder.Entity("FictionArchive.Service.NovelService.Models.Novels.Chapter", b =>
|
|
||||||
{
|
|
||||||
b.Property<long>("Id")
|
|
||||||
.ValueGeneratedOnAdd()
|
|
||||||
.HasColumnType("bigint");
|
|
||||||
|
|
||||||
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<long>("Id"));
|
|
||||||
|
|
||||||
b.Property<Guid>("BodyId")
|
|
||||||
.HasColumnType("uuid");
|
|
||||||
|
|
||||||
b.Property<Instant>("CreatedTime")
|
|
||||||
.HasColumnType("timestamp with time zone");
|
|
||||||
|
|
||||||
b.Property<Instant>("LastUpdatedTime")
|
|
||||||
.HasColumnType("timestamp with time zone");
|
|
||||||
|
|
||||||
b.Property<Guid>("NameId")
|
|
||||||
.HasColumnType("uuid");
|
|
||||||
|
|
||||||
b.Property<long>("Order")
|
|
||||||
.HasColumnType("bigint");
|
|
||||||
|
|
||||||
b.Property<long>("Revision")
|
|
||||||
.HasColumnType("bigint");
|
|
||||||
|
|
||||||
b.Property<string>("Url")
|
|
||||||
.HasColumnType("text");
|
|
||||||
|
|
||||||
b.Property<long>("VolumeId")
|
|
||||||
.HasColumnType("bigint");
|
|
||||||
|
|
||||||
b.HasKey("Id");
|
|
||||||
|
|
||||||
b.HasIndex("BodyId");
|
|
||||||
|
|
||||||
b.HasIndex("NameId");
|
|
||||||
|
|
||||||
b.HasIndex("VolumeId", "Order")
|
|
||||||
.IsUnique();
|
|
||||||
|
|
||||||
b.ToTable("Chapter");
|
|
||||||
});
|
|
||||||
|
|
||||||
modelBuilder.Entity("FictionArchive.Service.NovelService.Models.Novels.Novel", b =>
|
|
||||||
{
|
|
||||||
b.Property<long>("Id")
|
|
||||||
.ValueGeneratedOnAdd()
|
|
||||||
.HasColumnType("bigint");
|
|
||||||
|
|
||||||
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<long>("Id"));
|
|
||||||
|
|
||||||
b.Property<long>("AuthorId")
|
|
||||||
.HasColumnType("bigint");
|
|
||||||
|
|
||||||
b.Property<Guid?>("CoverImageId")
|
|
||||||
.HasColumnType("uuid");
|
|
||||||
|
|
||||||
b.Property<Instant>("CreatedTime")
|
|
||||||
.HasColumnType("timestamp with time zone");
|
|
||||||
|
|
||||||
b.Property<Guid>("DescriptionId")
|
|
||||||
.HasColumnType("uuid");
|
|
||||||
|
|
||||||
b.Property<string>("ExternalId")
|
|
||||||
.IsRequired()
|
|
||||||
.HasColumnType("text");
|
|
||||||
|
|
||||||
b.Property<Instant>("LastUpdatedTime")
|
|
||||||
.HasColumnType("timestamp with time zone");
|
|
||||||
|
|
||||||
b.Property<Guid>("NameId")
|
|
||||||
.HasColumnType("uuid");
|
|
||||||
|
|
||||||
b.Property<int>("RawLanguage")
|
|
||||||
.HasColumnType("integer");
|
|
||||||
|
|
||||||
b.Property<int>("RawStatus")
|
|
||||||
.HasColumnType("integer");
|
|
||||||
|
|
||||||
b.Property<long>("SourceId")
|
|
||||||
.HasColumnType("bigint");
|
|
||||||
|
|
||||||
b.Property<int?>("StatusOverride")
|
|
||||||
.HasColumnType("integer");
|
|
||||||
|
|
||||||
b.Property<string>("Url")
|
|
||||||
.IsRequired()
|
|
||||||
.HasColumnType("text");
|
|
||||||
|
|
||||||
b.HasKey("Id");
|
|
||||||
|
|
||||||
b.HasIndex("AuthorId");
|
|
||||||
|
|
||||||
b.HasIndex("CoverImageId");
|
|
||||||
|
|
||||||
b.HasIndex("DescriptionId");
|
|
||||||
|
|
||||||
b.HasIndex("NameId");
|
|
||||||
|
|
||||||
b.HasIndex("SourceId");
|
|
||||||
|
|
||||||
b.HasIndex("ExternalId", "SourceId")
|
|
||||||
.IsUnique();
|
|
||||||
|
|
||||||
b.ToTable("Novels");
|
|
||||||
});
|
|
||||||
|
|
||||||
modelBuilder.Entity("FictionArchive.Service.NovelService.Models.Novels.NovelTag", b =>
|
|
||||||
{
|
|
||||||
b.Property<long>("Id")
|
|
||||||
.ValueGeneratedOnAdd()
|
|
||||||
.HasColumnType("bigint");
|
|
||||||
|
|
||||||
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<long>("Id"));
|
|
||||||
|
|
||||||
b.Property<Instant>("CreatedTime")
|
|
||||||
.HasColumnType("timestamp with time zone");
|
|
||||||
|
|
||||||
b.Property<Guid>("DisplayNameId")
|
|
||||||
.HasColumnType("uuid");
|
|
||||||
|
|
||||||
b.Property<string>("Key")
|
|
||||||
.IsRequired()
|
|
||||||
.HasColumnType("text");
|
|
||||||
|
|
||||||
b.Property<Instant>("LastUpdatedTime")
|
|
||||||
.HasColumnType("timestamp with time zone");
|
|
||||||
|
|
||||||
b.Property<long?>("SourceId")
|
|
||||||
.HasColumnType("bigint");
|
|
||||||
|
|
||||||
b.Property<int>("TagType")
|
|
||||||
.HasColumnType("integer");
|
|
||||||
|
|
||||||
b.HasKey("Id");
|
|
||||||
|
|
||||||
b.HasIndex("DisplayNameId");
|
|
||||||
|
|
||||||
b.HasIndex("SourceId");
|
|
||||||
|
|
||||||
b.ToTable("Tags");
|
|
||||||
});
|
|
||||||
|
|
||||||
modelBuilder.Entity("FictionArchive.Service.NovelService.Models.Novels.Person", b =>
|
|
||||||
{
|
|
||||||
b.Property<long>("Id")
|
|
||||||
.ValueGeneratedOnAdd()
|
|
||||||
.HasColumnType("bigint");
|
|
||||||
|
|
||||||
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<long>("Id"));
|
|
||||||
|
|
||||||
b.Property<Instant>("CreatedTime")
|
|
||||||
.HasColumnType("timestamp with time zone");
|
|
||||||
|
|
||||||
b.Property<string>("ExternalUrl")
|
|
||||||
.HasColumnType("text");
|
|
||||||
|
|
||||||
b.Property<Instant>("LastUpdatedTime")
|
|
||||||
.HasColumnType("timestamp with time zone");
|
|
||||||
|
|
||||||
b.Property<Guid>("NameId")
|
|
||||||
.HasColumnType("uuid");
|
|
||||||
|
|
||||||
b.HasKey("Id");
|
|
||||||
|
|
||||||
b.HasIndex("NameId");
|
|
||||||
|
|
||||||
b.ToTable("Person");
|
|
||||||
});
|
|
||||||
|
|
||||||
modelBuilder.Entity("FictionArchive.Service.NovelService.Models.Novels.Source", b =>
|
|
||||||
{
|
|
||||||
b.Property<long>("Id")
|
|
||||||
.ValueGeneratedOnAdd()
|
|
||||||
.HasColumnType("bigint");
|
|
||||||
|
|
||||||
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<long>("Id"));
|
|
||||||
|
|
||||||
b.Property<Instant>("CreatedTime")
|
|
||||||
.HasColumnType("timestamp with time zone");
|
|
||||||
|
|
||||||
b.Property<string>("Key")
|
|
||||||
.IsRequired()
|
|
||||||
.HasColumnType("text");
|
|
||||||
|
|
||||||
b.Property<Instant>("LastUpdatedTime")
|
|
||||||
.HasColumnType("timestamp with time zone");
|
|
||||||
|
|
||||||
b.Property<string>("Name")
|
|
||||||
.IsRequired()
|
|
||||||
.HasColumnType("text");
|
|
||||||
|
|
||||||
b.Property<string>("Url")
|
|
||||||
.IsRequired()
|
|
||||||
.HasColumnType("text");
|
|
||||||
|
|
||||||
b.HasKey("Id");
|
|
||||||
|
|
||||||
b.ToTable("Sources");
|
|
||||||
});
|
|
||||||
|
|
||||||
modelBuilder.Entity("FictionArchive.Service.NovelService.Models.Novels.TranslationEngine", b =>
|
|
||||||
{
|
|
||||||
b.Property<long>("Id")
|
|
||||||
.ValueGeneratedOnAdd()
|
|
||||||
.HasColumnType("bigint");
|
|
||||||
|
|
||||||
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<long>("Id"));
|
|
||||||
|
|
||||||
b.Property<Instant>("CreatedTime")
|
|
||||||
.HasColumnType("timestamp with time zone");
|
|
||||||
|
|
||||||
b.Property<string>("Key")
|
|
||||||
.IsRequired()
|
|
||||||
.HasColumnType("text");
|
|
||||||
|
|
||||||
b.Property<Instant>("LastUpdatedTime")
|
|
||||||
.HasColumnType("timestamp with time zone");
|
|
||||||
|
|
||||||
b.HasKey("Id");
|
|
||||||
|
|
||||||
b.ToTable("TranslationEngines");
|
|
||||||
});
|
|
||||||
|
|
||||||
modelBuilder.Entity("FictionArchive.Service.NovelService.Models.Novels.Volume", b =>
|
|
||||||
{
|
|
||||||
b.Property<long>("Id")
|
|
||||||
.ValueGeneratedOnAdd()
|
|
||||||
.HasColumnType("bigint");
|
|
||||||
|
|
||||||
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<long>("Id"));
|
|
||||||
|
|
||||||
b.Property<Instant>("CreatedTime")
|
|
||||||
.HasColumnType("timestamp with time zone");
|
|
||||||
|
|
||||||
b.Property<Instant>("LastUpdatedTime")
|
|
||||||
.HasColumnType("timestamp with time zone");
|
|
||||||
|
|
||||||
b.Property<Guid>("NameId")
|
|
||||||
.HasColumnType("uuid");
|
|
||||||
|
|
||||||
b.Property<long>("NovelId")
|
|
||||||
.HasColumnType("bigint");
|
|
||||||
|
|
||||||
b.Property<int>("Order")
|
|
||||||
.HasColumnType("integer");
|
|
||||||
|
|
||||||
b.HasKey("Id");
|
|
||||||
|
|
||||||
b.HasIndex("NameId");
|
|
||||||
|
|
||||||
b.HasIndex("NovelId", "Order")
|
|
||||||
.IsUnique();
|
|
||||||
|
|
||||||
b.ToTable("Volume");
|
|
||||||
});
|
|
||||||
|
|
||||||
modelBuilder.Entity("FictionArchive.Service.NovelService.Sagas.NovelImportSagaState", b =>
|
|
||||||
{
|
|
||||||
b.Property<Guid>("CorrelationId")
|
|
||||||
.ValueGeneratedOnAdd()
|
|
||||||
.HasColumnType("uuid");
|
|
||||||
|
|
||||||
b.Property<Instant?>("CompletedAt")
|
|
||||||
.HasColumnType("timestamp with time zone");
|
|
||||||
|
|
||||||
b.Property<int>("CompletedChapters")
|
|
||||||
.HasColumnType("integer");
|
|
||||||
|
|
||||||
b.Property<int>("CompletedImages")
|
|
||||||
.HasColumnType("integer");
|
|
||||||
|
|
||||||
b.Property<string>("CurrentState")
|
|
||||||
.IsRequired()
|
|
||||||
.HasColumnType("text");
|
|
||||||
|
|
||||||
b.Property<string>("ErrorMessage")
|
|
||||||
.HasColumnType("text");
|
|
||||||
|
|
||||||
b.Property<int>("ExpectedChapters")
|
|
||||||
.HasColumnType("integer");
|
|
||||||
|
|
||||||
b.Property<int>("ExpectedImages")
|
|
||||||
.HasColumnType("integer");
|
|
||||||
|
|
||||||
b.Property<long?>("NovelId")
|
|
||||||
.HasColumnType("bigint");
|
|
||||||
|
|
||||||
b.Property<string>("NovelUrl")
|
|
||||||
.IsRequired()
|
|
||||||
.HasColumnType("text");
|
|
||||||
|
|
||||||
b.Property<Instant>("StartedAt")
|
|
||||||
.HasColumnType("timestamp with time zone");
|
|
||||||
|
|
||||||
b.HasKey("CorrelationId");
|
|
||||||
|
|
||||||
b.HasIndex("CurrentState");
|
|
||||||
|
|
||||||
b.HasIndex("NovelUrl");
|
|
||||||
|
|
||||||
b.ToTable("NovelImportSagaStates");
|
|
||||||
});
|
|
||||||
|
|
||||||
modelBuilder.Entity("NovelNovelTag", b =>
|
|
||||||
{
|
|
||||||
b.Property<long>("NovelsId")
|
|
||||||
.HasColumnType("bigint");
|
|
||||||
|
|
||||||
b.Property<long>("TagsId")
|
|
||||||
.HasColumnType("bigint");
|
|
||||||
|
|
||||||
b.HasKey("NovelsId", "TagsId");
|
|
||||||
|
|
||||||
b.HasIndex("TagsId");
|
|
||||||
|
|
||||||
b.ToTable("NovelNovelTag");
|
|
||||||
});
|
|
||||||
|
|
||||||
modelBuilder.Entity("FictionArchive.Service.NovelService.Models.Images.Image", b =>
|
|
||||||
{
|
|
||||||
b.HasOne("FictionArchive.Service.NovelService.Models.Novels.Chapter", "Chapter")
|
|
||||||
.WithMany("Images")
|
|
||||||
.HasForeignKey("ChapterId");
|
|
||||||
|
|
||||||
b.Navigation("Chapter");
|
|
||||||
});
|
|
||||||
|
|
||||||
modelBuilder.Entity("FictionArchive.Service.NovelService.Models.Localization.LocalizationRequest", b =>
|
|
||||||
{
|
|
||||||
b.HasOne("FictionArchive.Service.NovelService.Models.Novels.TranslationEngine", "Engine")
|
|
||||||
.WithMany()
|
|
||||||
.HasForeignKey("EngineId")
|
|
||||||
.OnDelete(DeleteBehavior.Cascade)
|
|
||||||
.IsRequired();
|
|
||||||
|
|
||||||
b.HasOne("FictionArchive.Service.NovelService.Models.Localization.LocalizationKey", "KeyRequestedForTranslation")
|
|
||||||
.WithMany()
|
|
||||||
.HasForeignKey("KeyRequestedForTranslationId")
|
|
||||||
.OnDelete(DeleteBehavior.Cascade)
|
|
||||||
.IsRequired();
|
|
||||||
|
|
||||||
b.Navigation("Engine");
|
|
||||||
|
|
||||||
b.Navigation("KeyRequestedForTranslation");
|
|
||||||
});
|
|
||||||
|
|
||||||
modelBuilder.Entity("FictionArchive.Service.NovelService.Models.Localization.LocalizationText", b =>
|
|
||||||
{
|
|
||||||
b.HasOne("FictionArchive.Service.NovelService.Models.Localization.LocalizationKey", null)
|
|
||||||
.WithMany("Texts")
|
|
||||||
.HasForeignKey("LocalizationKeyId");
|
|
||||||
|
|
||||||
b.HasOne("FictionArchive.Service.NovelService.Models.Novels.TranslationEngine", "TranslationEngine")
|
|
||||||
.WithMany()
|
|
||||||
.HasForeignKey("TranslationEngineId");
|
|
||||||
|
|
||||||
b.Navigation("TranslationEngine");
|
|
||||||
});
|
|
||||||
|
|
||||||
modelBuilder.Entity("FictionArchive.Service.NovelService.Models.Novels.Chapter", b =>
|
|
||||||
{
|
|
||||||
b.HasOne("FictionArchive.Service.NovelService.Models.Localization.LocalizationKey", "Body")
|
|
||||||
.WithMany()
|
|
||||||
.HasForeignKey("BodyId")
|
|
||||||
.OnDelete(DeleteBehavior.Cascade)
|
|
||||||
.IsRequired();
|
|
||||||
|
|
||||||
b.HasOne("FictionArchive.Service.NovelService.Models.Localization.LocalizationKey", "Name")
|
|
||||||
.WithMany()
|
|
||||||
.HasForeignKey("NameId")
|
|
||||||
.OnDelete(DeleteBehavior.Cascade)
|
|
||||||
.IsRequired();
|
|
||||||
|
|
||||||
b.HasOne("FictionArchive.Service.NovelService.Models.Novels.Volume", "Volume")
|
|
||||||
.WithMany("Chapters")
|
|
||||||
.HasForeignKey("VolumeId")
|
|
||||||
.OnDelete(DeleteBehavior.Cascade)
|
|
||||||
.IsRequired();
|
|
||||||
|
|
||||||
b.Navigation("Body");
|
|
||||||
|
|
||||||
b.Navigation("Name");
|
|
||||||
|
|
||||||
b.Navigation("Volume");
|
|
||||||
});
|
|
||||||
|
|
||||||
modelBuilder.Entity("FictionArchive.Service.NovelService.Models.Novels.Novel", b =>
|
|
||||||
{
|
|
||||||
b.HasOne("FictionArchive.Service.NovelService.Models.Novels.Person", "Author")
|
|
||||||
.WithMany()
|
|
||||||
.HasForeignKey("AuthorId")
|
|
||||||
.OnDelete(DeleteBehavior.Cascade)
|
|
||||||
.IsRequired();
|
|
||||||
|
|
||||||
b.HasOne("FictionArchive.Service.NovelService.Models.Images.Image", "CoverImage")
|
|
||||||
.WithMany()
|
|
||||||
.HasForeignKey("CoverImageId");
|
|
||||||
|
|
||||||
b.HasOne("FictionArchive.Service.NovelService.Models.Localization.LocalizationKey", "Description")
|
|
||||||
.WithMany()
|
|
||||||
.HasForeignKey("DescriptionId")
|
|
||||||
.OnDelete(DeleteBehavior.Cascade)
|
|
||||||
.IsRequired();
|
|
||||||
|
|
||||||
b.HasOne("FictionArchive.Service.NovelService.Models.Localization.LocalizationKey", "Name")
|
|
||||||
.WithMany()
|
|
||||||
.HasForeignKey("NameId")
|
|
||||||
.OnDelete(DeleteBehavior.Cascade)
|
|
||||||
.IsRequired();
|
|
||||||
|
|
||||||
b.HasOne("FictionArchive.Service.NovelService.Models.Novels.Source", "Source")
|
|
||||||
.WithMany()
|
|
||||||
.HasForeignKey("SourceId")
|
|
||||||
.OnDelete(DeleteBehavior.Cascade)
|
|
||||||
.IsRequired();
|
|
||||||
|
|
||||||
b.Navigation("Author");
|
|
||||||
|
|
||||||
b.Navigation("CoverImage");
|
|
||||||
|
|
||||||
b.Navigation("Description");
|
|
||||||
|
|
||||||
b.Navigation("Name");
|
|
||||||
|
|
||||||
b.Navigation("Source");
|
|
||||||
});
|
|
||||||
|
|
||||||
modelBuilder.Entity("FictionArchive.Service.NovelService.Models.Novels.NovelTag", b =>
|
|
||||||
{
|
|
||||||
b.HasOne("FictionArchive.Service.NovelService.Models.Localization.LocalizationKey", "DisplayName")
|
|
||||||
.WithMany()
|
|
||||||
.HasForeignKey("DisplayNameId")
|
|
||||||
.OnDelete(DeleteBehavior.Cascade)
|
|
||||||
.IsRequired();
|
|
||||||
|
|
||||||
b.HasOne("FictionArchive.Service.NovelService.Models.Novels.Source", "Source")
|
|
||||||
.WithMany()
|
|
||||||
.HasForeignKey("SourceId");
|
|
||||||
|
|
||||||
b.Navigation("DisplayName");
|
|
||||||
|
|
||||||
b.Navigation("Source");
|
|
||||||
});
|
|
||||||
|
|
||||||
modelBuilder.Entity("FictionArchive.Service.NovelService.Models.Novels.Person", b =>
|
|
||||||
{
|
|
||||||
b.HasOne("FictionArchive.Service.NovelService.Models.Localization.LocalizationKey", "Name")
|
|
||||||
.WithMany()
|
|
||||||
.HasForeignKey("NameId")
|
|
||||||
.OnDelete(DeleteBehavior.Cascade)
|
|
||||||
.IsRequired();
|
|
||||||
|
|
||||||
b.Navigation("Name");
|
|
||||||
});
|
|
||||||
|
|
||||||
modelBuilder.Entity("FictionArchive.Service.NovelService.Models.Novels.Volume", b =>
|
|
||||||
{
|
|
||||||
b.HasOne("FictionArchive.Service.NovelService.Models.Localization.LocalizationKey", "Name")
|
|
||||||
.WithMany()
|
|
||||||
.HasForeignKey("NameId")
|
|
||||||
.OnDelete(DeleteBehavior.Cascade)
|
|
||||||
.IsRequired();
|
|
||||||
|
|
||||||
b.HasOne("FictionArchive.Service.NovelService.Models.Novels.Novel", "Novel")
|
|
||||||
.WithMany("Volumes")
|
|
||||||
.HasForeignKey("NovelId")
|
|
||||||
.OnDelete(DeleteBehavior.Cascade)
|
|
||||||
.IsRequired();
|
|
||||||
|
|
||||||
b.Navigation("Name");
|
|
||||||
|
|
||||||
b.Navigation("Novel");
|
|
||||||
});
|
|
||||||
|
|
||||||
modelBuilder.Entity("NovelNovelTag", b =>
|
|
||||||
{
|
|
||||||
b.HasOne("FictionArchive.Service.NovelService.Models.Novels.Novel", null)
|
|
||||||
.WithMany()
|
|
||||||
.HasForeignKey("NovelsId")
|
|
||||||
.OnDelete(DeleteBehavior.Cascade)
|
|
||||||
.IsRequired();
|
|
||||||
|
|
||||||
b.HasOne("FictionArchive.Service.NovelService.Models.Novels.NovelTag", null)
|
|
||||||
.WithMany()
|
|
||||||
.HasForeignKey("TagsId")
|
|
||||||
.OnDelete(DeleteBehavior.Cascade)
|
|
||||||
.IsRequired();
|
|
||||||
});
|
|
||||||
|
|
||||||
modelBuilder.Entity("FictionArchive.Service.NovelService.Models.Localization.LocalizationKey", b =>
|
|
||||||
{
|
|
||||||
b.Navigation("Texts");
|
|
||||||
});
|
|
||||||
|
|
||||||
modelBuilder.Entity("FictionArchive.Service.NovelService.Models.Novels.Chapter", b =>
|
|
||||||
{
|
|
||||||
b.Navigation("Images");
|
|
||||||
});
|
|
||||||
|
|
||||||
modelBuilder.Entity("FictionArchive.Service.NovelService.Models.Novels.Novel", b =>
|
|
||||||
{
|
|
||||||
b.Navigation("Volumes");
|
|
||||||
});
|
|
||||||
|
|
||||||
modelBuilder.Entity("FictionArchive.Service.NovelService.Models.Novels.Volume", b =>
|
|
||||||
{
|
|
||||||
b.Navigation("Chapters");
|
|
||||||
});
|
|
||||||
#pragma warning restore 612, 618
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,76 +0,0 @@
|
|||||||
using System;
|
|
||||||
using Microsoft.EntityFrameworkCore.Migrations;
|
|
||||||
using NodaTime;
|
|
||||||
|
|
||||||
#nullable disable
|
|
||||||
|
|
||||||
namespace FictionArchive.Service.NovelService.Migrations
|
|
||||||
{
|
|
||||||
/// <inheritdoc />
|
|
||||||
public partial class AddNovelImportSaga : Migration
|
|
||||||
{
|
|
||||||
/// <inheritdoc />
|
|
||||||
protected override void Up(MigrationBuilder migrationBuilder)
|
|
||||||
{
|
|
||||||
migrationBuilder.CreateTable(
|
|
||||||
name: "ActiveImports",
|
|
||||||
columns: table => new
|
|
||||||
{
|
|
||||||
ImportId = table.Column<Guid>(type: "uuid", nullable: false),
|
|
||||||
NovelUrl = table.Column<string>(type: "text", nullable: false),
|
|
||||||
StartedAt = table.Column<Instant>(type: "timestamp with time zone", nullable: false)
|
|
||||||
},
|
|
||||||
constraints: table =>
|
|
||||||
{
|
|
||||||
table.PrimaryKey("PK_ActiveImports", x => x.ImportId);
|
|
||||||
});
|
|
||||||
|
|
||||||
migrationBuilder.CreateTable(
|
|
||||||
name: "NovelImportSagaStates",
|
|
||||||
columns: table => new
|
|
||||||
{
|
|
||||||
CorrelationId = table.Column<Guid>(type: "uuid", nullable: false),
|
|
||||||
CurrentState = table.Column<string>(type: "text", nullable: false),
|
|
||||||
NovelUrl = table.Column<string>(type: "text", nullable: false),
|
|
||||||
NovelId = table.Column<long>(type: "bigint", nullable: true),
|
|
||||||
ExpectedChapters = table.Column<int>(type: "integer", nullable: false),
|
|
||||||
CompletedChapters = table.Column<int>(type: "integer", nullable: false),
|
|
||||||
ExpectedImages = table.Column<int>(type: "integer", nullable: false),
|
|
||||||
CompletedImages = table.Column<int>(type: "integer", nullable: false),
|
|
||||||
StartedAt = table.Column<Instant>(type: "timestamp with time zone", nullable: false),
|
|
||||||
CompletedAt = table.Column<Instant>(type: "timestamp with time zone", nullable: true),
|
|
||||||
ErrorMessage = table.Column<string>(type: "text", nullable: true)
|
|
||||||
},
|
|
||||||
constraints: table =>
|
|
||||||
{
|
|
||||||
table.PrimaryKey("PK_NovelImportSagaStates", x => x.CorrelationId);
|
|
||||||
});
|
|
||||||
|
|
||||||
migrationBuilder.CreateIndex(
|
|
||||||
name: "IX_ActiveImports_NovelUrl",
|
|
||||||
table: "ActiveImports",
|
|
||||||
column: "NovelUrl",
|
|
||||||
unique: true);
|
|
||||||
|
|
||||||
migrationBuilder.CreateIndex(
|
|
||||||
name: "IX_NovelImportSagaStates_CurrentState",
|
|
||||||
table: "NovelImportSagaStates",
|
|
||||||
column: "CurrentState");
|
|
||||||
|
|
||||||
migrationBuilder.CreateIndex(
|
|
||||||
name: "IX_NovelImportSagaStates_NovelUrl",
|
|
||||||
table: "NovelImportSagaStates",
|
|
||||||
column: "NovelUrl");
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
protected override void Down(MigrationBuilder migrationBuilder)
|
|
||||||
{
|
|
||||||
migrationBuilder.DropTable(
|
|
||||||
name: "ActiveImports");
|
|
||||||
|
|
||||||
migrationBuilder.DropTable(
|
|
||||||
name: "NovelImportSagaStates");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -23,27 +23,6 @@ namespace FictionArchive.Service.NovelService.Migrations
|
|||||||
|
|
||||||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||||
|
|
||||||
modelBuilder.Entity("FictionArchive.Service.NovelService.Models.ActiveImport", b =>
|
|
||||||
{
|
|
||||||
b.Property<Guid>("ImportId")
|
|
||||||
.ValueGeneratedOnAdd()
|
|
||||||
.HasColumnType("uuid");
|
|
||||||
|
|
||||||
b.Property<string>("NovelUrl")
|
|
||||||
.IsRequired()
|
|
||||||
.HasColumnType("text");
|
|
||||||
|
|
||||||
b.Property<Instant>("StartedAt")
|
|
||||||
.HasColumnType("timestamp with time zone");
|
|
||||||
|
|
||||||
b.HasKey("ImportId");
|
|
||||||
|
|
||||||
b.HasIndex("NovelUrl")
|
|
||||||
.IsUnique();
|
|
||||||
|
|
||||||
b.ToTable("ActiveImports");
|
|
||||||
});
|
|
||||||
|
|
||||||
modelBuilder.Entity("FictionArchive.Service.NovelService.Models.Images.Image", b =>
|
modelBuilder.Entity("FictionArchive.Service.NovelService.Models.Images.Image", b =>
|
||||||
{
|
{
|
||||||
b.Property<Guid>("Id")
|
b.Property<Guid>("Id")
|
||||||
@@ -412,53 +391,6 @@ namespace FictionArchive.Service.NovelService.Migrations
|
|||||||
b.ToTable("Volume");
|
b.ToTable("Volume");
|
||||||
});
|
});
|
||||||
|
|
||||||
modelBuilder.Entity("FictionArchive.Service.NovelService.Sagas.NovelImportSagaState", b =>
|
|
||||||
{
|
|
||||||
b.Property<Guid>("CorrelationId")
|
|
||||||
.ValueGeneratedOnAdd()
|
|
||||||
.HasColumnType("uuid");
|
|
||||||
|
|
||||||
b.Property<Instant?>("CompletedAt")
|
|
||||||
.HasColumnType("timestamp with time zone");
|
|
||||||
|
|
||||||
b.Property<int>("CompletedChapters")
|
|
||||||
.HasColumnType("integer");
|
|
||||||
|
|
||||||
b.Property<int>("CompletedImages")
|
|
||||||
.HasColumnType("integer");
|
|
||||||
|
|
||||||
b.Property<string>("CurrentState")
|
|
||||||
.IsRequired()
|
|
||||||
.HasColumnType("text");
|
|
||||||
|
|
||||||
b.Property<string>("ErrorMessage")
|
|
||||||
.HasColumnType("text");
|
|
||||||
|
|
||||||
b.Property<int>("ExpectedChapters")
|
|
||||||
.HasColumnType("integer");
|
|
||||||
|
|
||||||
b.Property<int>("ExpectedImages")
|
|
||||||
.HasColumnType("integer");
|
|
||||||
|
|
||||||
b.Property<long?>("NovelId")
|
|
||||||
.HasColumnType("bigint");
|
|
||||||
|
|
||||||
b.Property<string>("NovelUrl")
|
|
||||||
.IsRequired()
|
|
||||||
.HasColumnType("text");
|
|
||||||
|
|
||||||
b.Property<Instant>("StartedAt")
|
|
||||||
.HasColumnType("timestamp with time zone");
|
|
||||||
|
|
||||||
b.HasKey("CorrelationId");
|
|
||||||
|
|
||||||
b.HasIndex("CurrentState");
|
|
||||||
|
|
||||||
b.HasIndex("NovelUrl");
|
|
||||||
|
|
||||||
b.ToTable("NovelImportSagaStates");
|
|
||||||
});
|
|
||||||
|
|
||||||
modelBuilder.Entity("NovelNovelTag", b =>
|
modelBuilder.Entity("NovelNovelTag", b =>
|
||||||
{
|
{
|
||||||
b.Property<long>("NovelsId")
|
b.Property<long>("NovelsId")
|
||||||
|
|||||||
@@ -1,10 +0,0 @@
|
|||||||
using NodaTime;
|
|
||||||
|
|
||||||
namespace FictionArchive.Service.NovelService.Models;
|
|
||||||
|
|
||||||
public class ActiveImport
|
|
||||||
{
|
|
||||||
public Guid ImportId { get; set; }
|
|
||||||
public required string NovelUrl { get; set; }
|
|
||||||
public Instant StartedAt { get; set; }
|
|
||||||
}
|
|
||||||
@@ -1,17 +1,15 @@
|
|||||||
using FictionArchive.Common.Extensions;
|
using FictionArchive.Common.Extensions;
|
||||||
using FictionArchive.Service.NovelService.Consumers;
|
|
||||||
using FictionArchive.Service.NovelService.GraphQL;
|
using FictionArchive.Service.NovelService.GraphQL;
|
||||||
using FictionArchive.Service.NovelService.Models.Configuration;
|
using FictionArchive.Service.NovelService.Models.Configuration;
|
||||||
using FictionArchive.Service.NovelService.Sagas;
|
|
||||||
using FictionArchive.Service.NovelService.Services;
|
using FictionArchive.Service.NovelService.Services;
|
||||||
|
using FictionArchive.Service.NovelService.Services.Consumers;
|
||||||
using FictionArchive.Service.NovelService.Services.SourceAdapters;
|
using FictionArchive.Service.NovelService.Services.SourceAdapters;
|
||||||
using FictionArchive.Service.NovelService.Services.SourceAdapters.Novelpia;
|
using FictionArchive.Service.NovelService.Services.SourceAdapters.Novelpia;
|
||||||
using FictionArchive.Service.Shared;
|
using FictionArchive.Service.Shared;
|
||||||
using FictionArchive.Service.Shared.Extensions;
|
using FictionArchive.Service.Shared.Extensions;
|
||||||
|
using FictionArchive.Service.Shared.MassTransit;
|
||||||
using FictionArchive.Service.Shared.Services.GraphQL;
|
using FictionArchive.Service.Shared.Services.GraphQL;
|
||||||
using MassTransit;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using NodaTime;
|
|
||||||
|
|
||||||
namespace FictionArchive.Service.NovelService;
|
namespace FictionArchive.Service.NovelService;
|
||||||
|
|
||||||
@@ -30,23 +28,14 @@ public class Program
|
|||||||
|
|
||||||
if (!isSchemaExport)
|
if (!isSchemaExport)
|
||||||
{
|
{
|
||||||
builder.Services.AddFictionArchiveMassTransit(
|
builder.Services.AddFictionArchiveMassTransit<NovelServiceDbContext>(
|
||||||
builder.Configuration,
|
builder.Configuration,
|
||||||
x =>
|
cfg =>
|
||||||
{
|
{
|
||||||
x.AddConsumer<TranslationRequestCompletedConsumer>();
|
cfg.AddConsumer<ImportNovelCommandConsumer>();
|
||||||
x.AddConsumer<FileUploadRequestStatusUpdateConsumer>();
|
cfg.AddConsumer<PullChapterContentCommandConsumer>();
|
||||||
x.AddConsumer<ChapterPullRequestedConsumer>();
|
cfg.AddConsumer<TranslationCompletedEventConsumer>();
|
||||||
x.AddConsumer<NovelImportRequestedConsumer>();
|
cfg.AddConsumer<FileUploadCompletedEventConsumer>();
|
||||||
x.AddConsumer<NovelImportCompletedConsumer>();
|
|
||||||
|
|
||||||
x.AddSagaStateMachine<NovelImportSaga, NovelImportSagaState>()
|
|
||||||
.EntityFrameworkRepository(r =>
|
|
||||||
{
|
|
||||||
r.ConcurrencyMode = ConcurrencyMode.Optimistic;
|
|
||||||
r.ExistingDbContext<NovelServiceDbContext>();
|
|
||||||
r.UsePostgres();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,9 +76,6 @@ public class Program
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
// Register IClock for saga and service use
|
|
||||||
builder.Services.AddSingleton<IClock>(SystemClock.Instance);
|
|
||||||
|
|
||||||
builder.Services.AddHealthChecks();
|
builder.Services.AddHealthChecks();
|
||||||
|
|
||||||
// Authentication & Authorization
|
// Authentication & Authorization
|
||||||
|
|||||||
@@ -1,135 +0,0 @@
|
|||||||
using FictionArchive.Service.Shared.Contracts.Events;
|
|
||||||
using MassTransit;
|
|
||||||
using NodaTime;
|
|
||||||
|
|
||||||
namespace FictionArchive.Service.NovelService.Sagas;
|
|
||||||
|
|
||||||
public class NovelImportSaga : MassTransitStateMachine<NovelImportSagaState>
|
|
||||||
{
|
|
||||||
public State Importing { get; private set; } = null!;
|
|
||||||
public State Processing { get; private set; } = null!;
|
|
||||||
public State Completed { get; private set; } = null!;
|
|
||||||
public State Failed { get; private set; } = null!;
|
|
||||||
|
|
||||||
public Event<INovelImportRequested> NovelImportRequested { get; private set; } = null!;
|
|
||||||
public Event<INovelMetadataImported> NovelMetadataImported { get; private set; } = null!;
|
|
||||||
public Event<IChapterPullCompleted> ChapterPullCompleted { get; private set; } = null!;
|
|
||||||
public Event<IFileUploadRequestStatusUpdate> FileUploadStatusUpdate { get; private set; } = null!;
|
|
||||||
public Event<Fault<IChapterPullRequested>> ChapterPullFaulted { get; private set; } = null!;
|
|
||||||
public Event<Fault<IFileUploadRequestCreated>> FileUploadFaulted { get; private set; } = null!;
|
|
||||||
|
|
||||||
private readonly IClock _clock;
|
|
||||||
|
|
||||||
public NovelImportSaga(IClock clock)
|
|
||||||
{
|
|
||||||
_clock = clock;
|
|
||||||
|
|
||||||
InstanceState(x => x.CurrentState);
|
|
||||||
|
|
||||||
Event(() => NovelImportRequested, x => x.CorrelateById(ctx => ctx.Message.ImportId));
|
|
||||||
Event(() => NovelMetadataImported, x => x.CorrelateById(ctx => ctx.Message.ImportId));
|
|
||||||
Event(() => ChapterPullCompleted, x => x.CorrelateById(ctx => ctx.Message.ImportId));
|
|
||||||
Event(() => FileUploadStatusUpdate, x =>
|
|
||||||
{
|
|
||||||
x.CorrelateById(ctx => ctx.Message.ImportId ?? Guid.Empty);
|
|
||||||
x.OnMissingInstance(m => m.Discard());
|
|
||||||
});
|
|
||||||
Event(() => ChapterPullFaulted, x => x.CorrelateById(ctx => ctx.Message.Message.ImportId));
|
|
||||||
Event(() => FileUploadFaulted, x =>
|
|
||||||
{
|
|
||||||
x.CorrelateById(ctx => ctx.Message.Message.ImportId ?? Guid.Empty);
|
|
||||||
x.OnMissingInstance(m => m.Discard());
|
|
||||||
});
|
|
||||||
|
|
||||||
Initially(
|
|
||||||
When(NovelImportRequested)
|
|
||||||
.Then(ctx =>
|
|
||||||
{
|
|
||||||
ctx.Saga.NovelUrl = ctx.Message.NovelUrl;
|
|
||||||
ctx.Saga.StartedAt = _clock.GetCurrentInstant();
|
|
||||||
})
|
|
||||||
.TransitionTo(Importing)
|
|
||||||
);
|
|
||||||
|
|
||||||
During(Importing,
|
|
||||||
When(NovelMetadataImported)
|
|
||||||
.Then(ctx =>
|
|
||||||
{
|
|
||||||
ctx.Saga.NovelId = ctx.Message.NovelId;
|
|
||||||
ctx.Saga.ExpectedChapters = ctx.Message.ChaptersPendingPull;
|
|
||||||
})
|
|
||||||
.IfElse(
|
|
||||||
ctx => ctx.Saga.ExpectedChapters == 0,
|
|
||||||
thenBinder => thenBinder
|
|
||||||
.Then(ctx => ctx.Saga.CompletedAt = _clock.GetCurrentInstant())
|
|
||||||
.TransitionTo(Completed)
|
|
||||||
.PublishAsync(ctx => ctx.Init<INovelImportCompleted>(new NovelImportCompleted(
|
|
||||||
ctx.Saga.CorrelationId,
|
|
||||||
ctx.Saga.NovelId,
|
|
||||||
true,
|
|
||||||
null))),
|
|
||||||
elseBinder => elseBinder.TransitionTo(Processing)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
During(Processing,
|
|
||||||
When(ChapterPullCompleted)
|
|
||||||
.Then(ctx =>
|
|
||||||
{
|
|
||||||
ctx.Saga.CompletedChapters++;
|
|
||||||
ctx.Saga.ExpectedImages += ctx.Message.ImagesQueued;
|
|
||||||
})
|
|
||||||
.If(ctx => IsComplete(ctx.Saga), ctx => ctx
|
|
||||||
.Then(c => c.Saga.CompletedAt = _clock.GetCurrentInstant())
|
|
||||||
.TransitionTo(Completed)
|
|
||||||
.PublishAsync(c => c.Init<INovelImportCompleted>(new NovelImportCompleted(
|
|
||||||
c.Saga.CorrelationId,
|
|
||||||
c.Saga.NovelId,
|
|
||||||
true,
|
|
||||||
null)))),
|
|
||||||
|
|
||||||
When(FileUploadStatusUpdate)
|
|
||||||
.Then(ctx => ctx.Saga.CompletedImages++)
|
|
||||||
.If(ctx => IsComplete(ctx.Saga), ctx => ctx
|
|
||||||
.Then(c => c.Saga.CompletedAt = _clock.GetCurrentInstant())
|
|
||||||
.TransitionTo(Completed)
|
|
||||||
.PublishAsync(c => c.Init<INovelImportCompleted>(new NovelImportCompleted(
|
|
||||||
c.Saga.CorrelationId,
|
|
||||||
c.Saga.NovelId,
|
|
||||||
true,
|
|
||||||
null)))),
|
|
||||||
|
|
||||||
When(ChapterPullFaulted)
|
|
||||||
.Then(ctx =>
|
|
||||||
{
|
|
||||||
ctx.Saga.ErrorMessage = ctx.Message.Exceptions.FirstOrDefault()?.Message;
|
|
||||||
ctx.Saga.CompletedAt = _clock.GetCurrentInstant();
|
|
||||||
})
|
|
||||||
.TransitionTo(Failed)
|
|
||||||
.PublishAsync(ctx => ctx.Init<INovelImportCompleted>(new NovelImportCompleted(
|
|
||||||
ctx.Saga.CorrelationId,
|
|
||||||
ctx.Saga.NovelId,
|
|
||||||
false,
|
|
||||||
ctx.Saga.ErrorMessage))),
|
|
||||||
|
|
||||||
When(FileUploadFaulted)
|
|
||||||
.Then(ctx =>
|
|
||||||
{
|
|
||||||
ctx.Saga.ErrorMessage = ctx.Message.Exceptions.FirstOrDefault()?.Message;
|
|
||||||
ctx.Saga.CompletedAt = _clock.GetCurrentInstant();
|
|
||||||
})
|
|
||||||
.TransitionTo(Failed)
|
|
||||||
.PublishAsync(ctx => ctx.Init<INovelImportCompleted>(new NovelImportCompleted(
|
|
||||||
ctx.Saga.CorrelationId,
|
|
||||||
ctx.Saga.NovelId,
|
|
||||||
false,
|
|
||||||
ctx.Saga.ErrorMessage)))
|
|
||||||
);
|
|
||||||
|
|
||||||
SetCompletedWhenFinalized();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool IsComplete(NovelImportSagaState saga) =>
|
|
||||||
saga.CompletedChapters >= saga.ExpectedChapters &&
|
|
||||||
saga.CompletedImages >= saga.ExpectedImages;
|
|
||||||
}
|
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
using MassTransit;
|
|
||||||
using NodaTime;
|
|
||||||
|
|
||||||
namespace FictionArchive.Service.NovelService.Sagas;
|
|
||||||
|
|
||||||
public class NovelImportSagaState : SagaStateMachineInstance
|
|
||||||
{
|
|
||||||
public Guid CorrelationId { get; set; }
|
|
||||||
public string CurrentState { get; set; } = null!;
|
|
||||||
|
|
||||||
// Identity
|
|
||||||
public string NovelUrl { get; set; } = null!;
|
|
||||||
public uint? NovelId { get; set; }
|
|
||||||
|
|
||||||
// Chapter tracking
|
|
||||||
public int ExpectedChapters { get; set; }
|
|
||||||
public int CompletedChapters { get; set; }
|
|
||||||
|
|
||||||
// Image tracking
|
|
||||||
public int ExpectedImages { get; set; }
|
|
||||||
public int CompletedImages { get; set; }
|
|
||||||
|
|
||||||
// Timestamps
|
|
||||||
public Instant StartedAt { get; set; }
|
|
||||||
public Instant? CompletedAt { get; set; }
|
|
||||||
|
|
||||||
// Error info
|
|
||||||
public string? ErrorMessage { get; set; }
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
using FictionArchive.Common.Enums;
|
||||||
|
using FictionArchive.Service.Shared.MassTransit.Contracts.Events;
|
||||||
|
using MassTransit;
|
||||||
|
|
||||||
|
namespace FictionArchive.Service.NovelService.Services.Consumers;
|
||||||
|
|
||||||
|
public class FileUploadCompletedEventConsumer : IConsumer<FileUploadCompletedEvent>
|
||||||
|
{
|
||||||
|
private readonly ILogger<FileUploadCompletedEventConsumer> _logger;
|
||||||
|
private readonly NovelServiceDbContext _dbContext;
|
||||||
|
private readonly NovelUpdateService _novelUpdateService;
|
||||||
|
|
||||||
|
public FileUploadCompletedEventConsumer(
|
||||||
|
ILogger<FileUploadCompletedEventConsumer> logger,
|
||||||
|
NovelServiceDbContext dbContext,
|
||||||
|
NovelUpdateService novelUpdateService)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
_dbContext = dbContext;
|
||||||
|
_novelUpdateService = novelUpdateService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Consume(ConsumeContext<FileUploadCompletedEvent> context)
|
||||||
|
{
|
||||||
|
var @event = context.Message;
|
||||||
|
|
||||||
|
var image = await _dbContext.Images.FindAsync(@event.RequestId);
|
||||||
|
if (image == null)
|
||||||
|
{
|
||||||
|
// Not a request we care about.
|
||||||
|
_logger.LogDebug(
|
||||||
|
"FileUploadCompletedEvent received for unknown image: {RequestId}",
|
||||||
|
@event.RequestId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (@event.Status == RequestStatus.Failed)
|
||||||
|
{
|
||||||
|
_logger.LogError(
|
||||||
|
"Image upload failed for image with id {ImageId}: {ErrorMessage}",
|
||||||
|
image.Id, @event.ErrorMessage);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (@event.Status == RequestStatus.Success)
|
||||||
|
{
|
||||||
|
_logger.LogInformation(
|
||||||
|
"Image upload succeeded for image with id {ImageId}",
|
||||||
|
image.Id);
|
||||||
|
await _novelUpdateService.UpdateImage(image.Id, @event.FileAccessUrl!);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
using FictionArchive.Service.Shared.MassTransit.Contracts.Commands;
|
||||||
|
using MassTransit;
|
||||||
|
|
||||||
|
namespace FictionArchive.Service.NovelService.Services.Consumers;
|
||||||
|
|
||||||
|
public class ImportNovelCommandConsumer : IConsumer<ImportNovelCommand>
|
||||||
|
{
|
||||||
|
private readonly ILogger<ImportNovelCommandConsumer> _logger;
|
||||||
|
private readonly NovelUpdateService _novelUpdateService;
|
||||||
|
|
||||||
|
public ImportNovelCommandConsumer(
|
||||||
|
ILogger<ImportNovelCommandConsumer> logger,
|
||||||
|
NovelUpdateService novelUpdateService)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
_novelUpdateService = novelUpdateService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Consume(ConsumeContext<ImportNovelCommand> context)
|
||||||
|
{
|
||||||
|
var command = context.Message;
|
||||||
|
_logger.LogInformation("Processing ImportNovelCommand for URL: {NovelUrl}", command.NovelUrl);
|
||||||
|
|
||||||
|
await _novelUpdateService.ImportNovel(command.NovelUrl);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
using FictionArchive.Service.Shared.MassTransit.Contracts.Commands;
|
||||||
|
using MassTransit;
|
||||||
|
|
||||||
|
namespace FictionArchive.Service.NovelService.Services.Consumers;
|
||||||
|
|
||||||
|
public class PullChapterContentCommandConsumer : IConsumer<PullChapterContentCommand>
|
||||||
|
{
|
||||||
|
private readonly ILogger<PullChapterContentCommandConsumer> _logger;
|
||||||
|
private readonly NovelUpdateService _novelUpdateService;
|
||||||
|
|
||||||
|
public PullChapterContentCommandConsumer(
|
||||||
|
ILogger<PullChapterContentCommandConsumer> logger,
|
||||||
|
NovelUpdateService novelUpdateService)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
_novelUpdateService = novelUpdateService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Consume(ConsumeContext<PullChapterContentCommand> context)
|
||||||
|
{
|
||||||
|
var command = context.Message;
|
||||||
|
_logger.LogInformation(
|
||||||
|
"Processing PullChapterContentCommand for Novel: {NovelId}, Volume: {VolumeId}, Chapter: {ChapterOrder}",
|
||||||
|
command.NovelId, command.VolumeId, command.ChapterOrder);
|
||||||
|
|
||||||
|
await _novelUpdateService.PullChapterContents(command.NovelId, command.VolumeId, command.ChapterOrder);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
using FictionArchive.Service.NovelService.Models.Localization;
|
||||||
|
using FictionArchive.Service.Shared.MassTransit.Contracts.Events;
|
||||||
|
using MassTransit;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace FictionArchive.Service.NovelService.Services.Consumers;
|
||||||
|
|
||||||
|
public class TranslationCompletedEventConsumer : IConsumer<TranslationCompletedEvent>
|
||||||
|
{
|
||||||
|
private readonly ILogger<TranslationCompletedEventConsumer> _logger;
|
||||||
|
private readonly NovelServiceDbContext _dbContext;
|
||||||
|
|
||||||
|
public TranslationCompletedEventConsumer(
|
||||||
|
ILogger<TranslationCompletedEventConsumer> logger,
|
||||||
|
NovelServiceDbContext dbContext)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
_dbContext = dbContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Consume(ConsumeContext<TranslationCompletedEvent> context)
|
||||||
|
{
|
||||||
|
var @event = context.Message;
|
||||||
|
|
||||||
|
var localizationRequest = await _dbContext.LocalizationRequests
|
||||||
|
.Include(r => r.KeyRequestedForTranslation)
|
||||||
|
.ThenInclude(lk => lk.Texts)
|
||||||
|
.FirstOrDefaultAsync(lk => lk.Id == @event.TranslationRequestId);
|
||||||
|
|
||||||
|
if (localizationRequest == null)
|
||||||
|
{
|
||||||
|
// Not one of our requests, discard it
|
||||||
|
_logger.LogDebug(
|
||||||
|
"TranslationCompletedEvent received for unknown request: {RequestId}",
|
||||||
|
@event.TranslationRequestId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
localizationRequest.KeyRequestedForTranslation.Texts.Add(new LocalizationText()
|
||||||
|
{
|
||||||
|
Language = localizationRequest.TranslateTo,
|
||||||
|
Text = @event.TranslatedText,
|
||||||
|
TranslationEngine = localizationRequest.Engine
|
||||||
|
});
|
||||||
|
_dbContext.LocalizationRequests.Remove(localizationRequest);
|
||||||
|
await _dbContext.SaveChangesAsync();
|
||||||
|
|
||||||
|
_logger.LogInformation(
|
||||||
|
"Completed translation for request: {RequestId}",
|
||||||
|
@event.TranslationRequestId);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,6 @@
|
|||||||
using FictionArchive.Service.NovelService.Models;
|
|
||||||
using FictionArchive.Service.NovelService.Models.Images;
|
using FictionArchive.Service.NovelService.Models.Images;
|
||||||
using FictionArchive.Service.NovelService.Models.Localization;
|
using FictionArchive.Service.NovelService.Models.Localization;
|
||||||
using FictionArchive.Service.NovelService.Models.Novels;
|
using FictionArchive.Service.NovelService.Models.Novels;
|
||||||
using FictionArchive.Service.NovelService.Sagas;
|
|
||||||
using FictionArchive.Service.Shared.Services.Database;
|
using FictionArchive.Service.Shared.Services.Database;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
@@ -20,8 +18,6 @@ public class NovelServiceDbContext(DbContextOptions options, ILogger<NovelServic
|
|||||||
public DbSet<LocalizationKey> LocalizationKeys { get; set; }
|
public DbSet<LocalizationKey> LocalizationKeys { get; set; }
|
||||||
public DbSet<LocalizationRequest> LocalizationRequests { get; set; }
|
public DbSet<LocalizationRequest> LocalizationRequests { get; set; }
|
||||||
public DbSet<Image> Images { get; set; }
|
public DbSet<Image> Images { get; set; }
|
||||||
public DbSet<ActiveImport> ActiveImports { get; set; }
|
|
||||||
public DbSet<NovelImportSagaState> NovelImportSagaStates { get; set; }
|
|
||||||
|
|
||||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||||
{
|
{
|
||||||
@@ -40,18 +36,5 @@ public class NovelServiceDbContext(DbContextOptions options, ILogger<NovelServic
|
|||||||
modelBuilder.Entity<Chapter>()
|
modelBuilder.Entity<Chapter>()
|
||||||
.HasIndex("VolumeId", "Order")
|
.HasIndex("VolumeId", "Order")
|
||||||
.IsUnique();
|
.IsUnique();
|
||||||
|
|
||||||
modelBuilder.Entity<ActiveImport>(entity =>
|
|
||||||
{
|
|
||||||
entity.HasKey(e => e.ImportId);
|
|
||||||
entity.HasIndex(e => e.NovelUrl).IsUnique();
|
|
||||||
});
|
|
||||||
|
|
||||||
modelBuilder.Entity<NovelImportSagaState>(entity =>
|
|
||||||
{
|
|
||||||
entity.HasKey(e => e.CorrelationId);
|
|
||||||
entity.HasIndex(e => e.NovelUrl);
|
|
||||||
entity.HasIndex(e => e.CurrentState);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,4 @@
|
|||||||
using FictionArchive.Common.Enums;
|
using FictionArchive.Common.Enums;
|
||||||
using FictionArchive.Service.NovelService.Contracts;
|
|
||||||
using FictionArchive.Service.NovelService.Models;
|
|
||||||
using FictionArchive.Service.NovelService.Models.Configuration;
|
using FictionArchive.Service.NovelService.Models.Configuration;
|
||||||
using FictionArchive.Service.NovelService.Models.Enums;
|
using FictionArchive.Service.NovelService.Models.Enums;
|
||||||
using FictionArchive.Service.NovelService.Models.Images;
|
using FictionArchive.Service.NovelService.Models.Images;
|
||||||
@@ -8,12 +6,12 @@ using FictionArchive.Service.NovelService.Models.Localization;
|
|||||||
using FictionArchive.Service.NovelService.Models.Novels;
|
using FictionArchive.Service.NovelService.Models.Novels;
|
||||||
using FictionArchive.Service.NovelService.Models.SourceAdapters;
|
using FictionArchive.Service.NovelService.Models.SourceAdapters;
|
||||||
using FictionArchive.Service.NovelService.Services.SourceAdapters;
|
using FictionArchive.Service.NovelService.Services.SourceAdapters;
|
||||||
using FictionArchive.Service.Shared.Contracts.Events;
|
using FictionArchive.Service.Shared.MassTransit.Contracts.Commands;
|
||||||
|
using FictionArchive.Service.Shared.MassTransit.Contracts.Events;
|
||||||
using HtmlAgilityPack;
|
using HtmlAgilityPack;
|
||||||
using MassTransit;
|
using MassTransit;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using NodaTime;
|
|
||||||
|
|
||||||
namespace FictionArchive.Service.NovelService.Services;
|
namespace FictionArchive.Service.NovelService.Services;
|
||||||
|
|
||||||
@@ -23,17 +21,23 @@ public class NovelUpdateService
|
|||||||
private readonly ILogger<NovelUpdateService> _logger;
|
private readonly ILogger<NovelUpdateService> _logger;
|
||||||
private readonly IEnumerable<ISourceAdapter> _sourceAdapters;
|
private readonly IEnumerable<ISourceAdapter> _sourceAdapters;
|
||||||
private readonly IPublishEndpoint _publishEndpoint;
|
private readonly IPublishEndpoint _publishEndpoint;
|
||||||
|
private readonly ISendEndpointProvider _sendEndpointProvider;
|
||||||
private readonly NovelUpdateServiceConfiguration _novelUpdateServiceConfiguration;
|
private readonly NovelUpdateServiceConfiguration _novelUpdateServiceConfiguration;
|
||||||
private readonly IClock _clock;
|
|
||||||
|
|
||||||
public NovelUpdateService(NovelServiceDbContext dbContext, ILogger<NovelUpdateService> logger, IEnumerable<ISourceAdapter> sourceAdapters, IPublishEndpoint publishEndpoint, IOptions<NovelUpdateServiceConfiguration> novelUpdateServiceConfiguration, IClock clock)
|
public NovelUpdateService(
|
||||||
|
NovelServiceDbContext dbContext,
|
||||||
|
ILogger<NovelUpdateService> logger,
|
||||||
|
IEnumerable<ISourceAdapter> sourceAdapters,
|
||||||
|
IPublishEndpoint publishEndpoint,
|
||||||
|
ISendEndpointProvider sendEndpointProvider,
|
||||||
|
IOptions<NovelUpdateServiceConfiguration> novelUpdateServiceConfiguration)
|
||||||
{
|
{
|
||||||
_dbContext = dbContext;
|
_dbContext = dbContext;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_sourceAdapters = sourceAdapters;
|
_sourceAdapters = sourceAdapters;
|
||||||
_publishEndpoint = publishEndpoint;
|
_publishEndpoint = publishEndpoint;
|
||||||
|
_sendEndpointProvider = sendEndpointProvider;
|
||||||
_novelUpdateServiceConfiguration = novelUpdateServiceConfiguration.Value;
|
_novelUpdateServiceConfiguration = novelUpdateServiceConfiguration.Value;
|
||||||
_clock = clock;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#region Helper Methods
|
#region Helper Methods
|
||||||
@@ -303,7 +307,7 @@ public class NovelUpdateService
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
public async Task<Novel> ImportNovel(Guid importId, string novelUrl)
|
public async Task<Novel> ImportNovel(string novelUrl)
|
||||||
{
|
{
|
||||||
// Step 1: Get metadata from source adapter
|
// Step 1: Get metadata from source adapter
|
||||||
NovelMetadata? metadata = null;
|
NovelMetadata? metadata = null;
|
||||||
@@ -339,8 +343,7 @@ public class NovelUpdateService
|
|||||||
.ThenInclude(volume => volume.Chapters)
|
.ThenInclude(volume => volume.Chapters)
|
||||||
.ThenInclude(chapter => chapter.Body)
|
.ThenInclude(chapter => chapter.Body)
|
||||||
.ThenInclude(localizationKey => localizationKey.Texts)
|
.ThenInclude(localizationKey => localizationKey.Texts)
|
||||||
.Include(n => n.CoverImage).Include(novel => novel.Volumes).ThenInclude(volume => volume.Chapters)
|
.Include(n => n.CoverImage)
|
||||||
.ThenInclude(chapter => chapter.Name)
|
|
||||||
.FirstOrDefaultAsync(n =>
|
.FirstOrDefaultAsync(n =>
|
||||||
n.ExternalId == metadata.ExternalId &&
|
n.ExternalId == metadata.ExternalId &&
|
||||||
n.Source.Key == metadata.SourceDescriptor.Key);
|
n.Source.Key == metadata.SourceDescriptor.Key);
|
||||||
@@ -398,12 +401,14 @@ public class NovelUpdateService
|
|||||||
// Publish novel created event for new novels
|
// Publish novel created event for new novels
|
||||||
if (existingNovel == null)
|
if (existingNovel == null)
|
||||||
{
|
{
|
||||||
await _publishEndpoint.Publish<INovelCreated>(new NovelCreated(
|
await _publishEndpoint.Publish(new NovelCreatedEvent
|
||||||
novel.Id,
|
{
|
||||||
novel.Name.Texts.First(t => t.Language == novel.RawLanguage).Text,
|
NovelId = novel.Id,
|
||||||
novel.RawLanguage,
|
Title = novel.Name.Texts.First(t => t.Language == novel.RawLanguage).Text,
|
||||||
novel.Source.Key,
|
OriginalLanguage = novel.RawLanguage,
|
||||||
novel.Author.Name.Texts.First(t => t.Language == novel.RawLanguage).Text));
|
Source = novel.Source.Key,
|
||||||
|
AuthorName = novel.Author.Name.Texts.First(t => t.Language == novel.RawLanguage).Text
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Publish chapter created events for new chapters
|
// Publish chapter created events for new chapters
|
||||||
@@ -411,60 +416,53 @@ public class NovelUpdateService
|
|||||||
{
|
{
|
||||||
foreach (var chapter in volume.Chapters.Where(c => !existingChapterIds.Contains(c.Id)))
|
foreach (var chapter in volume.Chapters.Where(c => !existingChapterIds.Contains(c.Id)))
|
||||||
{
|
{
|
||||||
await _publishEndpoint.Publish<IChapterCreated>(new ChapterCreated(
|
await _publishEndpoint.Publish(new ChapterCreatedEvent
|
||||||
chapter.Id,
|
{
|
||||||
novel.Id,
|
ChapterId = chapter.Id,
|
||||||
volume.Id,
|
NovelId = novel.Id,
|
||||||
(uint)volume.Order,
|
VolumeId = volume.Id,
|
||||||
chapter.Order,
|
VolumeOrder = volume.Order,
|
||||||
chapter.Name.Texts.First(t => t.Language == novel.RawLanguage).Text));
|
ChapterOrder = chapter.Order,
|
||||||
|
ChapterTitle = chapter.Name.Texts.First(t => t.Language == novel.RawLanguage).Text
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Count chapters that need pulling
|
// Send cover image upload command if needed
|
||||||
var chaptersNeedingPull = novel.Volumes
|
|
||||||
.SelectMany(v => v.Chapters)
|
|
||||||
.Where(c => c.Body?.Texts == null || !c.Body.Texts.Any())
|
|
||||||
.ToList();
|
|
||||||
|
|
||||||
// Publish metadata imported event for saga
|
|
||||||
await _publishEndpoint.Publish<INovelMetadataImported>(new NovelMetadataImported(
|
|
||||||
importId,
|
|
||||||
novel.Id,
|
|
||||||
chaptersNeedingPull.Count
|
|
||||||
));
|
|
||||||
|
|
||||||
// Publish cover image event if needed
|
|
||||||
if (shouldPublishCoverEvent && novel.CoverImage != null && metadata.CoverImage != null)
|
if (shouldPublishCoverEvent && novel.CoverImage != null && metadata.CoverImage != null)
|
||||||
{
|
{
|
||||||
await _publishEndpoint.Publish<IFileUploadRequestCreated>(new FileUploadRequestCreated(
|
var uploadEndpoint = await _sendEndpointProvider.GetSendEndpoint(new Uri("queue:upload-file-command"));
|
||||||
importId,
|
await uploadEndpoint.Send(new UploadFileCommand
|
||||||
novel.CoverImage.Id,
|
{
|
||||||
$"Novels/{novel.Id}/Images/cover.jpg",
|
RequestId = novel.CoverImage.Id,
|
||||||
metadata.CoverImage.Data));
|
FileData = metadata.CoverImage.Data,
|
||||||
|
FilePath = $"Novels/{novel.Id}/Images/cover.jpg"
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Publish chapter pull events for chapters without body content
|
// Send chapter pull commands for chapters without body content
|
||||||
|
var pullChapterEndpoint = await _sendEndpointProvider.GetSendEndpoint(new Uri("queue:pull-chapter-content-command"));
|
||||||
foreach (var volume in novel.Volumes)
|
foreach (var volume in novel.Volumes)
|
||||||
{
|
{
|
||||||
var volumeChaptersNeedingPull = volume.Chapters
|
var chaptersNeedingPull = volume.Chapters
|
||||||
.Where(c => c.Body?.Texts == null || !c.Body.Texts.Any())
|
.Where(c => c.Body?.Texts == null || !c.Body.Texts.Any())
|
||||||
.ToList();
|
.ToList();
|
||||||
|
|
||||||
foreach (var chapter in volumeChaptersNeedingPull)
|
foreach (var chapter in chaptersNeedingPull)
|
||||||
{
|
{
|
||||||
await _publishEndpoint.Publish<IChapterPullRequested>(new ChapterPullRequested(
|
await pullChapterEndpoint.Send(new PullChapterContentCommand
|
||||||
importId,
|
{
|
||||||
novel.Id,
|
NovelId = novel.Id,
|
||||||
volume.Id,
|
VolumeId = volume.Id,
|
||||||
chapter.Order));
|
ChapterOrder = chapter.Order
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return novel;
|
return novel;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<(Chapter chapter, int imageCount)> PullChapterContents(Guid importId, uint novelId, uint volumeId, uint chapterOrder)
|
public async Task<Chapter> PullChapterContents(uint novelId, uint volumeId, uint chapterOrder)
|
||||||
{
|
{
|
||||||
var novel = await _dbContext.Novels.Where(novel => novel.Id == novelId)
|
var novel = await _dbContext.Novels.Where(novel => novel.Id == novelId)
|
||||||
.Include(novel => novel.Volumes)
|
.Include(novel => novel.Volumes)
|
||||||
@@ -525,19 +523,21 @@ public class NovelUpdateService
|
|||||||
localizationText.Text = chapterDoc.DocumentNode.OuterHtml;
|
localizationText.Text = chapterDoc.DocumentNode.OuterHtml;
|
||||||
await _dbContext.SaveChangesAsync();
|
await _dbContext.SaveChangesAsync();
|
||||||
|
|
||||||
// Body was updated, raise image request
|
// Body was updated, send upload commands for images
|
||||||
|
var uploadEndpoint = await _sendEndpointProvider.GetSendEndpoint(new Uri("queue:upload-file-command"));
|
||||||
int imgCount = 0;
|
int imgCount = 0;
|
||||||
foreach (var image in chapter.Images)
|
foreach (var image in chapter.Images)
|
||||||
{
|
{
|
||||||
var data = rawChapter.ImageData.FirstOrDefault(img => img.Url == image.OriginalPath);
|
var data = rawChapter.ImageData.FirstOrDefault(img => img.Url == image.OriginalPath);
|
||||||
await _publishEndpoint.Publish<IFileUploadRequestCreated>(new FileUploadRequestCreated(
|
await uploadEndpoint.Send(new UploadFileCommand
|
||||||
importId,
|
{
|
||||||
image.Id,
|
FileData = data.Data,
|
||||||
$"Novels/{novel.Id}/Images/Chapter-{chapter.Id}/{imgCount++}.jpg",
|
FilePath = $"{novel.Id}/Images/Chapter-{chapter.Id}/{imgCount++}.jpg",
|
||||||
data.Data));
|
RequestId = image.Id
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return (chapter, chapter.Images.Count);
|
return chapter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task UpdateImage(Guid imageId, string newUrl)
|
public async Task UpdateImage(Guid imageId, string newUrl)
|
||||||
@@ -568,36 +568,28 @@ public class NovelUpdateService
|
|||||||
await _dbContext.SaveChangesAsync();
|
await _dbContext.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<NovelImportRequested> QueueNovelImport(string novelUrl)
|
public async Task<ImportNovelCommand> QueueNovelImport(string novelUrl)
|
||||||
{
|
{
|
||||||
var importId = Guid.NewGuid();
|
var command = new ImportNovelCommand
|
||||||
var activeImport = new ActiveImport
|
|
||||||
{
|
{
|
||||||
ImportId = importId,
|
NovelUrl = novelUrl
|
||||||
NovelUrl = novelUrl,
|
|
||||||
StartedAt = _clock.GetCurrentInstant()
|
|
||||||
};
|
};
|
||||||
|
var endpoint = await _sendEndpointProvider.GetSendEndpoint(new Uri("queue:import-novel-command"));
|
||||||
try
|
await endpoint.Send(command);
|
||||||
{
|
return command;
|
||||||
await _dbContext.ActiveImports.AddAsync(activeImport);
|
|
||||||
await _dbContext.SaveChangesAsync();
|
|
||||||
}
|
|
||||||
catch (DbUpdateException)
|
|
||||||
{
|
|
||||||
throw new InvalidOperationException($"An import is already in progress for {novelUrl}");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var importNovelRequestEvent = new NovelImportRequested(importId, novelUrl);
|
public async Task<PullChapterContentCommand> QueueChapterPull(uint novelId, uint volumeId, uint chapterOrder)
|
||||||
await _publishEndpoint.Publish<INovelImportRequested>(importNovelRequestEvent);
|
|
||||||
return importNovelRequestEvent;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<ChapterPullRequested> QueueChapterPull(Guid importId, uint novelId, uint volumeId, uint chapterOrder)
|
|
||||||
{
|
{
|
||||||
var chapterPullEvent = new ChapterPullRequested(importId, novelId, volumeId, chapterOrder);
|
var command = new PullChapterContentCommand
|
||||||
await _publishEndpoint.Publish<IChapterPullRequested>(chapterPullEvent);
|
{
|
||||||
return chapterPullEvent;
|
NovelId = novelId,
|
||||||
|
VolumeId = volumeId,
|
||||||
|
ChapterOrder = chapterOrder
|
||||||
|
};
|
||||||
|
var endpoint = await _sendEndpointProvider.GetSendEndpoint(new Uri("queue:pull-chapter-content-command"));
|
||||||
|
await endpoint.Send(command);
|
||||||
|
return command;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task DeleteNovel(uint novelId)
|
public async Task DeleteNovel(uint novelId)
|
||||||
|
|||||||
@@ -2,8 +2,7 @@
|
|||||||
"Logging": {
|
"Logging": {
|
||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
"Default": "Information",
|
"Default": "Information",
|
||||||
"Microsoft.AspNetCore": "Warning",
|
"Microsoft.AspNetCore": "Warning"
|
||||||
"Microsoft.EntityFrameworkCore": "Warning"
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"Novelpia": {
|
"Novelpia": {
|
||||||
@@ -17,8 +16,10 @@
|
|||||||
"DefaultConnection": "Host=localhost;Database=FictionArchive_NovelService;Username=postgres;password=postgres"
|
"DefaultConnection": "Host=localhost;Database=FictionArchive_NovelService;Username=postgres;password=postgres"
|
||||||
},
|
},
|
||||||
"RabbitMQ": {
|
"RabbitMQ": {
|
||||||
"ConnectionString": "amqp://localhost",
|
"Host": "localhost",
|
||||||
"ClientIdentifier": "NovelService"
|
"VirtualHost": "/",
|
||||||
|
"Username": "guest",
|
||||||
|
"Password": "guest"
|
||||||
},
|
},
|
||||||
"AllowedHosts": "*",
|
"AllowedHosts": "*",
|
||||||
"OIDC": {
|
"OIDC": {
|
||||||
|
|||||||
23
FictionArchive.Service.ReportingService/Dockerfile
Normal file
23
FictionArchive.Service.ReportingService/Dockerfile
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
|
||||||
|
USER $APP_UID
|
||||||
|
WORKDIR /app
|
||||||
|
EXPOSE 8080
|
||||||
|
EXPOSE 8081
|
||||||
|
|
||||||
|
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
|
||||||
|
ARG BUILD_CONFIGURATION=Release
|
||||||
|
WORKDIR /src
|
||||||
|
COPY ["FictionArchive.Service.ReportingService/FictionArchive.Service.ReportingService.csproj", "FictionArchive.Service.ReportingService/"]
|
||||||
|
RUN dotnet restore "FictionArchive.Service.ReportingService/FictionArchive.Service.ReportingService.csproj"
|
||||||
|
COPY . .
|
||||||
|
WORKDIR "/src/FictionArchive.Service.ReportingService"
|
||||||
|
RUN dotnet build "./FictionArchive.Service.ReportingService.csproj" -c $BUILD_CONFIGURATION -o /app/build
|
||||||
|
|
||||||
|
FROM build AS publish
|
||||||
|
ARG BUILD_CONFIGURATION=Release
|
||||||
|
RUN dotnet publish "./FictionArchive.Service.ReportingService.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
|
||||||
|
|
||||||
|
FROM base AS final
|
||||||
|
WORKDIR /app
|
||||||
|
COPY --from=publish /app/publish .
|
||||||
|
ENTRYPOINT ["dotnet", "FictionArchive.Service.ReportingService.dll"]
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.11">
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
</PackageReference>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\FictionArchive.Service.Shared\FictionArchive.Service.Shared.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
15
FictionArchive.Service.ReportingService/GraphQL/Mutation.cs
Normal file
15
FictionArchive.Service.ReportingService/GraphQL/Mutation.cs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
using HotChocolate;
|
||||||
|
using HotChocolate.Authorization;
|
||||||
|
|
||||||
|
namespace FictionArchive.Service.ReportingService.GraphQL;
|
||||||
|
|
||||||
|
public class Mutation
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Placeholder mutation for GraphQL schema requirements.
|
||||||
|
/// The ReportingService is primarily read-only, consuming events from other services.
|
||||||
|
/// </summary>
|
||||||
|
[Authorize(Roles = ["admin"])]
|
||||||
|
[GraphQLDescription("Placeholder mutation. ReportingService is primarily read-only.")]
|
||||||
|
public bool Ping() => true;
|
||||||
|
}
|
||||||
52
FictionArchive.Service.ReportingService/GraphQL/Query.cs
Normal file
52
FictionArchive.Service.ReportingService/GraphQL/Query.cs
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
using System.Text.Json;
|
||||||
|
using FictionArchive.Service.ReportingService.Models.Database;
|
||||||
|
using FictionArchive.Service.ReportingService.Models.DTOs;
|
||||||
|
using FictionArchive.Service.ReportingService.Services;
|
||||||
|
using HotChocolate;
|
||||||
|
using HotChocolate.Data;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace FictionArchive.Service.ReportingService.GraphQL;
|
||||||
|
|
||||||
|
public class Query
|
||||||
|
{
|
||||||
|
[UseProjection]
|
||||||
|
[UseFiltering]
|
||||||
|
[UseSorting]
|
||||||
|
[GraphQLName("reportingJobs")]
|
||||||
|
public IQueryable<Job> GetReportingJobs(ReportingServiceDbContext dbContext)
|
||||||
|
=> dbContext.Jobs.Include(j => j.History);
|
||||||
|
|
||||||
|
[GraphQLName("reportingJob")]
|
||||||
|
public async Task<JobDto?> GetReportingJob(Guid id, ReportingServiceDbContext dbContext)
|
||||||
|
{
|
||||||
|
var job = await dbContext.Jobs
|
||||||
|
.Include(j => j.History.OrderBy(h => h.Timestamp))
|
||||||
|
.FirstOrDefaultAsync(j => j.Id == id);
|
||||||
|
|
||||||
|
if (job == null) return null;
|
||||||
|
|
||||||
|
return new JobDto
|
||||||
|
{
|
||||||
|
Id = job.Id,
|
||||||
|
JobType = job.JobType,
|
||||||
|
Status = job.Status,
|
||||||
|
CurrentStep = job.CurrentStep,
|
||||||
|
ErrorMessage = job.ErrorMessage,
|
||||||
|
Metadata = job.Metadata != null
|
||||||
|
? JsonSerializer.Deserialize<Dictionary<string, object>>(job.Metadata.RootElement.GetRawText())
|
||||||
|
: null,
|
||||||
|
History = job.History.Select(h => new JobHistoryEntryDto
|
||||||
|
{
|
||||||
|
FromState = h.FromState,
|
||||||
|
ToState = h.ToState,
|
||||||
|
Message = h.Message,
|
||||||
|
Error = h.Error,
|
||||||
|
Timestamp = h.Timestamp
|
||||||
|
}).ToList(),
|
||||||
|
CreatedTime = job.CreatedTime,
|
||||||
|
UpdatedTime = job.UpdatedTime,
|
||||||
|
CompletedTime = job.CompletedTime
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
using NodaTime;
|
||||||
|
|
||||||
|
namespace FictionArchive.Service.ReportingService.Models.DTOs;
|
||||||
|
|
||||||
|
public class JobDto
|
||||||
|
{
|
||||||
|
public Guid Id { get; set; }
|
||||||
|
public required string JobType { get; set; }
|
||||||
|
public required string Status { get; set; }
|
||||||
|
public string? CurrentStep { get; set; }
|
||||||
|
public string? ErrorMessage { get; set; }
|
||||||
|
public Dictionary<string, object>? Metadata { get; set; }
|
||||||
|
public List<JobHistoryEntryDto> History { get; set; } = new();
|
||||||
|
public Instant CreatedTime { get; set; }
|
||||||
|
public Instant UpdatedTime { get; set; }
|
||||||
|
public Instant? CompletedTime { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
using NodaTime;
|
||||||
|
|
||||||
|
namespace FictionArchive.Service.ReportingService.Models.DTOs;
|
||||||
|
|
||||||
|
public class JobHistoryEntryDto
|
||||||
|
{
|
||||||
|
public required string FromState { get; set; }
|
||||||
|
public required string ToState { get; set; }
|
||||||
|
public string? Message { get; set; }
|
||||||
|
public string? Error { get; set; }
|
||||||
|
public Instant Timestamp { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
using System.Text.Json;
|
||||||
|
using NodaTime;
|
||||||
|
|
||||||
|
namespace FictionArchive.Service.ReportingService.Models.Database;
|
||||||
|
|
||||||
|
public class Job
|
||||||
|
{
|
||||||
|
public Guid Id { get; set; }
|
||||||
|
public required string JobType { get; set; }
|
||||||
|
public required string Status { get; set; }
|
||||||
|
public string? CurrentStep { get; set; }
|
||||||
|
public string? ErrorMessage { get; set; }
|
||||||
|
public JsonDocument? Metadata { get; set; }
|
||||||
|
public Instant CreatedTime { get; set; }
|
||||||
|
public Instant UpdatedTime { get; set; }
|
||||||
|
public Instant? CompletedTime { get; set; }
|
||||||
|
|
||||||
|
public ICollection<JobHistoryEntry> History { get; set; } = new List<JobHistoryEntry>();
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
using NodaTime;
|
||||||
|
|
||||||
|
namespace FictionArchive.Service.ReportingService.Models.Database;
|
||||||
|
|
||||||
|
public class JobHistoryEntry
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
public Guid JobId { get; set; }
|
||||||
|
public required string FromState { get; set; }
|
||||||
|
public required string ToState { get; set; }
|
||||||
|
public string? Message { get; set; }
|
||||||
|
public string? Error { get; set; }
|
||||||
|
public Instant Timestamp { get; set; }
|
||||||
|
|
||||||
|
public Job Job { get; set; } = null!;
|
||||||
|
}
|
||||||
76
FictionArchive.Service.ReportingService/Program.cs
Normal file
76
FictionArchive.Service.ReportingService/Program.cs
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
using FictionArchive.Common.Extensions;
|
||||||
|
using FictionArchive.Service.ReportingService.GraphQL;
|
||||||
|
using FictionArchive.Service.ReportingService.Services;
|
||||||
|
using FictionArchive.Service.ReportingService.Services.Consumers;
|
||||||
|
using FictionArchive.Service.Shared;
|
||||||
|
using FictionArchive.Service.Shared.Extensions;
|
||||||
|
using FictionArchive.Service.Shared.MassTransit;
|
||||||
|
|
||||||
|
namespace FictionArchive.Service.ReportingService;
|
||||||
|
|
||||||
|
public class Program
|
||||||
|
{
|
||||||
|
public static void Main(string[] args)
|
||||||
|
{
|
||||||
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
|
var isSchemaExport = SchemaExportDetector.IsSchemaExportMode(args);
|
||||||
|
|
||||||
|
builder.AddLocalAppsettings();
|
||||||
|
|
||||||
|
builder.Services.AddHealthChecks();
|
||||||
|
|
||||||
|
#region Database
|
||||||
|
|
||||||
|
builder.Services.RegisterDbContext<ReportingServiceDbContext>(
|
||||||
|
builder.Configuration.GetConnectionString("DefaultConnection")!,
|
||||||
|
skipInfrastructure: isSchemaExport);
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region MassTransit
|
||||||
|
|
||||||
|
if (!isSchemaExport)
|
||||||
|
{
|
||||||
|
builder.Services.AddFictionArchiveMassTransit<ReportingServiceDbContext>(
|
||||||
|
builder.Configuration,
|
||||||
|
x =>
|
||||||
|
{
|
||||||
|
x.AddConsumer<JobStateChangedEventConsumer>();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region GraphQL
|
||||||
|
|
||||||
|
builder.Services.AddDefaultGraphQl<Query, Mutation>()
|
||||||
|
.AddAuthorization();
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
// Authentication & Authorization
|
||||||
|
builder.Services.AddOidcAuthentication(builder.Configuration);
|
||||||
|
builder.Services.AddFictionArchiveAuthorization();
|
||||||
|
|
||||||
|
var app = builder.Build();
|
||||||
|
|
||||||
|
if (!isSchemaExport)
|
||||||
|
{
|
||||||
|
using var scope = app.Services.CreateScope();
|
||||||
|
var dbContext = scope.ServiceProvider.GetRequiredService<ReportingServiceDbContext>();
|
||||||
|
dbContext.UpdateDatabase();
|
||||||
|
}
|
||||||
|
|
||||||
|
app.UseHttpsRedirection();
|
||||||
|
|
||||||
|
app.MapHealthChecks("/healthz");
|
||||||
|
|
||||||
|
app.UseAuthentication();
|
||||||
|
app.UseAuthorization();
|
||||||
|
|
||||||
|
app.MapGraphQL();
|
||||||
|
|
||||||
|
app.RunWithGraphQLCommands(args);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
{
|
||||||
|
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||||
|
"iisSettings": {
|
||||||
|
"windowsAuthentication": false,
|
||||||
|
"anonymousAuthentication": true,
|
||||||
|
"iisExpress": {
|
||||||
|
"applicationUrl": "http://localhost:45320",
|
||||||
|
"sslPort": 44320
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"profiles": {
|
||||||
|
"http": {
|
||||||
|
"commandName": "Project",
|
||||||
|
"dotnetRunMessages": true,
|
||||||
|
"launchBrowser": true,
|
||||||
|
"applicationUrl": "http://localhost:5180",
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"https": {
|
||||||
|
"commandName": "Project",
|
||||||
|
"dotnetRunMessages": true,
|
||||||
|
"launchBrowser": true,
|
||||||
|
"launchUrl": "graphql",
|
||||||
|
"applicationUrl": "https://localhost:7320;http://localhost:5180",
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"IIS Express": {
|
||||||
|
"commandName": "IISExpress",
|
||||||
|
"launchBrowser": true,
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
using System.Text.Json;
|
||||||
|
using FictionArchive.Service.ReportingService.Models.Database;
|
||||||
|
using FictionArchive.Service.Shared.MassTransit.Contracts;
|
||||||
|
using MassTransit;
|
||||||
|
|
||||||
|
namespace FictionArchive.Service.ReportingService.Services.Consumers;
|
||||||
|
|
||||||
|
public class JobStateChangedEventConsumer : IConsumer<JobStateChangedEvent>
|
||||||
|
{
|
||||||
|
private readonly ReportingServiceDbContext _dbContext;
|
||||||
|
private readonly ILogger<JobStateChangedEventConsumer> _logger;
|
||||||
|
|
||||||
|
public JobStateChangedEventConsumer(
|
||||||
|
ReportingServiceDbContext dbContext,
|
||||||
|
ILogger<JobStateChangedEventConsumer> logger)
|
||||||
|
{
|
||||||
|
_dbContext = dbContext;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Consume(ConsumeContext<JobStateChangedEvent> context)
|
||||||
|
{
|
||||||
|
var @event = context.Message;
|
||||||
|
|
||||||
|
var job = await _dbContext.Jobs.FindAsync(@event.JobId);
|
||||||
|
|
||||||
|
if (job == null)
|
||||||
|
{
|
||||||
|
job = new Job
|
||||||
|
{
|
||||||
|
Id = @event.JobId,
|
||||||
|
JobType = @event.JobType,
|
||||||
|
Status = @event.ToState,
|
||||||
|
CreatedTime = @event.Timestamp,
|
||||||
|
UpdatedTime = @event.Timestamp,
|
||||||
|
Metadata = @event.Metadata != null
|
||||||
|
? JsonSerializer.SerializeToDocument(@event.Metadata)
|
||||||
|
: null
|
||||||
|
};
|
||||||
|
_dbContext.Jobs.Add(job);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
job.Status = @event.ToState;
|
||||||
|
job.UpdatedTime = @event.Timestamp;
|
||||||
|
|
||||||
|
if (@event.Error != null)
|
||||||
|
{
|
||||||
|
job.ErrorMessage = @event.Error;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (@event.ToState is "Completed" or "Failed")
|
||||||
|
{
|
||||||
|
job.CompletedTime = @event.Timestamp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var historyEntry = new JobHistoryEntry
|
||||||
|
{
|
||||||
|
JobId = @event.JobId,
|
||||||
|
FromState = @event.FromState,
|
||||||
|
ToState = @event.ToState,
|
||||||
|
Message = @event.Message,
|
||||||
|
Error = @event.Error,
|
||||||
|
Timestamp = @event.Timestamp
|
||||||
|
};
|
||||||
|
_dbContext.JobHistoryEntries.Add(historyEntry);
|
||||||
|
|
||||||
|
await _dbContext.SaveChangesAsync();
|
||||||
|
|
||||||
|
_logger.LogDebug("Recorded job state change: {JobId} {FromState} -> {ToState}",
|
||||||
|
@event.JobId, @event.FromState, @event.ToState);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
using FictionArchive.Service.ReportingService.Models.Database;
|
||||||
|
using FictionArchive.Service.Shared.Services.Database;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace FictionArchive.Service.ReportingService.Services;
|
||||||
|
|
||||||
|
public class ReportingServiceDbContext : FictionArchiveDbContext
|
||||||
|
{
|
||||||
|
public ReportingServiceDbContext(DbContextOptions options, ILogger<ReportingServiceDbContext> logger)
|
||||||
|
: base(options, logger) { }
|
||||||
|
|
||||||
|
public DbSet<Job> Jobs => Set<Job>();
|
||||||
|
public DbSet<JobHistoryEntry> JobHistoryEntries => Set<JobHistoryEntry>();
|
||||||
|
|
||||||
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
base.OnModelCreating(modelBuilder);
|
||||||
|
|
||||||
|
modelBuilder.Entity<Job>(entity =>
|
||||||
|
{
|
||||||
|
entity.HasKey(e => e.Id);
|
||||||
|
entity.HasIndex(e => e.JobType);
|
||||||
|
entity.HasIndex(e => e.Status);
|
||||||
|
entity.HasIndex(e => e.CreatedTime);
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity<JobHistoryEntry>(entity =>
|
||||||
|
{
|
||||||
|
entity.HasKey(e => e.Id);
|
||||||
|
entity.HasOne(e => e.Job)
|
||||||
|
.WithMany(j => j.History)
|
||||||
|
.HasForeignKey(e => e.JobId)
|
||||||
|
.OnDelete(DeleteBehavior.Cascade);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft.AspNetCore": "Warning"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
28
FictionArchive.Service.ReportingService/appsettings.json
Normal file
28
FictionArchive.Service.ReportingService/appsettings.json
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft.AspNetCore": "Warning"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"ConnectionStrings": {
|
||||||
|
"DefaultConnection": "Host=localhost;Database=FictionArchive_ReportingService;Username=postgres;password=postgres"
|
||||||
|
},
|
||||||
|
"RabbitMQ": {
|
||||||
|
"Host": "localhost",
|
||||||
|
"VirtualHost": "/",
|
||||||
|
"Username": "guest",
|
||||||
|
"Password": "guest"
|
||||||
|
},
|
||||||
|
"OIDC": {
|
||||||
|
"Authority": "https://auth.orfl.xyz/application/o/fiction-archive/",
|
||||||
|
"ClientId": "ldi5IpEidq2WW0Ka1lehVskb2SOBjnYRaZCpEyBh",
|
||||||
|
"Audience": "ldi5IpEidq2WW0Ka1lehVskb2SOBjnYRaZCpEyBh",
|
||||||
|
"ValidIssuer": "https://auth.orfl.xyz/application/o/fiction-archive/",
|
||||||
|
"ValidateIssuer": true,
|
||||||
|
"ValidateAudience": true,
|
||||||
|
"ValidateLifetime": true,
|
||||||
|
"ValidateIssuerSigningKey": true
|
||||||
|
},
|
||||||
|
"AllowedHosts": "*"
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"subgraph": "Reporting",
|
||||||
|
"http": {
|
||||||
|
"baseAddress": "http://localhost:5180/graphql"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="AppAny.Quartz.EntityFrameworkCore.Migrations.PostgreSQL" Version="0.5.1" />
|
<PackageReference Include="AppAny.Quartz.EntityFrameworkCore.Migrations.PostgreSQL" Version="0.5.1" />
|
||||||
|
<PackageReference Include="MassTransit" Version="8.4.0" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.11">
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.11">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
|||||||
@@ -6,15 +6,22 @@ namespace FictionArchive.Service.SchedulerService.Models.JobTemplates;
|
|||||||
|
|
||||||
public class EventJobTemplate : IJob
|
public class EventJobTemplate : IJob
|
||||||
{
|
{
|
||||||
private readonly IBus _bus;
|
private readonly IPublishEndpoint _publishEndpoint;
|
||||||
|
private readonly ISendEndpointProvider _sendEndpointProvider;
|
||||||
private readonly ILogger<EventJobTemplate> _logger;
|
private readonly ILogger<EventJobTemplate> _logger;
|
||||||
|
|
||||||
public const string EventTypeParameter = "RoutingKey";
|
public const string EventTypeParameter = "MessageType";
|
||||||
public const string EventDataParameter = "MessageData";
|
public const string EventDataParameter = "MessageData";
|
||||||
|
public const string IsCommandParameter = "IsCommand";
|
||||||
|
public const string DestinationQueueParameter = "DestinationQueue";
|
||||||
|
|
||||||
public EventJobTemplate(IBus bus, ILogger<EventJobTemplate> logger)
|
public EventJobTemplate(
|
||||||
|
IPublishEndpoint publishEndpoint,
|
||||||
|
ISendEndpointProvider sendEndpointProvider,
|
||||||
|
ILogger<EventJobTemplate> logger)
|
||||||
{
|
{
|
||||||
_bus = bus;
|
_publishEndpoint = publishEndpoint;
|
||||||
|
_sendEndpointProvider = sendEndpointProvider;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -22,14 +29,47 @@ public class EventJobTemplate : IJob
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var eventData = context.MergedJobDataMap.GetString(EventDataParameter);
|
var messageData = context.MergedJobDataMap.GetString(EventDataParameter);
|
||||||
var eventType = context.MergedJobDataMap.GetString(EventTypeParameter);
|
var messageTypeName = context.MergedJobDataMap.GetString(EventTypeParameter);
|
||||||
var eventObject = JsonConvert.DeserializeObject(eventData);
|
var isCommand = context.MergedJobDataMap.GetBoolean(IsCommandParameter);
|
||||||
await _bus.Publish(eventObject);
|
|
||||||
|
var messageType = Type.GetType(messageTypeName!);
|
||||||
|
if (messageType == null)
|
||||||
|
{
|
||||||
|
_logger.LogError("Could not resolve message type: {MessageType}", messageTypeName);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var message = JsonConvert.DeserializeObject(messageData!, messageType);
|
||||||
|
if (message == null)
|
||||||
|
{
|
||||||
|
_logger.LogError("Could not deserialize message data for type: {MessageType}", messageTypeName);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isCommand)
|
||||||
|
{
|
||||||
|
var destinationQueue = context.MergedJobDataMap.GetString(DestinationQueueParameter);
|
||||||
|
if (string.IsNullOrEmpty(destinationQueue))
|
||||||
|
{
|
||||||
|
_logger.LogError("Destination queue not specified for command message");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var endpoint = await _sendEndpointProvider.GetSendEndpoint(new Uri($"queue:{destinationQueue}"));
|
||||||
|
await endpoint.Send(message, messageType);
|
||||||
|
_logger.LogInformation("Sent command {MessageType} to queue {Queue}", messageTypeName, destinationQueue);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await _publishEndpoint.Publish(message, messageType);
|
||||||
|
_logger.LogInformation("Published event {MessageType}", messageTypeName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "An error occurred while running an event job.");
|
_logger.LogError(ex, "An error occurred while running an event job.");
|
||||||
|
throw; // Re-throw to let Quartz handle retries
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,7 @@ using FictionArchive.Service.SchedulerService.GraphQL;
|
|||||||
using FictionArchive.Service.SchedulerService.Services;
|
using FictionArchive.Service.SchedulerService.Services;
|
||||||
using FictionArchive.Service.Shared;
|
using FictionArchive.Service.Shared;
|
||||||
using FictionArchive.Service.Shared.Extensions;
|
using FictionArchive.Service.Shared.Extensions;
|
||||||
|
using FictionArchive.Service.Shared.MassTransit;
|
||||||
using Quartz;
|
using Quartz;
|
||||||
using Quartz.Impl.AdoJobStore;
|
using Quartz.Impl.AdoJobStore;
|
||||||
|
|
||||||
@@ -33,7 +34,7 @@ public class Program
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Event Bus
|
#region MassTransit
|
||||||
|
|
||||||
if (!isSchemaExport)
|
if (!isSchemaExport)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -2,13 +2,14 @@
|
|||||||
"Logging": {
|
"Logging": {
|
||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
"Default": "Information",
|
"Default": "Information",
|
||||||
"Microsoft.AspNetCore": "Warning",
|
"Microsoft.AspNetCore": "Warning"
|
||||||
"Microsoft.EntityFrameworkCore": "Warning"
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"RabbitMQ": {
|
"RabbitMQ": {
|
||||||
"ConnectionString": "amqp://localhost",
|
"Host": "localhost",
|
||||||
"ClientIdentifier": "SchedulerService"
|
"VirtualHost": "/",
|
||||||
|
"Username": "guest",
|
||||||
|
"Password": "guest"
|
||||||
},
|
},
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
"DefaultConnection": "Host=localhost;Database=FictionArchive_SchedulerService;Username=postgres;password=postgres"
|
"DefaultConnection": "Host=localhost;Database=FictionArchive_SchedulerService;Username=postgres;password=postgres"
|
||||||
|
|||||||
@@ -1,11 +0,0 @@
|
|||||||
namespace FictionArchive.Service.Shared.Contracts.Events;
|
|
||||||
|
|
||||||
public interface IChapterCreated
|
|
||||||
{
|
|
||||||
uint ChapterId { get; }
|
|
||||||
uint NovelId { get; }
|
|
||||||
uint VolumeId { get; }
|
|
||||||
uint VolumeOrder { get; }
|
|
||||||
uint ChapterOrder { get; }
|
|
||||||
string ChapterTitle { get; }
|
|
||||||
}
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
namespace FictionArchive.Service.Shared.Contracts.Events;
|
|
||||||
|
|
||||||
public interface IChapterPullCompleted
|
|
||||||
{
|
|
||||||
Guid ImportId { get; }
|
|
||||||
uint ChapterId { get; }
|
|
||||||
int ImagesQueued { get; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public record ChapterPullCompleted(Guid ImportId, uint ChapterId, int ImagesQueued) : IChapterPullCompleted;
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
namespace FictionArchive.Service.Shared.Contracts.Events;
|
|
||||||
|
|
||||||
public interface IChapterPullRequested
|
|
||||||
{
|
|
||||||
Guid ImportId { get; }
|
|
||||||
uint NovelId { get; }
|
|
||||||
uint VolumeId { get; }
|
|
||||||
uint ChapterOrder { get; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public record ChapterPullRequested(Guid ImportId, uint NovelId, uint VolumeId, uint ChapterOrder) : IChapterPullRequested;
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
namespace FictionArchive.Service.Shared.Contracts.Events;
|
|
||||||
|
|
||||||
public interface IFileUploadRequestCreated
|
|
||||||
{
|
|
||||||
Guid? ImportId { get; }
|
|
||||||
Guid RequestId { get; }
|
|
||||||
string FilePath { get; }
|
|
||||||
byte[] FileData { get; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public record FileUploadRequestCreated(Guid? ImportId, Guid RequestId, string FilePath, byte[] FileData) : IFileUploadRequestCreated;
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
using FictionArchive.Common.Enums;
|
|
||||||
|
|
||||||
namespace FictionArchive.Service.Shared.Contracts.Events;
|
|
||||||
|
|
||||||
public interface IFileUploadRequestStatusUpdate
|
|
||||||
{
|
|
||||||
Guid? ImportId { get; }
|
|
||||||
Guid RequestId { get; }
|
|
||||||
RequestStatus Status { get; }
|
|
||||||
string? FileAccessUrl { get; }
|
|
||||||
string? ErrorMessage { get; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public record FileUploadRequestStatusUpdate(Guid? ImportId, Guid RequestId, RequestStatus Status, string? FileAccessUrl, string? ErrorMessage) : IFileUploadRequestStatusUpdate;
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
using FictionArchive.Common.Enums;
|
|
||||||
|
|
||||||
namespace FictionArchive.Service.Shared.Contracts.Events;
|
|
||||||
|
|
||||||
public interface INovelCreated
|
|
||||||
{
|
|
||||||
uint NovelId { get; }
|
|
||||||
string Title { get; }
|
|
||||||
Language OriginalLanguage { get; }
|
|
||||||
string Source { get; }
|
|
||||||
string AuthorName { get; }
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
namespace FictionArchive.Service.Shared.Contracts.Events;
|
|
||||||
|
|
||||||
public interface INovelImportCompleted
|
|
||||||
{
|
|
||||||
Guid ImportId { get; }
|
|
||||||
uint? NovelId { get; }
|
|
||||||
bool Success { get; }
|
|
||||||
string? ErrorMessage { get; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public record NovelImportCompleted(Guid ImportId, uint? NovelId, bool Success, string? ErrorMessage) : INovelImportCompleted;
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
namespace FictionArchive.Service.Shared.Contracts.Events;
|
|
||||||
|
|
||||||
public interface INovelImportRequested
|
|
||||||
{
|
|
||||||
Guid ImportId { get; }
|
|
||||||
string NovelUrl { get; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public record NovelImportRequested(Guid ImportId, string NovelUrl) : INovelImportRequested;
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
namespace FictionArchive.Service.Shared.Contracts.Events;
|
|
||||||
|
|
||||||
public interface INovelMetadataImported
|
|
||||||
{
|
|
||||||
Guid ImportId { get; }
|
|
||||||
uint NovelId { get; }
|
|
||||||
int ChaptersPendingPull { get; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public record NovelMetadataImported(Guid ImportId, uint NovelId, int ChaptersPendingPull) : INovelMetadataImported;
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
namespace FictionArchive.Service.Shared.Contracts.Events;
|
|
||||||
|
|
||||||
public interface ITranslationRequestCompleted
|
|
||||||
{
|
|
||||||
Guid? TranslationRequestId { get; }
|
|
||||||
string? TranslatedText { get; }
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
using FictionArchive.Common.Enums;
|
|
||||||
|
|
||||||
namespace FictionArchive.Service.Shared.Contracts.Events;
|
|
||||||
|
|
||||||
public interface ITranslationRequestCreated
|
|
||||||
{
|
|
||||||
Guid TranslationRequestId { get; }
|
|
||||||
Language From { get; }
|
|
||||||
Language To { get; }
|
|
||||||
string Body { get; }
|
|
||||||
string TranslationEngineKey { get; }
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
namespace FictionArchive.Service.Shared.Contracts.Events;
|
|
||||||
|
|
||||||
public interface IUserInvited
|
|
||||||
{
|
|
||||||
string InvitedUserId { get; }
|
|
||||||
string InvitedUsername { get; }
|
|
||||||
string InvitedEmail { get; }
|
|
||||||
string InvitedOAuthProviderId { get; }
|
|
||||||
string InviterId { get; }
|
|
||||||
string InviterUsername { get; }
|
|
||||||
string InviterOAuthProviderId { get; }
|
|
||||||
}
|
|
||||||
@@ -1,118 +0,0 @@
|
|||||||
using System.Text.RegularExpressions;
|
|
||||||
using FictionArchive.Service.Shared.Services.Filters;
|
|
||||||
using MassTransit;
|
|
||||||
using Microsoft.Extensions.Configuration;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
|
|
||||||
namespace FictionArchive.Service.Shared.Extensions;
|
|
||||||
|
|
||||||
public static class MassTransitExtensions
|
|
||||||
{
|
|
||||||
public static IServiceCollection AddFictionArchiveMassTransit(
|
|
||||||
this IServiceCollection services,
|
|
||||||
IConfiguration configuration,
|
|
||||||
Action<IBusRegistrationConfigurator>? configureConsumers = null)
|
|
||||||
{
|
|
||||||
services.AddMassTransit(x =>
|
|
||||||
{
|
|
||||||
configureConsumers?.Invoke(x);
|
|
||||||
|
|
||||||
x.UsingRabbitMq((context, cfg) =>
|
|
||||||
{
|
|
||||||
var (host, username, password) = ParseRabbitMqConfiguration(configuration);
|
|
||||||
|
|
||||||
cfg.Host(host, h =>
|
|
||||||
{
|
|
||||||
h.Username(username);
|
|
||||||
h.Password(password);
|
|
||||||
});
|
|
||||||
|
|
||||||
cfg.UseMessageRetry(r => r.Exponential(
|
|
||||||
retryLimit: 5,
|
|
||||||
minInterval: TimeSpan.FromSeconds(1),
|
|
||||||
maxInterval: TimeSpan.FromMinutes(1),
|
|
||||||
intervalDelta: TimeSpan.FromSeconds(2)));
|
|
||||||
|
|
||||||
cfg.UseConsumeFilter(typeof(LoggingConsumeFilter<>), context);
|
|
||||||
|
|
||||||
// Process one message at a time per consumer (matches old EventBus behavior)
|
|
||||||
cfg.PrefetchCount = 1;
|
|
||||||
|
|
||||||
cfg.ConfigureEndpoints(context);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
return services;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Parses RabbitMQ configuration from either ConnectionString format or separate Host/Username/Password keys.
|
|
||||||
/// ConnectionString format: amqp://[username:password@]host[:port]
|
|
||||||
/// </summary>
|
|
||||||
private static (string Host, string Username, string Password) ParseRabbitMqConfiguration(IConfiguration configuration)
|
|
||||||
{
|
|
||||||
var connectionString = configuration["RabbitMQ:ConnectionString"];
|
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(connectionString))
|
|
||||||
{
|
|
||||||
return ParseConnectionString(connectionString);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fallback to separate configuration keys
|
|
||||||
var host = configuration["RabbitMQ:Host"] ?? "localhost";
|
|
||||||
var username = configuration["RabbitMQ:Username"] ?? "guest";
|
|
||||||
var password = configuration["RabbitMQ:Password"] ?? "guest";
|
|
||||||
|
|
||||||
return (host, username, password);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Parses an AMQP connection string into host, username, and password components.
|
|
||||||
/// Supports formats:
|
|
||||||
/// - amqp://host
|
|
||||||
/// - amqp://host:port
|
|
||||||
/// - amqp://username:password@host
|
|
||||||
/// - amqp://username:password@host:port
|
|
||||||
/// </summary>
|
|
||||||
private static (string Host, string Username, string Password) ParseConnectionString(string connectionString)
|
|
||||||
{
|
|
||||||
var username = "guest";
|
|
||||||
var password = "guest";
|
|
||||||
var host = "localhost";
|
|
||||||
|
|
||||||
// Try to parse as URI first
|
|
||||||
if (Uri.TryCreate(connectionString, UriKind.Absolute, out var uri))
|
|
||||||
{
|
|
||||||
host = uri.Host;
|
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(uri.UserInfo))
|
|
||||||
{
|
|
||||||
var userInfoParts = uri.UserInfo.Split(':', 2);
|
|
||||||
username = Uri.UnescapeDataString(userInfoParts[0]);
|
|
||||||
if (userInfoParts.Length > 1)
|
|
||||||
{
|
|
||||||
password = Uri.UnescapeDataString(userInfoParts[1]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Fallback regex parsing for edge cases
|
|
||||||
var match = Regex.Match(connectionString, @"amqp://(?:([^:]+):([^@]+)@)?([^:/]+)");
|
|
||||||
if (match.Success)
|
|
||||||
{
|
|
||||||
if (match.Groups[1].Success && match.Groups[2].Success)
|
|
||||||
{
|
|
||||||
username = match.Groups[1].Value;
|
|
||||||
password = match.Groups[2].Value;
|
|
||||||
}
|
|
||||||
if (match.Groups[3].Success)
|
|
||||||
{
|
|
||||||
host = match.Groups[3].Value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return (host, username, password);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -31,7 +31,9 @@
|
|||||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
|
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
|
||||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime" Version="9.0.4" />
|
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime" Version="9.0.4" />
|
||||||
<PackageReference Include="Polly" Version="8.6.5" />
|
<PackageReference Include="Polly" Version="8.6.5" />
|
||||||
<PackageReference Include="MassTransit.RabbitMQ" Version="8.*" />
|
<PackageReference Include="MassTransit" Version="8.4.0" />
|
||||||
|
<PackageReference Include="MassTransit.RabbitMQ" Version="8.4.0" />
|
||||||
|
<PackageReference Include="MassTransit.EntityFrameworkCore" Version="8.4.0" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.11" />
|
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.11" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
namespace FictionArchive.Service.Shared.MassTransit.Configuration;
|
||||||
|
|
||||||
|
public class MassTransitOptions
|
||||||
|
{
|
||||||
|
public string Host { get; set; } = "localhost";
|
||||||
|
public string VirtualHost { get; set; } = "/";
|
||||||
|
public string Username { get; set; } = "guest";
|
||||||
|
public string Password { get; set; } = "guest";
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
namespace FictionArchive.Service.Shared.MassTransit.Contracts.Commands;
|
||||||
|
|
||||||
|
public record ImportNovelCommand : ICommand
|
||||||
|
{
|
||||||
|
public required string NovelUrl { get; init; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
namespace FictionArchive.Service.Shared.MassTransit.Contracts.Commands;
|
||||||
|
|
||||||
|
public record PullChapterContentCommand : ICommand
|
||||||
|
{
|
||||||
|
public required uint NovelId { get; init; }
|
||||||
|
public required uint VolumeId { get; init; }
|
||||||
|
public required uint ChapterOrder { get; init; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
using FictionArchive.Common.Enums;
|
||||||
|
|
||||||
|
namespace FictionArchive.Service.Shared.MassTransit.Contracts.Commands;
|
||||||
|
|
||||||
|
public record TranslateTextCommand : ICommand
|
||||||
|
{
|
||||||
|
public Guid TranslationRequestId { get; init; }
|
||||||
|
public Language From { get; init; }
|
||||||
|
public Language To { get; init; }
|
||||||
|
public required string Body { get; init; }
|
||||||
|
public required string TranslationEngineKey { get; init; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
namespace FictionArchive.Service.Shared.MassTransit.Contracts.Commands;
|
||||||
|
|
||||||
|
public record UploadFileCommand : ICommand
|
||||||
|
{
|
||||||
|
public Guid RequestId { get; init; }
|
||||||
|
public required string FilePath { get; init; }
|
||||||
|
public required byte[] FileData { get; init; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
namespace FictionArchive.Service.Shared.MassTransit.Contracts.Events;
|
||||||
|
|
||||||
|
public record AuthUserAddedEvent : IEvent
|
||||||
|
{
|
||||||
|
public required string OAuthProviderId { get; init; }
|
||||||
|
public required string InviterOAuthProviderId { get; init; }
|
||||||
|
public required string EventUserEmail { get; init; }
|
||||||
|
public required string EventUserUsername { get; init; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
namespace FictionArchive.Service.Shared.MassTransit.Contracts.Events;
|
||||||
|
|
||||||
|
public record ChapterCreatedEvent : IEvent
|
||||||
|
{
|
||||||
|
public required uint ChapterId { get; init; }
|
||||||
|
public required uint NovelId { get; init; }
|
||||||
|
public required uint VolumeId { get; init; }
|
||||||
|
public required int VolumeOrder { get; init; }
|
||||||
|
public required uint ChapterOrder { get; init; }
|
||||||
|
public required string ChapterTitle { get; init; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
using FictionArchive.Common.Enums;
|
||||||
|
|
||||||
|
namespace FictionArchive.Service.Shared.MassTransit.Contracts.Events;
|
||||||
|
|
||||||
|
public record FileUploadCompletedEvent : IEvent
|
||||||
|
{
|
||||||
|
public Guid RequestId { get; init; }
|
||||||
|
public RequestStatus Status { get; init; }
|
||||||
|
public string? FileAccessUrl { get; init; }
|
||||||
|
public string? ErrorMessage { get; init; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
using FictionArchive.Common.Enums;
|
||||||
|
|
||||||
|
namespace FictionArchive.Service.Shared.MassTransit.Contracts.Events;
|
||||||
|
|
||||||
|
public record NovelCreatedEvent : IEvent
|
||||||
|
{
|
||||||
|
public required uint NovelId { get; init; }
|
||||||
|
public required string Title { get; init; }
|
||||||
|
public required Language OriginalLanguage { get; init; }
|
||||||
|
public required string Source { get; init; }
|
||||||
|
public required string AuthorName { get; init; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
namespace FictionArchive.Service.Shared.MassTransit.Contracts.Events;
|
||||||
|
|
||||||
|
public record TranslationCompletedEvent : IEvent
|
||||||
|
{
|
||||||
|
public Guid TranslationRequestId { get; init; }
|
||||||
|
public required string TranslatedText { get; init; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
namespace FictionArchive.Service.Shared.MassTransit.Contracts.Events;
|
||||||
|
|
||||||
|
public record UserInvitedEvent : IEvent
|
||||||
|
{
|
||||||
|
public Guid InvitedUserId { get; init; }
|
||||||
|
public required string InvitedUsername { get; init; }
|
||||||
|
public required string InvitedEmail { get; init; }
|
||||||
|
public required string InvitedOAuthProviderId { get; init; }
|
||||||
|
|
||||||
|
public Guid InviterId { get; init; }
|
||||||
|
public required string InviterUsername { get; init; }
|
||||||
|
public required string InviterOAuthProviderId { get; init; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
namespace FictionArchive.Service.Shared.MassTransit.Contracts;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Marker interface for commands (do something, single consumer)
|
||||||
|
/// </summary>
|
||||||
|
public interface ICommand { }
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
namespace FictionArchive.Service.Shared.MassTransit.Contracts;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Marker interface for events (something happened, multiple subscribers)
|
||||||
|
/// </summary>
|
||||||
|
public interface IEvent { }
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
using NodaTime;
|
||||||
|
|
||||||
|
namespace FictionArchive.Service.Shared.MassTransit.Contracts;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Published by sagas on state transitions for centralized job tracking
|
||||||
|
/// </summary>
|
||||||
|
public record JobStateChangedEvent : IEvent
|
||||||
|
{
|
||||||
|
public Guid JobId { get; init; }
|
||||||
|
public required string JobType { get; init; }
|
||||||
|
public required string FromState { get; init; }
|
||||||
|
public required string ToState { get; init; }
|
||||||
|
public string? Message { get; init; }
|
||||||
|
public string? Error { get; init; }
|
||||||
|
public Instant Timestamp { get; init; }
|
||||||
|
public Dictionary<string, object>? Metadata { get; init; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,103 @@
|
|||||||
|
using FictionArchive.Service.Shared.MassTransit.Configuration;
|
||||||
|
using MassTransit;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace FictionArchive.Service.Shared.MassTransit;
|
||||||
|
|
||||||
|
public static class MassTransitExtensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Adds MassTransit with RabbitMQ and Entity Framework outbox
|
||||||
|
/// </summary>
|
||||||
|
public static IServiceCollection AddFictionArchiveMassTransit<TDbContext>(
|
||||||
|
this IServiceCollection services,
|
||||||
|
IConfiguration configuration,
|
||||||
|
Action<IBusRegistrationConfigurator>? configureConsumers = null)
|
||||||
|
where TDbContext : DbContext
|
||||||
|
{
|
||||||
|
services.AddMassTransit(x =>
|
||||||
|
{
|
||||||
|
configureConsumers?.Invoke(x);
|
||||||
|
|
||||||
|
x.AddEntityFrameworkOutbox<TDbContext>(o =>
|
||||||
|
{
|
||||||
|
o.UsePostgres();
|
||||||
|
o.UseBusOutbox();
|
||||||
|
});
|
||||||
|
|
||||||
|
x.UsingRabbitMq((context, cfg) =>
|
||||||
|
{
|
||||||
|
var options = configuration.GetSection("RabbitMQ").Get<MassTransitOptions>()
|
||||||
|
?? new MassTransitOptions();
|
||||||
|
|
||||||
|
cfg.Host(options.Host, options.VirtualHost, h =>
|
||||||
|
{
|
||||||
|
h.Username(options.Username);
|
||||||
|
h.Password(options.Password);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Immediate retries for transient failures
|
||||||
|
cfg.UseMessageRetry(r => r.Intervals(
|
||||||
|
TimeSpan.FromSeconds(1),
|
||||||
|
TimeSpan.FromSeconds(1),
|
||||||
|
TimeSpan.FromSeconds(1)));
|
||||||
|
|
||||||
|
// Delayed redelivery for longer outages
|
||||||
|
cfg.UseDelayedRedelivery(r => r.Intervals(
|
||||||
|
TimeSpan.FromSeconds(5),
|
||||||
|
TimeSpan.FromSeconds(30),
|
||||||
|
TimeSpan.FromMinutes(2),
|
||||||
|
TimeSpan.FromMinutes(10),
|
||||||
|
TimeSpan.FromMinutes(30)));
|
||||||
|
|
||||||
|
cfg.ConfigureEndpoints(context);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds MassTransit with RabbitMQ without outbox (for services without EF)
|
||||||
|
/// </summary>
|
||||||
|
public static IServiceCollection AddFictionArchiveMassTransit(
|
||||||
|
this IServiceCollection services,
|
||||||
|
IConfiguration configuration,
|
||||||
|
Action<IBusRegistrationConfigurator>? configureConsumers = null)
|
||||||
|
{
|
||||||
|
services.AddMassTransit(x =>
|
||||||
|
{
|
||||||
|
configureConsumers?.Invoke(x);
|
||||||
|
|
||||||
|
x.UsingRabbitMq((context, cfg) =>
|
||||||
|
{
|
||||||
|
var options = configuration.GetSection("RabbitMQ").Get<MassTransitOptions>()
|
||||||
|
?? new MassTransitOptions();
|
||||||
|
|
||||||
|
cfg.Host(options.Host, options.VirtualHost, h =>
|
||||||
|
{
|
||||||
|
h.Username(options.Username);
|
||||||
|
h.Password(options.Password);
|
||||||
|
});
|
||||||
|
|
||||||
|
cfg.UseMessageRetry(r => r.Intervals(
|
||||||
|
TimeSpan.FromSeconds(1),
|
||||||
|
TimeSpan.FromSeconds(1),
|
||||||
|
TimeSpan.FromSeconds(1)));
|
||||||
|
|
||||||
|
cfg.UseDelayedRedelivery(r => r.Intervals(
|
||||||
|
TimeSpan.FromSeconds(5),
|
||||||
|
TimeSpan.FromSeconds(30),
|
||||||
|
TimeSpan.FromMinutes(2),
|
||||||
|
TimeSpan.FromMinutes(10),
|
||||||
|
TimeSpan.FromMinutes(30)));
|
||||||
|
|
||||||
|
cfg.ConfigureEndpoints(context);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
using MassTransit;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
|
|
||||||
namespace FictionArchive.Service.Shared.Services.Filters;
|
|
||||||
|
|
||||||
public class LoggingConsumeFilter<T> : IFilter<ConsumeContext<T>> where T : class
|
|
||||||
{
|
|
||||||
private readonly ILogger<LoggingConsumeFilter<T>> _logger;
|
|
||||||
|
|
||||||
public LoggingConsumeFilter(ILogger<LoggingConsumeFilter<T>> logger)
|
|
||||||
{
|
|
||||||
_logger = logger;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task Send(ConsumeContext<T> context, IPipe<ConsumeContext<T>> next)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
await next.Send(context);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.LogError(ex,
|
|
||||||
"Message {MessageType} failed after all retries. MessageId: {MessageId}, ConversationId: {ConversationId}",
|
|
||||||
typeof(T).Name,
|
|
||||||
context.MessageId,
|
|
||||||
context.ConversationId);
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Probe(ProbeContext context) => context.CreateFilterScope("logging");
|
|
||||||
}
|
|
||||||
@@ -1,53 +0,0 @@
|
|||||||
using FictionArchive.Service.Shared.Contracts.Events;
|
|
||||||
using FictionArchive.Service.TranslationService.Contracts;
|
|
||||||
using FictionArchive.Service.TranslationService.Models.Enums;
|
|
||||||
using FictionArchive.Service.TranslationService.Services;
|
|
||||||
using MassTransit;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
|
|
||||||
namespace FictionArchive.Service.TranslationService.Consumers;
|
|
||||||
|
|
||||||
public class TranslationRequestCreatedConsumer : IConsumer<ITranslationRequestCreated>
|
|
||||||
{
|
|
||||||
private readonly ILogger<TranslationRequestCreatedConsumer> _logger;
|
|
||||||
private readonly TranslationEngineService _translationEngineService;
|
|
||||||
private readonly IPublishEndpoint _publishEndpoint;
|
|
||||||
|
|
||||||
public TranslationRequestCreatedConsumer(
|
|
||||||
ILogger<TranslationRequestCreatedConsumer> logger,
|
|
||||||
TranslationEngineService translationEngineService,
|
|
||||||
IPublishEndpoint publishEndpoint)
|
|
||||||
{
|
|
||||||
_logger = logger;
|
|
||||||
_translationEngineService = translationEngineService;
|
|
||||||
_publishEndpoint = publishEndpoint;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task Consume(ConsumeContext<ITranslationRequestCreated> context)
|
|
||||||
{
|
|
||||||
var message = context.Message;
|
|
||||||
|
|
||||||
_logger.LogInformation("Processing translation request {TranslationRequestId}", message.TranslationRequestId);
|
|
||||||
|
|
||||||
var result = await _translationEngineService.Translate(
|
|
||||||
message.From,
|
|
||||||
message.To,
|
|
||||||
message.Body,
|
|
||||||
message.TranslationEngineKey);
|
|
||||||
|
|
||||||
if (result.Status == TranslationRequestStatus.Success)
|
|
||||||
{
|
|
||||||
await _publishEndpoint.Publish<ITranslationRequestCompleted>(
|
|
||||||
new TranslationRequestCompleted(
|
|
||||||
TranslationRequestId: message.TranslationRequestId,
|
|
||||||
TranslatedText: result.TranslatedText));
|
|
||||||
|
|
||||||
_logger.LogInformation("Translation completed for request {TranslationRequestId}", message.TranslationRequestId);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_logger.LogError("Translation failed for request {TranslationRequestId}", message.TranslationRequestId);
|
|
||||||
throw new InvalidOperationException($"Translation failed for request {message.TranslationRequestId}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
using FictionArchive.Service.Shared.Contracts.Events;
|
|
||||||
|
|
||||||
namespace FictionArchive.Service.TranslationService.Contracts;
|
|
||||||
|
|
||||||
public record TranslationRequestCompleted(
|
|
||||||
Guid? TranslationRequestId,
|
|
||||||
string? TranslatedText) : ITranslationRequestCompleted;
|
|
||||||
@@ -23,6 +23,7 @@
|
|||||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
|
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
|
||||||
<PackageReference Include="DeepL.net" Version="1.17.0" />
|
<PackageReference Include="DeepL.net" Version="1.17.0" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2"/>
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2"/>
|
||||||
|
<PackageReference Include="MassTransit" Version="8.4.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -2,11 +2,12 @@ using DeepL;
|
|||||||
using FictionArchive.Common.Extensions;
|
using FictionArchive.Common.Extensions;
|
||||||
using FictionArchive.Service.Shared;
|
using FictionArchive.Service.Shared;
|
||||||
using FictionArchive.Service.Shared.Extensions;
|
using FictionArchive.Service.Shared.Extensions;
|
||||||
|
using FictionArchive.Service.Shared.MassTransit;
|
||||||
using FictionArchive.Service.Shared.Services.GraphQL;
|
using FictionArchive.Service.Shared.Services.GraphQL;
|
||||||
using FictionArchive.Service.TranslationService.Consumers;
|
|
||||||
using FictionArchive.Service.TranslationService.GraphQL;
|
using FictionArchive.Service.TranslationService.GraphQL;
|
||||||
using FictionArchive.Service.TranslationService.Services;
|
using FictionArchive.Service.TranslationService.Services;
|
||||||
using FictionArchive.Service.TranslationService.Services.Database;
|
using FictionArchive.Service.TranslationService.Services.Database;
|
||||||
|
using FictionArchive.Service.TranslationService.Services.EventHandlers;
|
||||||
using FictionArchive.Service.TranslationService.Services.TranslationEngines;
|
using FictionArchive.Service.TranslationService.Services.TranslationEngines;
|
||||||
using FictionArchive.Service.TranslationService.Services.TranslationEngines.DeepLTranslate;
|
using FictionArchive.Service.TranslationService.Services.TranslationEngines.DeepLTranslate;
|
||||||
|
|
||||||
@@ -27,11 +28,11 @@ public class Program
|
|||||||
|
|
||||||
if (!isSchemaExport)
|
if (!isSchemaExport)
|
||||||
{
|
{
|
||||||
builder.Services.AddFictionArchiveMassTransit(
|
builder.Services.AddFictionArchiveMassTransit<TranslationServiceDbContext>(
|
||||||
builder.Configuration,
|
builder.Configuration,
|
||||||
x =>
|
x =>
|
||||||
{
|
{
|
||||||
x.AddConsumer<TranslationRequestCreatedConsumer>();
|
x.AddConsumer<TranslateTextCommandConsumer>();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
using FictionArchive.Service.Shared.MassTransit.Contracts.Commands;
|
||||||
|
using FictionArchive.Service.Shared.MassTransit.Contracts.Events;
|
||||||
|
using FictionArchive.Service.TranslationService.Models.Enums;
|
||||||
|
using MassTransit;
|
||||||
|
|
||||||
|
namespace FictionArchive.Service.TranslationService.Services.EventHandlers;
|
||||||
|
|
||||||
|
public class TranslateTextCommandConsumer : IConsumer<TranslateTextCommand>
|
||||||
|
{
|
||||||
|
private readonly ILogger<TranslateTextCommandConsumer> _logger;
|
||||||
|
private readonly TranslationEngineService _translationEngineService;
|
||||||
|
|
||||||
|
public TranslateTextCommandConsumer(
|
||||||
|
ILogger<TranslateTextCommandConsumer> logger,
|
||||||
|
TranslationEngineService translationEngineService)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
_translationEngineService = translationEngineService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Consume(ConsumeContext<TranslateTextCommand> context)
|
||||||
|
{
|
||||||
|
var command = context.Message;
|
||||||
|
|
||||||
|
var result = await _translationEngineService.Translate(
|
||||||
|
command.From,
|
||||||
|
command.To,
|
||||||
|
command.Body,
|
||||||
|
command.TranslationEngineKey);
|
||||||
|
|
||||||
|
if (result.Status == TranslationRequestStatus.Success)
|
||||||
|
{
|
||||||
|
await context.Publish(new TranslationCompletedEvent
|
||||||
|
{
|
||||||
|
TranslatedText = result.TranslatedText!,
|
||||||
|
TranslationRequestId = command.TranslationRequestId,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,8 +2,7 @@
|
|||||||
"Logging": {
|
"Logging": {
|
||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
"Default": "Information",
|
"Default": "Information",
|
||||||
"Microsoft.AspNetCore": "Warning",
|
"Microsoft.AspNetCore": "Warning"
|
||||||
"Microsoft.EntityFrameworkCore": "Warning"
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"DeepL": {
|
"DeepL": {
|
||||||
@@ -13,8 +12,10 @@
|
|||||||
"DefaultConnection": "Host=localhost;Database=FictionArchive_NovelService;Username=postgres;password=postgres"
|
"DefaultConnection": "Host=localhost;Database=FictionArchive_NovelService;Username=postgres;password=postgres"
|
||||||
},
|
},
|
||||||
"RabbitMQ": {
|
"RabbitMQ": {
|
||||||
"ConnectionString": "amqp://localhost",
|
"Host": "localhost",
|
||||||
"ClientIdentifier": "TranslationService"
|
"VirtualHost": "/",
|
||||||
|
"Username": "guest",
|
||||||
|
"Password": "guest"
|
||||||
},
|
},
|
||||||
"AllowedHosts": "*",
|
"AllowedHosts": "*",
|
||||||
"OIDC": {
|
"OIDC": {
|
||||||
|
|||||||
@@ -1,39 +0,0 @@
|
|||||||
using FictionArchive.Service.Shared.Contracts.Events;
|
|
||||||
using FictionArchive.Service.UserNovelDataService.Models.Database;
|
|
||||||
using FictionArchive.Service.UserNovelDataService.Services;
|
|
||||||
using MassTransit;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
|
|
||||||
namespace FictionArchive.Service.UserNovelDataService.Consumers;
|
|
||||||
|
|
||||||
public class NovelCreatedConsumer : IConsumer<INovelCreated>
|
|
||||||
{
|
|
||||||
private readonly ILogger<NovelCreatedConsumer> _logger;
|
|
||||||
private readonly UserNovelDataServiceDbContext _dbContext;
|
|
||||||
|
|
||||||
public NovelCreatedConsumer(
|
|
||||||
ILogger<NovelCreatedConsumer> logger,
|
|
||||||
UserNovelDataServiceDbContext dbContext)
|
|
||||||
{
|
|
||||||
_logger = logger;
|
|
||||||
_dbContext = dbContext;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task Consume(ConsumeContext<INovelCreated> context)
|
|
||||||
{
|
|
||||||
var message = context.Message;
|
|
||||||
|
|
||||||
var exists = await _dbContext.Novels.AnyAsync(n => n.Id == message.NovelId);
|
|
||||||
if (exists)
|
|
||||||
{
|
|
||||||
_logger.LogDebug("Novel {NovelId} already exists, skipping", message.NovelId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var novel = new Novel { Id = message.NovelId };
|
|
||||||
_dbContext.Novels.Add(novel);
|
|
||||||
await _dbContext.SaveChangesAsync();
|
|
||||||
|
|
||||||
_logger.LogInformation("Created novel stub for {NovelId}", message.NovelId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
using FictionArchive.Service.Shared.Contracts.Events;
|
|
||||||
using FictionArchive.Service.UserNovelDataService.Models.Database;
|
|
||||||
using FictionArchive.Service.UserNovelDataService.Services;
|
|
||||||
using MassTransit;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
|
|
||||||
namespace FictionArchive.Service.UserNovelDataService.Consumers;
|
|
||||||
|
|
||||||
public class UserInvitedConsumer : IConsumer<IUserInvited>
|
|
||||||
{
|
|
||||||
private readonly ILogger<UserInvitedConsumer> _logger;
|
|
||||||
private readonly UserNovelDataServiceDbContext _dbContext;
|
|
||||||
|
|
||||||
public UserInvitedConsumer(
|
|
||||||
ILogger<UserInvitedConsumer> logger,
|
|
||||||
UserNovelDataServiceDbContext dbContext)
|
|
||||||
{
|
|
||||||
_logger = logger;
|
|
||||||
_dbContext = dbContext;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task Consume(ConsumeContext<IUserInvited> context)
|
|
||||||
{
|
|
||||||
var message = context.Message;
|
|
||||||
|
|
||||||
var userId = Guid.Parse(message.InvitedUserId);
|
|
||||||
var exists = await _dbContext.Users.AnyAsync(u => u.Id == userId);
|
|
||||||
if (exists)
|
|
||||||
{
|
|
||||||
_logger.LogDebug("User {UserId} already exists, skipping", message.InvitedUserId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var user = new User
|
|
||||||
{
|
|
||||||
Id = userId,
|
|
||||||
OAuthProviderId = message.InvitedOAuthProviderId
|
|
||||||
};
|
|
||||||
_dbContext.Users.Add(user);
|
|
||||||
await _dbContext.SaveChangesAsync();
|
|
||||||
|
|
||||||
_logger.LogInformation("Created user stub for {UserId}", message.InvitedUserId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,9 +1,10 @@
|
|||||||
using FictionArchive.Common.Extensions;
|
using FictionArchive.Common.Extensions;
|
||||||
using FictionArchive.Service.Shared;
|
using FictionArchive.Service.Shared;
|
||||||
using FictionArchive.Service.Shared.Extensions;
|
using FictionArchive.Service.Shared.Extensions;
|
||||||
using FictionArchive.Service.UserNovelDataService.Consumers;
|
using FictionArchive.Service.Shared.MassTransit;
|
||||||
using FictionArchive.Service.UserNovelDataService.GraphQL;
|
using FictionArchive.Service.UserNovelDataService.GraphQL;
|
||||||
using FictionArchive.Service.UserNovelDataService.Services;
|
using FictionArchive.Service.UserNovelDataService.Services;
|
||||||
|
using FictionArchive.Service.UserNovelDataService.Services.EventHandlers;
|
||||||
|
|
||||||
namespace FictionArchive.Service.UserNovelDataService;
|
namespace FictionArchive.Service.UserNovelDataService;
|
||||||
|
|
||||||
@@ -24,13 +25,13 @@ public class Program
|
|||||||
|
|
||||||
if (!isSchemaExport)
|
if (!isSchemaExport)
|
||||||
{
|
{
|
||||||
builder.Services.AddFictionArchiveMassTransit(
|
builder.Services.AddFictionArchiveMassTransit<UserNovelDataServiceDbContext>(
|
||||||
builder.Configuration,
|
builder.Configuration,
|
||||||
x =>
|
x =>
|
||||||
{
|
{
|
||||||
x.AddConsumer<NovelCreatedConsumer>();
|
x.AddConsumer<NovelCreatedEventConsumer>();
|
||||||
x.AddConsumer<ChapterCreatedConsumer>();
|
x.AddConsumer<ChapterCreatedEventConsumer>();
|
||||||
x.AddConsumer<UserInvitedConsumer>();
|
x.AddConsumer<UserInvitedEventConsumer>();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,56 +1,55 @@
|
|||||||
using FictionArchive.Service.Shared.Contracts.Events;
|
using FictionArchive.Service.Shared.MassTransit.Contracts.Events;
|
||||||
using FictionArchive.Service.UserNovelDataService.Models.Database;
|
using FictionArchive.Service.UserNovelDataService.Models.Database;
|
||||||
using FictionArchive.Service.UserNovelDataService.Services;
|
|
||||||
using MassTransit;
|
using MassTransit;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
namespace FictionArchive.Service.UserNovelDataService.Consumers;
|
namespace FictionArchive.Service.UserNovelDataService.Services.EventHandlers;
|
||||||
|
|
||||||
public class ChapterCreatedConsumer : IConsumer<IChapterCreated>
|
public class ChapterCreatedEventConsumer : IConsumer<ChapterCreatedEvent>
|
||||||
{
|
{
|
||||||
private readonly ILogger<ChapterCreatedConsumer> _logger;
|
|
||||||
private readonly UserNovelDataServiceDbContext _dbContext;
|
private readonly UserNovelDataServiceDbContext _dbContext;
|
||||||
|
private readonly ILogger<ChapterCreatedEventConsumer> _logger;
|
||||||
|
|
||||||
public ChapterCreatedConsumer(
|
public ChapterCreatedEventConsumer(
|
||||||
ILogger<ChapterCreatedConsumer> logger,
|
UserNovelDataServiceDbContext dbContext,
|
||||||
UserNovelDataServiceDbContext dbContext)
|
ILogger<ChapterCreatedEventConsumer> logger)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
|
||||||
_dbContext = dbContext;
|
_dbContext = dbContext;
|
||||||
|
_logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Consume(ConsumeContext<IChapterCreated> context)
|
public async Task Consume(ConsumeContext<ChapterCreatedEvent> context)
|
||||||
{
|
{
|
||||||
var message = context.Message;
|
var @event = context.Message;
|
||||||
|
|
||||||
// Ensure novel exists
|
// Ensure novel exists
|
||||||
var novelExists = await _dbContext.Novels.AnyAsync(n => n.Id == message.NovelId);
|
var novelExists = await _dbContext.Novels.AnyAsync(n => n.Id == @event.NovelId);
|
||||||
if (!novelExists)
|
if (!novelExists)
|
||||||
{
|
{
|
||||||
var novel = new Novel { Id = message.NovelId };
|
var novel = new Novel { Id = @event.NovelId };
|
||||||
_dbContext.Novels.Add(novel);
|
_dbContext.Novels.Add(novel);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure volume exists
|
// Ensure volume exists
|
||||||
var volumeExists = await _dbContext.Volumes.AnyAsync(v => v.Id == message.VolumeId);
|
var volumeExists = await _dbContext.Volumes.AnyAsync(v => v.Id == @event.VolumeId);
|
||||||
if (!volumeExists)
|
if (!volumeExists)
|
||||||
{
|
{
|
||||||
var volume = new Volume { Id = message.VolumeId, NovelId = message.NovelId };
|
var volume = new Volume { Id = @event.VolumeId };
|
||||||
_dbContext.Volumes.Add(volume);
|
_dbContext.Volumes.Add(volume);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create chapter if not exists
|
// Create chapter if not exists
|
||||||
var chapterExists = await _dbContext.Chapters.AnyAsync(c => c.Id == message.ChapterId);
|
var chapterExists = await _dbContext.Chapters.AnyAsync(c => c.Id == @event.ChapterId);
|
||||||
if (chapterExists)
|
if (chapterExists)
|
||||||
{
|
{
|
||||||
_logger.LogDebug("Chapter {ChapterId} already exists, skipping", message.ChapterId);
|
_logger.LogDebug("Chapter {ChapterId} already exists, skipping", @event.ChapterId);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var chapter = new Chapter { Id = message.ChapterId, VolumeId = message.VolumeId };
|
var chapter = new Chapter { Id = @event.ChapterId };
|
||||||
_dbContext.Chapters.Add(chapter);
|
_dbContext.Chapters.Add(chapter);
|
||||||
await _dbContext.SaveChangesAsync();
|
await _dbContext.SaveChangesAsync();
|
||||||
|
|
||||||
_logger.LogInformation("Created chapter stub for {ChapterId} in novel {NovelId}", message.ChapterId, message.NovelId);
|
_logger.LogInformation("Created chapter stub for {ChapterId} in novel {NovelId}", @event.ChapterId, @event.NovelId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
using FictionArchive.Service.Shared.MassTransit.Contracts.Events;
|
||||||
|
using FictionArchive.Service.UserNovelDataService.Models.Database;
|
||||||
|
using MassTransit;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace FictionArchive.Service.UserNovelDataService.Services.EventHandlers;
|
||||||
|
|
||||||
|
public class NovelCreatedEventConsumer : IConsumer<NovelCreatedEvent>
|
||||||
|
{
|
||||||
|
private readonly UserNovelDataServiceDbContext _dbContext;
|
||||||
|
private readonly ILogger<NovelCreatedEventConsumer> _logger;
|
||||||
|
|
||||||
|
public NovelCreatedEventConsumer(
|
||||||
|
UserNovelDataServiceDbContext dbContext,
|
||||||
|
ILogger<NovelCreatedEventConsumer> logger)
|
||||||
|
{
|
||||||
|
_dbContext = dbContext;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Consume(ConsumeContext<NovelCreatedEvent> context)
|
||||||
|
{
|
||||||
|
var @event = context.Message;
|
||||||
|
|
||||||
|
var exists = await _dbContext.Novels.AnyAsync(n => n.Id == @event.NovelId);
|
||||||
|
if (exists)
|
||||||
|
{
|
||||||
|
_logger.LogDebug("Novel {NovelId} already exists, skipping", @event.NovelId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var novel = new Novel { Id = @event.NovelId };
|
||||||
|
_dbContext.Novels.Add(novel);
|
||||||
|
await _dbContext.SaveChangesAsync();
|
||||||
|
|
||||||
|
_logger.LogInformation("Created novel stub for {NovelId}", @event.NovelId);
|
||||||
|
}
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user