Compare commits
11 Commits
c97654631b
...
epic/FA-mi
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ec967770d3 | ||
|
|
579e05b853 | ||
| e7435435c1 | |||
|
|
dd7aa4b044 | ||
|
|
1b9da7441c | ||
| 055ef33666 | |||
|
|
48ee43c4f6 | ||
| 98ae4ea4f2 | |||
|
|
15e1a84f55 | ||
|
|
70d4ba201a | ||
|
|
b69bcd6bf4 |
@@ -28,6 +28,9 @@ jobs:
|
||||
- name: user-service
|
||||
project: FictionArchive.Service.UserService
|
||||
subgraph: User
|
||||
- name: usernoveldata-service
|
||||
project: FictionArchive.Service.UserNovelDataService
|
||||
subgraph: UserNovelData
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
@@ -110,6 +113,12 @@ jobs:
|
||||
name: user-service-subgraph
|
||||
path: subgraphs/user
|
||||
|
||||
- name: Download UserNovelData Service subgraph
|
||||
uses: christopherhx/gitea-download-artifact@v4
|
||||
with:
|
||||
name: usernoveldata-service-subgraph
|
||||
path: subgraphs/usernoveldata
|
||||
|
||||
- name: Configure subgraph URLs for Docker
|
||||
run: |
|
||||
for fsp in subgraphs/*/*.fsp; do
|
||||
|
||||
@@ -27,6 +27,8 @@ jobs:
|
||||
dockerfile: FictionArchive.Service.SchedulerService/Dockerfile
|
||||
- name: authentication-service
|
||||
dockerfile: FictionArchive.Service.AuthenticationService/Dockerfile
|
||||
- name: usernoveldata-service
|
||||
dockerfile: FictionArchive.Service.UserNovelDataService/Dockerfile
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
using FictionArchive.Service.AuthenticationService.Models.Requests;
|
||||
using FictionArchive.Service.AuthenticationService.Models.IntegrationEvents;
|
||||
using FictionArchive.Service.Shared.Services.EventBus;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace FictionArchive.Service.AuthenticationService.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class AuthenticationWebhookController : ControllerBase
|
||||
{
|
||||
private readonly IEventBus _eventBus;
|
||||
|
||||
public AuthenticationWebhookController(IEventBus eventBus)
|
||||
{
|
||||
_eventBus = eventBus;
|
||||
}
|
||||
|
||||
[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 _eventBus.Publish(authUserAddedEvent);
|
||||
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
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"]
|
||||
@@ -1,29 +0,0 @@
|
||||
<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>
|
||||
@@ -1,6 +0,0 @@
|
||||
@FictionArchive.Service.AuthenticationService_HostAddress = http://localhost:5091
|
||||
|
||||
GET {{FictionArchive.Service.AuthenticationService_HostAddress}}/weatherforecast/
|
||||
Accept: application/json
|
||||
|
||||
###
|
||||
@@ -1,16 +0,0 @@
|
||||
using FictionArchive.Service.Shared.Services.EventBus;
|
||||
|
||||
namespace FictionArchive.Service.AuthenticationService.Models.IntegrationEvents;
|
||||
|
||||
public class AuthUserAddedEvent : IIntegrationEvent
|
||||
{
|
||||
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; }
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
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; }
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
using FictionArchive.Service.Shared;
|
||||
using FictionArchive.Service.Shared.Services.EventBus.Implementations;
|
||||
|
||||
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 Event Bus
|
||||
|
||||
builder.Services.AddRabbitMQ(opt =>
|
||||
{
|
||||
builder.Configuration.GetSection("RabbitMQ").Bind(opt);
|
||||
});
|
||||
|
||||
#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();
|
||||
}
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
{
|
||||
"$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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"RabbitMQ": {
|
||||
"ConnectionString": "amqp://localhost",
|
||||
"ClientIdentifier": "AuthenticationService"
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
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,10 +0,0 @@
|
||||
using FictionArchive.Service.Shared.Services.EventBus;
|
||||
|
||||
namespace FictionArchive.Service.FileService.Models.IntegrationEvents;
|
||||
|
||||
public class FileUploadRequestCreatedEvent : IIntegrationEvent
|
||||
{
|
||||
public Guid RequestId { get; set; }
|
||||
public string FilePath { get; set; }
|
||||
public byte[] FileData { get; set; }
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
using FictionArchive.Common.Enums;
|
||||
using FictionArchive.Service.Shared.Services.EventBus;
|
||||
|
||||
namespace FictionArchive.Service.FileService.Models.IntegrationEvents;
|
||||
|
||||
public class FileUploadRequestStatusUpdateEvent : IIntegrationEvent
|
||||
{
|
||||
public Guid RequestId { get; set; }
|
||||
public RequestStatus Status { get; set; }
|
||||
|
||||
#region Success
|
||||
|
||||
public string? FileAccessUrl { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Failure
|
||||
|
||||
public string? ErrorMessage { get; set; }
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,11 +1,9 @@
|
||||
using Amazon.Runtime;
|
||||
using Amazon.S3;
|
||||
using FictionArchive.Common.Extensions;
|
||||
using FictionArchive.Service.FileService.Consumers;
|
||||
using FictionArchive.Service.FileService.Models;
|
||||
using FictionArchive.Service.FileService.Models.IntegrationEvents;
|
||||
using FictionArchive.Service.FileService.Services.EventHandlers;
|
||||
using FictionArchive.Service.Shared.Extensions;
|
||||
using FictionArchive.Service.Shared.Services.EventBus.Implementations;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace FictionArchive.Service.FileService;
|
||||
@@ -24,13 +22,14 @@ public class Program
|
||||
|
||||
builder.Services.AddHealthChecks();
|
||||
|
||||
#region Event Bus
|
||||
#region MassTransit
|
||||
|
||||
builder.Services.AddRabbitMQ(opt =>
|
||||
builder.Services.AddFictionArchiveMassTransit(
|
||||
builder.Configuration,
|
||||
x =>
|
||||
{
|
||||
builder.Configuration.GetSection("RabbitMQ").Bind(opt);
|
||||
})
|
||||
.Subscribe<FileUploadRequestCreatedEvent, FileUploadRequestCreatedEventHandler>();
|
||||
x.AddConsumer<FileUploadRequestCreatedConsumer>();
|
||||
});
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
using Amazon.S3;
|
||||
using Amazon.S3.Model;
|
||||
using FictionArchive.Common.Enums;
|
||||
using FictionArchive.Service.FileService.Models;
|
||||
using FictionArchive.Service.FileService.Models.IntegrationEvents;
|
||||
using FictionArchive.Service.Shared.Services.EventBus;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace FictionArchive.Service.FileService.Services.EventHandlers;
|
||||
|
||||
public class FileUploadRequestCreatedEventHandler : IIntegrationEventHandler<FileUploadRequestCreatedEvent>
|
||||
{
|
||||
private readonly ILogger<FileUploadRequestCreatedEventHandler> _logger;
|
||||
private readonly AmazonS3Client _amazonS3Client;
|
||||
private readonly IEventBus _eventBus;
|
||||
private readonly S3Configuration _s3Configuration;
|
||||
private readonly ProxyConfiguration _proxyConfiguration;
|
||||
|
||||
public FileUploadRequestCreatedEventHandler(ILogger<FileUploadRequestCreatedEventHandler> logger, AmazonS3Client amazonS3Client, IEventBus eventBus, IOptions<S3Configuration> s3Configuration, IOptions<ProxyConfiguration> proxyConfiguration)
|
||||
{
|
||||
_logger = logger;
|
||||
_amazonS3Client = amazonS3Client;
|
||||
_eventBus = eventBus;
|
||||
_proxyConfiguration = proxyConfiguration.Value;
|
||||
_s3Configuration = s3Configuration.Value;
|
||||
}
|
||||
|
||||
public async Task Handle(FileUploadRequestCreatedEvent @event)
|
||||
{
|
||||
var putObjectRequest = new PutObjectRequest();
|
||||
putObjectRequest.BucketName = _s3Configuration.Bucket;
|
||||
putObjectRequest.Key = @event.FilePath;
|
||||
putObjectRequest.UseChunkEncoding = false; // Needed to avoid an error with Garage
|
||||
|
||||
using MemoryStream memoryStream = new MemoryStream(@event.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 _eventBus.Publish(new FileUploadRequestStatusUpdateEvent()
|
||||
{
|
||||
RequestId = @event.RequestId,
|
||||
Status = RequestStatus.Failed,
|
||||
ErrorMessage = "An error occurred while uploading file to S3."
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
await _eventBus.Publish(new FileUploadRequestStatusUpdateEvent()
|
||||
{
|
||||
Status = RequestStatus.Success,
|
||||
RequestId = @event.RequestId,
|
||||
FileAccessUrl = _proxyConfiguration.BaseUrl + "/" + @event.FilePath
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,8 @@
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
"Microsoft.AspNetCore": "Warning",
|
||||
"Microsoft.EntityFrameworkCore": "Warning"
|
||||
}
|
||||
},
|
||||
"ProxyConfiguration": {
|
||||
|
||||
@@ -9,8 +9,10 @@
|
||||
|
||||
<ItemGroup>
|
||||
<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.NET.Test.Sdk" Version="17.11.1" />
|
||||
<PackageReference Include="NodaTime.Testing" Version="3.3.0" />
|
||||
<PackageReference Include="NSubstitute" Version="5.1.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.2" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using FictionArchive.Common.Enums;
|
||||
using FictionArchive.Service.FileService.IntegrationEvents;
|
||||
using FictionArchive.Service.NovelService.Models.Configuration;
|
||||
using FictionArchive.Service.NovelService.Models.Enums;
|
||||
using FictionArchive.Service.NovelService.Models.Images;
|
||||
@@ -8,12 +7,14 @@ using FictionArchive.Service.NovelService.Models.Novels;
|
||||
using FictionArchive.Service.NovelService.Models.SourceAdapters;
|
||||
using FictionArchive.Service.NovelService.Services;
|
||||
using FictionArchive.Service.NovelService.Services.SourceAdapters;
|
||||
using FictionArchive.Service.Shared.Services.EventBus;
|
||||
using FictionArchive.Service.Shared.Contracts.Events;
|
||||
using FluentAssertions;
|
||||
using HtmlAgilityPack;
|
||||
using MassTransit;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Microsoft.Extensions.Options;
|
||||
using NodaTime;
|
||||
using NSubstitute;
|
||||
using Xunit;
|
||||
|
||||
@@ -72,7 +73,7 @@ public class NovelUpdateServiceTests
|
||||
private static NovelUpdateService CreateService(
|
||||
NovelServiceDbContext dbContext,
|
||||
ISourceAdapter adapter,
|
||||
IEventBus eventBus,
|
||||
IPublishEndpoint publishEndpoint,
|
||||
string pendingImageUrl = "https://pending/placeholder.jpg")
|
||||
{
|
||||
var options = Options.Create(new NovelUpdateServiceConfiguration
|
||||
@@ -80,7 +81,10 @@ public class NovelUpdateServiceTests
|
||||
PendingImageUrl = pendingImageUrl
|
||||
});
|
||||
|
||||
return new NovelUpdateService(dbContext, NullLogger<NovelUpdateService>.Instance, new[] { adapter }, eventBus, options);
|
||||
var clock = Substitute.For<IClock>();
|
||||
clock.GetCurrentInstant().Returns(Instant.FromUnixTimeSeconds(0));
|
||||
|
||||
return new NovelUpdateService(dbContext, NullLogger<NovelUpdateService>.Instance, new[] { adapter }, publishEndpoint, options, clock);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -102,16 +106,18 @@ public class NovelUpdateServiceTests
|
||||
ImageData = new List<ImageData> { image1, image2 }
|
||||
}));
|
||||
|
||||
var publishedEvents = new List<FileUploadRequestCreatedEvent>();
|
||||
var eventBus = Substitute.For<IEventBus>();
|
||||
eventBus.Publish(Arg.Do<FileUploadRequestCreatedEvent>(publishedEvents.Add)).Returns(Task.CompletedTask);
|
||||
eventBus.Publish(Arg.Any<object>(), Arg.Any<string>()).Returns(Task.CompletedTask);
|
||||
var publishedEvents = new List<IFileUploadRequestCreated>();
|
||||
var publishEndpoint = Substitute.For<IPublishEndpoint>();
|
||||
publishEndpoint.Publish(Arg.Do<IFileUploadRequestCreated>(e => publishedEvents.Add(e)), Arg.Any<CancellationToken>())
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
var pendingImageUrl = "https://pending/placeholder.jpg";
|
||||
var service = CreateService(dbContext, adapter, eventBus, pendingImageUrl);
|
||||
var service = CreateService(dbContext, adapter, publishEndpoint, pendingImageUrl);
|
||||
|
||||
var updatedChapter = await service.PullChapterContents(novel.Id, volume.Id, chapter.Order);
|
||||
var importId = Guid.NewGuid();
|
||||
var (updatedChapter, imageCount) = await service.PullChapterContents(importId, novel.Id, volume.Id, chapter.Order);
|
||||
|
||||
imageCount.Should().Be(2);
|
||||
updatedChapter.Images.Should().HaveCount(2);
|
||||
updatedChapter.Images.Select(i => i.OriginalPath).Should().BeEquivalentTo(new[] { image1.Url, image2.Url });
|
||||
updatedChapter.Images.All(i => i.Id != Guid.Empty).Should().BeTrue();
|
||||
@@ -128,9 +134,10 @@ public class NovelUpdateServiceTests
|
||||
.BeEquivalentTo(updatedChapter.Images.Select(img => img.Id.ToString()));
|
||||
|
||||
publishedEvents.Should().HaveCount(2);
|
||||
publishedEvents.Should().OnlyContain(e => e.ImportId == importId);
|
||||
publishedEvents.Select(e => e.RequestId).Should().BeEquivalentTo(updatedChapter.Images.Select(i => i.Id));
|
||||
publishedEvents.Select(e => e.FileData).Should().BeEquivalentTo(new[] { image1.Data, image2.Data });
|
||||
publishedEvents.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]
|
||||
@@ -151,14 +158,14 @@ public class NovelUpdateServiceTests
|
||||
ImageData = new List<ImageData> { image }
|
||||
}));
|
||||
|
||||
var eventBus = Substitute.For<IEventBus>();
|
||||
eventBus.Publish(Arg.Any<FileUploadRequestCreatedEvent>()).Returns(Task.CompletedTask);
|
||||
eventBus.Publish(Arg.Any<object>(), Arg.Any<string>()).Returns(Task.CompletedTask);
|
||||
var publishEndpoint = Substitute.For<IPublishEndpoint>();
|
||||
|
||||
var service = CreateService(dbContext, adapter, eventBus);
|
||||
var service = CreateService(dbContext, adapter, publishEndpoint);
|
||||
|
||||
var updatedChapter = await service.PullChapterContents(novel.Id, volume.Id, chapter.Order);
|
||||
var importId = Guid.NewGuid();
|
||||
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 doc = new HtmlDocument();
|
||||
doc.LoadHtml(storedHtml);
|
||||
@@ -186,8 +193,8 @@ public class NovelUpdateServiceTests
|
||||
await dbContext.SaveChangesAsync();
|
||||
|
||||
var adapter = Substitute.For<ISourceAdapter>();
|
||||
var eventBus = Substitute.For<IEventBus>();
|
||||
var service = CreateService(dbContext, adapter, eventBus);
|
||||
var publishEndpoint = Substitute.For<IPublishEndpoint>();
|
||||
var service = CreateService(dbContext, adapter, publishEndpoint);
|
||||
|
||||
var newUrl = "https://cdn.example.com/uploaded/cover.jpg";
|
||||
|
||||
@@ -228,8 +235,8 @@ public class NovelUpdateServiceTests
|
||||
await dbContext.SaveChangesAsync();
|
||||
|
||||
var adapter = Substitute.For<ISourceAdapter>();
|
||||
var eventBus = Substitute.For<IEventBus>();
|
||||
var service = CreateService(dbContext, adapter, eventBus, pendingUrl);
|
||||
var publishEndpoint = Substitute.For<IPublishEndpoint>();
|
||||
var service = CreateService(dbContext, adapter, publishEndpoint, pendingUrl);
|
||||
|
||||
var newUrl = "https://cdn.example.com/uploaded/image.jpg";
|
||||
|
||||
@@ -277,8 +284,8 @@ public class NovelUpdateServiceTests
|
||||
await dbContext.SaveChangesAsync();
|
||||
|
||||
var adapter = Substitute.For<ISourceAdapter>();
|
||||
var eventBus = Substitute.For<IEventBus>();
|
||||
var service = CreateService(dbContext, adapter, eventBus, pendingUrl);
|
||||
var publishEndpoint = Substitute.For<IPublishEndpoint>();
|
||||
var service = CreateService(dbContext, adapter, publishEndpoint, pendingUrl);
|
||||
|
||||
var newUrl = "https://cdn.example.com/uploaded/img1.jpg";
|
||||
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
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
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using FictionArchive.Common.Enums;
|
||||
using FictionArchive.Service.NovelService.Services;
|
||||
using FictionArchive.Service.Shared.Contracts.Events;
|
||||
using MassTransit;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace FictionArchive.Service.NovelService.Consumers;
|
||||
|
||||
public class FileUploadRequestStatusUpdateConsumer : IConsumer<IFileUploadRequestStatusUpdate>
|
||||
{
|
||||
private readonly ILogger<FileUploadRequestStatusUpdateConsumer> _logger;
|
||||
private readonly NovelServiceDbContext _dbContext;
|
||||
private readonly NovelUpdateService _novelUpdateService;
|
||||
|
||||
public FileUploadRequestStatusUpdateConsumer(
|
||||
ILogger<FileUploadRequestStatusUpdateConsumer> logger,
|
||||
NovelServiceDbContext dbContext,
|
||||
NovelUpdateService novelUpdateService)
|
||||
{
|
||||
_logger = logger;
|
||||
_dbContext = dbContext;
|
||||
_novelUpdateService = novelUpdateService;
|
||||
}
|
||||
|
||||
public async Task Consume(ConsumeContext<IFileUploadRequestStatusUpdate> context)
|
||||
{
|
||||
var message = context.Message;
|
||||
|
||||
var image = await _dbContext.Images.FindAsync(message.RequestId);
|
||||
if (image == null)
|
||||
{
|
||||
// Not a request we care about.
|
||||
return;
|
||||
}
|
||||
|
||||
if (message.Status == RequestStatus.Failed)
|
||||
{
|
||||
_logger.LogError("Image upload failed for image with id {imageId}", image.Id);
|
||||
return;
|
||||
}
|
||||
else if (message.Status == RequestStatus.Success)
|
||||
{
|
||||
_logger.LogInformation("Image upload succeeded for image with id {imageId}", image.Id);
|
||||
await _novelUpdateService.UpdateImage(image.Id, message.FileAccessUrl);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using FictionArchive.Service.NovelService.Models.Localization;
|
||||
using FictionArchive.Service.NovelService.Services;
|
||||
using FictionArchive.Service.Shared.Contracts.Events;
|
||||
using MassTransit;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace FictionArchive.Service.NovelService.Consumers;
|
||||
|
||||
public class TranslationRequestCompletedConsumer : IConsumer<ITranslationRequestCompleted>
|
||||
{
|
||||
private readonly ILogger<TranslationRequestCompletedConsumer> _logger;
|
||||
private readonly NovelServiceDbContext _dbContext;
|
||||
|
||||
public TranslationRequestCompletedConsumer(
|
||||
ILogger<TranslationRequestCompletedConsumer> logger,
|
||||
NovelServiceDbContext dbContext)
|
||||
{
|
||||
_logger = logger;
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
public async Task Consume(ConsumeContext<ITranslationRequestCompleted> context)
|
||||
{
|
||||
var message = context.Message;
|
||||
|
||||
var localizationRequest = await _dbContext.LocalizationRequests
|
||||
.Include(r => r.KeyRequestedForTranslation)
|
||||
.ThenInclude(lk => lk.Texts)
|
||||
.FirstOrDefaultAsync(lk => lk.Id == message.TranslationRequestId);
|
||||
|
||||
if (localizationRequest == null)
|
||||
{
|
||||
// Not one of our requests, discard it
|
||||
return;
|
||||
}
|
||||
|
||||
localizationRequest.KeyRequestedForTranslation.Texts.Add(new LocalizationText
|
||||
{
|
||||
Language = localizationRequest.TranslateTo,
|
||||
Text = message.TranslatedText,
|
||||
TranslationEngine = localizationRequest.Engine
|
||||
});
|
||||
|
||||
_dbContext.LocalizationRequests.Remove(localizationRequest);
|
||||
await _dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
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;
|
||||
@@ -0,0 +1,11 @@
|
||||
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;
|
||||
@@ -0,0 +1,11 @@
|
||||
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,6 +10,7 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="HotChocolate.AspNetCore.CommandLine" Version="15.1.11" />
|
||||
<PackageReference Include="HtmlAgilityPack" Version="1.12.4" />
|
||||
<PackageReference Include="MassTransit.EntityFrameworkCore" Version="8.5.7" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.11">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
using FictionArchive.Service.NovelService.Contracts;
|
||||
using FictionArchive.Service.NovelService.Models.Enums;
|
||||
using FictionArchive.Service.NovelService.Models.IntegrationEvents;
|
||||
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.SourceAdapters;
|
||||
using FictionArchive.Service.Shared.Services.EventBus;
|
||||
using FictionArchive.Service.Shared.Contracts.Events;
|
||||
using HotChocolate.Authorization;
|
||||
using HotChocolate.Types;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
@@ -14,20 +14,22 @@ namespace FictionArchive.Service.NovelService.GraphQL;
|
||||
|
||||
public class Mutation
|
||||
{
|
||||
[Error<InvalidOperationException>]
|
||||
[Authorize]
|
||||
public async Task<NovelUpdateRequestedEvent> ImportNovel(string novelUrl, NovelUpdateService service)
|
||||
public async Task<NovelImportRequested> ImportNovel(string novelUrl, NovelUpdateService service)
|
||||
{
|
||||
return await service.QueueNovelImport(novelUrl);
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
public async Task<ChapterPullRequestedEvent> FetchChapterContents(
|
||||
public async Task<ChapterPullRequested> FetchChapterContents(
|
||||
Guid importId,
|
||||
uint novelId,
|
||||
uint volumeId,
|
||||
uint chapterOrder,
|
||||
NovelUpdateService service)
|
||||
{
|
||||
return await service.QueueChapterPull(novelId, volumeId, chapterOrder);
|
||||
return await service.QueueChapterPull(importId, novelId, volumeId, chapterOrder);
|
||||
}
|
||||
|
||||
[Error<KeyNotFoundException>]
|
||||
|
||||
673
FictionArchive.Service.NovelService/Migrations/20260127161500_AddNovelImportSaga.Designer.cs
generated
Normal file
673
FictionArchive.Service.NovelService/Migrations/20260127161500_AddNovelImportSaga.Designer.cs
generated
Normal file
@@ -0,0 +1,673 @@
|
||||
// <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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
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,6 +23,27 @@ namespace FictionArchive.Service.NovelService.Migrations
|
||||
|
||||
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")
|
||||
@@ -391,6 +412,53 @@ namespace FictionArchive.Service.NovelService.Migrations
|
||||
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")
|
||||
|
||||
10
FictionArchive.Service.NovelService/Models/ActiveImport.cs
Normal file
10
FictionArchive.Service.NovelService/Models/ActiveImport.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
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,13 +0,0 @@
|
||||
using FictionArchive.Service.Shared.Services.EventBus;
|
||||
|
||||
namespace FictionArchive.Service.NovelService.Models.IntegrationEvents;
|
||||
|
||||
public class ChapterCreatedEvent : IIntegrationEvent
|
||||
{
|
||||
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; }
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
using FictionArchive.Service.Shared.Services.EventBus;
|
||||
|
||||
namespace FictionArchive.Service.NovelService.Models.IntegrationEvents;
|
||||
|
||||
public class ChapterPullRequestedEvent : IIntegrationEvent
|
||||
{
|
||||
public uint NovelId { get; set; }
|
||||
public uint VolumeId { get; set; }
|
||||
public uint ChapterOrder { get; set; }
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
using FictionArchive.Service.Shared.Services.EventBus;
|
||||
|
||||
namespace FictionArchive.Service.FileService.IntegrationEvents;
|
||||
|
||||
public class FileUploadRequestCreatedEvent : IIntegrationEvent
|
||||
{
|
||||
public Guid RequestId { get; set; }
|
||||
public string FilePath { get; set; }
|
||||
public byte[] FileData { get; set; }
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
using FictionArchive.Common.Enums;
|
||||
using FictionArchive.Service.Shared.Services.EventBus;
|
||||
|
||||
namespace FictionArchive.Service.NovelService.Models.IntegrationEvents;
|
||||
|
||||
public class FileUploadRequestStatusUpdateEvent : IIntegrationEvent
|
||||
{
|
||||
public Guid RequestId { get; set; }
|
||||
public RequestStatus Status { get; set; }
|
||||
|
||||
#region Success
|
||||
|
||||
public string? FileAccessUrl { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Failure
|
||||
|
||||
public string? ErrorMessage { get; set; }
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
using FictionArchive.Common.Enums;
|
||||
using FictionArchive.Service.Shared.Services.EventBus;
|
||||
|
||||
namespace FictionArchive.Service.NovelService.Models.IntegrationEvents;
|
||||
|
||||
public class NovelCreatedEvent : IIntegrationEvent
|
||||
{
|
||||
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; }
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
using FictionArchive.Service.Shared.Services.EventBus;
|
||||
|
||||
namespace FictionArchive.Service.NovelService.Models.IntegrationEvents;
|
||||
|
||||
public class NovelUpdateRequestedEvent : IIntegrationEvent
|
||||
{
|
||||
public string NovelUrl { get; set; }
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
using FictionArchive.Common.Enums;
|
||||
using FictionArchive.Service.Shared.Services.EventBus;
|
||||
|
||||
namespace FictionArchive.Service.NovelService.Models.IntegrationEvents;
|
||||
|
||||
public class TranslationRequestCompletedEvent : IIntegrationEvent
|
||||
{
|
||||
/// <summary>
|
||||
/// Maps this event back to a triggering request.
|
||||
/// </summary>
|
||||
public Guid? TranslationRequestId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The resulting text.
|
||||
/// </summary>
|
||||
public string? TranslatedText { get; set; }
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
using FictionArchive.Common.Enums;
|
||||
using FictionArchive.Service.Shared.Services.EventBus;
|
||||
|
||||
namespace FictionArchive.Service.NovelService.Models.IntegrationEvents;
|
||||
|
||||
public class TranslationRequestCreatedEvent : IIntegrationEvent
|
||||
{
|
||||
public Guid TranslationRequestId { get; set; }
|
||||
public Language From { get; set; }
|
||||
public Language To { get; set; }
|
||||
public string Body { get; set; }
|
||||
public string TranslationEngineKey { get; set; }
|
||||
}
|
||||
@@ -1,16 +1,17 @@
|
||||
using FictionArchive.Common.Extensions;
|
||||
using FictionArchive.Service.NovelService.Consumers;
|
||||
using FictionArchive.Service.NovelService.GraphQL;
|
||||
using FictionArchive.Service.NovelService.Models.Configuration;
|
||||
using FictionArchive.Service.NovelService.Models.IntegrationEvents;
|
||||
using FictionArchive.Service.NovelService.Sagas;
|
||||
using FictionArchive.Service.NovelService.Services;
|
||||
using FictionArchive.Service.NovelService.Services.EventHandlers;
|
||||
using FictionArchive.Service.NovelService.Services.SourceAdapters;
|
||||
using FictionArchive.Service.NovelService.Services.SourceAdapters.Novelpia;
|
||||
using FictionArchive.Service.Shared;
|
||||
using FictionArchive.Service.Shared.Extensions;
|
||||
using FictionArchive.Service.Shared.Services.EventBus.Implementations;
|
||||
using FictionArchive.Service.Shared.Services.GraphQL;
|
||||
using MassTransit;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using NodaTime;
|
||||
|
||||
namespace FictionArchive.Service.NovelService;
|
||||
|
||||
@@ -25,18 +26,28 @@ public class Program
|
||||
|
||||
builder.Services.AddMemoryCache();
|
||||
|
||||
#region Event Bus
|
||||
#region MassTransit
|
||||
|
||||
if (!isSchemaExport)
|
||||
{
|
||||
builder.Services.AddRabbitMQ(opt =>
|
||||
builder.Services.AddFictionArchiveMassTransit(
|
||||
builder.Configuration,
|
||||
x =>
|
||||
{
|
||||
builder.Configuration.GetSection("RabbitMQ").Bind(opt);
|
||||
})
|
||||
.Subscribe<TranslationRequestCompletedEvent, TranslationRequestCompletedEventHandler>()
|
||||
.Subscribe<NovelUpdateRequestedEvent, NovelUpdateRequestedEventHandler>()
|
||||
.Subscribe<ChapterPullRequestedEvent, ChapterPullRequestedEventHandler>()
|
||||
.Subscribe<FileUploadRequestStatusUpdateEvent, FileUploadRequestStatusUpdateEventHandler>();
|
||||
x.AddConsumer<TranslationRequestCompletedConsumer>();
|
||||
x.AddConsumer<FileUploadRequestStatusUpdateConsumer>();
|
||||
x.AddConsumer<ChapterPullRequestedConsumer>();
|
||||
x.AddConsumer<NovelImportRequestedConsumer>();
|
||||
x.AddConsumer<NovelImportCompletedConsumer>();
|
||||
|
||||
x.AddSagaStateMachine<NovelImportSaga, NovelImportSagaState>()
|
||||
.EntityFrameworkRepository(r =>
|
||||
{
|
||||
r.ConcurrencyMode = ConcurrencyMode.Optimistic;
|
||||
r.ExistingDbContext<NovelServiceDbContext>();
|
||||
r.UsePostgres();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -76,6 +87,9 @@ public class Program
|
||||
|
||||
#endregion
|
||||
|
||||
// Register IClock for saga and service use
|
||||
builder.Services.AddSingleton<IClock>(SystemClock.Instance);
|
||||
|
||||
builder.Services.AddHealthChecks();
|
||||
|
||||
// Authentication & Authorization
|
||||
|
||||
135
FictionArchive.Service.NovelService/Sagas/NovelImportSaga.cs
Normal file
135
FictionArchive.Service.NovelService/Sagas/NovelImportSaga.cs
Normal file
@@ -0,0 +1,135 @@
|
||||
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;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
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; }
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
using FictionArchive.Service.NovelService.Models.IntegrationEvents;
|
||||
using FictionArchive.Service.Shared.Services.EventBus;
|
||||
|
||||
namespace FictionArchive.Service.NovelService.Services.EventHandlers;
|
||||
|
||||
public class ChapterPullRequestedEventHandler : IIntegrationEventHandler<ChapterPullRequestedEvent>
|
||||
{
|
||||
private readonly NovelUpdateService _novelUpdateService;
|
||||
|
||||
public ChapterPullRequestedEventHandler(NovelUpdateService novelUpdateService)
|
||||
{
|
||||
_novelUpdateService = novelUpdateService;
|
||||
}
|
||||
|
||||
public async Task Handle(ChapterPullRequestedEvent @event)
|
||||
{
|
||||
await _novelUpdateService.PullChapterContents(@event.NovelId, @event.VolumeId, @event.ChapterOrder);
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
using FictionArchive.Common.Enums;
|
||||
using FictionArchive.Service.NovelService.Models.IntegrationEvents;
|
||||
using FictionArchive.Service.Shared.Services.EventBus;
|
||||
|
||||
namespace FictionArchive.Service.NovelService.Services.EventHandlers;
|
||||
|
||||
public class FileUploadRequestStatusUpdateEventHandler : IIntegrationEventHandler<FileUploadRequestStatusUpdateEvent>
|
||||
{
|
||||
private readonly ILogger<FileUploadRequestStatusUpdateEventHandler> _logger;
|
||||
private readonly NovelServiceDbContext _context;
|
||||
private readonly NovelUpdateService _novelUpdateService;
|
||||
|
||||
public FileUploadRequestStatusUpdateEventHandler(ILogger<FileUploadRequestStatusUpdateEventHandler> logger, NovelServiceDbContext context, NovelUpdateService novelUpdateService)
|
||||
{
|
||||
_logger = logger;
|
||||
_context = context;
|
||||
_novelUpdateService = novelUpdateService;
|
||||
}
|
||||
|
||||
public async Task Handle(FileUploadRequestStatusUpdateEvent @event)
|
||||
{
|
||||
var image = await _context.Images.FindAsync(@event.RequestId);
|
||||
if (image == null)
|
||||
{
|
||||
// Not a request we care about.
|
||||
return;
|
||||
}
|
||||
if (@event.Status == RequestStatus.Failed)
|
||||
{
|
||||
_logger.LogError("Image upload failed for image with id {imageId}", image.Id);
|
||||
return;
|
||||
}
|
||||
else if (@event.Status == RequestStatus.Success)
|
||||
{
|
||||
_logger.LogInformation("Image upload succeeded for image with id {imageId}", image.Id);
|
||||
await _novelUpdateService.UpdateImage(image.Id, @event.FileAccessUrl);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
using FictionArchive.Service.NovelService.Models.IntegrationEvents;
|
||||
using FictionArchive.Service.Shared.Services.EventBus;
|
||||
|
||||
namespace FictionArchive.Service.NovelService.Services.EventHandlers;
|
||||
|
||||
public class NovelUpdateRequestedEventHandler : IIntegrationEventHandler<NovelUpdateRequestedEvent>
|
||||
{
|
||||
private readonly ILogger<NovelUpdateRequestedEventHandler> _logger;
|
||||
private readonly IEventBus _eventBus;
|
||||
private readonly NovelUpdateService _novelUpdateService;
|
||||
|
||||
public NovelUpdateRequestedEventHandler(ILogger<NovelUpdateRequestedEventHandler> logger, IEventBus eventBus, NovelUpdateService novelUpdateService)
|
||||
{
|
||||
_logger = logger;
|
||||
_eventBus = eventBus;
|
||||
_novelUpdateService = novelUpdateService;
|
||||
}
|
||||
|
||||
public async Task Handle(NovelUpdateRequestedEvent @event)
|
||||
{
|
||||
await _novelUpdateService.ImportNovel(@event.NovelUrl);
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
using FictionArchive.Service.NovelService.Models.IntegrationEvents;
|
||||
using FictionArchive.Service.NovelService.Models.Localization;
|
||||
using FictionArchive.Service.Shared.Services.EventBus;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace FictionArchive.Service.NovelService.Services.EventHandlers;
|
||||
|
||||
public class TranslationRequestCompletedEventHandler : IIntegrationEventHandler<TranslationRequestCompletedEvent>
|
||||
{
|
||||
private readonly ILogger<TranslationRequestCompletedEventHandler> _logger;
|
||||
private readonly NovelServiceDbContext _dbContext;
|
||||
|
||||
public TranslationRequestCompletedEventHandler(ILogger<TranslationRequestCompletedEventHandler> logger, NovelServiceDbContext dbContext)
|
||||
{
|
||||
_logger = logger;
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
public async Task Handle(TranslationRequestCompletedEvent @event)
|
||||
{
|
||||
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
|
||||
return;
|
||||
}
|
||||
|
||||
localizationRequest.KeyRequestedForTranslation.Texts.Add(new LocalizationText()
|
||||
{
|
||||
Language = localizationRequest.TranslateTo,
|
||||
Text = @event.TranslatedText,
|
||||
TranslationEngine = localizationRequest.Engine
|
||||
});
|
||||
_dbContext.LocalizationRequests.Remove(localizationRequest);
|
||||
await _dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
using FictionArchive.Service.NovelService.Models;
|
||||
using FictionArchive.Service.NovelService.Models.Images;
|
||||
using FictionArchive.Service.NovelService.Models.Localization;
|
||||
using FictionArchive.Service.NovelService.Models.Novels;
|
||||
using FictionArchive.Service.NovelService.Sagas;
|
||||
using FictionArchive.Service.Shared.Services.Database;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
@@ -18,6 +20,8 @@ public class NovelServiceDbContext(DbContextOptions options, ILogger<NovelServic
|
||||
public DbSet<LocalizationKey> LocalizationKeys { get; set; }
|
||||
public DbSet<LocalizationRequest> LocalizationRequests { 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)
|
||||
{
|
||||
@@ -36,5 +40,18 @@ public class NovelServiceDbContext(DbContextOptions options, ILogger<NovelServic
|
||||
modelBuilder.Entity<Chapter>()
|
||||
.HasIndex("VolumeId", "Order")
|
||||
.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,17 +1,19 @@
|
||||
using FictionArchive.Common.Enums;
|
||||
using FictionArchive.Service.FileService.IntegrationEvents;
|
||||
using FictionArchive.Service.NovelService.Contracts;
|
||||
using FictionArchive.Service.NovelService.Models;
|
||||
using FictionArchive.Service.NovelService.Models.Configuration;
|
||||
using FictionArchive.Service.NovelService.Models.Enums;
|
||||
using FictionArchive.Service.NovelService.Models.Images;
|
||||
using FictionArchive.Service.NovelService.Models.IntegrationEvents;
|
||||
using FictionArchive.Service.NovelService.Models.Localization;
|
||||
using FictionArchive.Service.NovelService.Models.Novels;
|
||||
using FictionArchive.Service.NovelService.Models.SourceAdapters;
|
||||
using FictionArchive.Service.NovelService.Services.SourceAdapters;
|
||||
using FictionArchive.Service.Shared.Services.EventBus;
|
||||
using FictionArchive.Service.Shared.Contracts.Events;
|
||||
using HtmlAgilityPack;
|
||||
using MassTransit;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Options;
|
||||
using NodaTime;
|
||||
|
||||
namespace FictionArchive.Service.NovelService.Services;
|
||||
|
||||
@@ -20,16 +22,18 @@ public class NovelUpdateService
|
||||
private readonly NovelServiceDbContext _dbContext;
|
||||
private readonly ILogger<NovelUpdateService> _logger;
|
||||
private readonly IEnumerable<ISourceAdapter> _sourceAdapters;
|
||||
private readonly IEventBus _eventBus;
|
||||
private readonly IPublishEndpoint _publishEndpoint;
|
||||
private readonly NovelUpdateServiceConfiguration _novelUpdateServiceConfiguration;
|
||||
private readonly IClock _clock;
|
||||
|
||||
public NovelUpdateService(NovelServiceDbContext dbContext, ILogger<NovelUpdateService> logger, IEnumerable<ISourceAdapter> sourceAdapters, IEventBus eventBus, IOptions<NovelUpdateServiceConfiguration> novelUpdateServiceConfiguration)
|
||||
public NovelUpdateService(NovelServiceDbContext dbContext, ILogger<NovelUpdateService> logger, IEnumerable<ISourceAdapter> sourceAdapters, IPublishEndpoint publishEndpoint, IOptions<NovelUpdateServiceConfiguration> novelUpdateServiceConfiguration, IClock clock)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_logger = logger;
|
||||
_sourceAdapters = sourceAdapters;
|
||||
_eventBus = eventBus;
|
||||
_publishEndpoint = publishEndpoint;
|
||||
_novelUpdateServiceConfiguration = novelUpdateServiceConfiguration.Value;
|
||||
_clock = clock;
|
||||
}
|
||||
|
||||
#region Helper Methods
|
||||
@@ -299,7 +303,7 @@ public class NovelUpdateService
|
||||
|
||||
#endregion
|
||||
|
||||
public async Task<Novel> ImportNovel(string novelUrl)
|
||||
public async Task<Novel> ImportNovel(Guid importId, string novelUrl)
|
||||
{
|
||||
// Step 1: Get metadata from source adapter
|
||||
NovelMetadata? metadata = null;
|
||||
@@ -335,7 +339,8 @@ public class NovelUpdateService
|
||||
.ThenInclude(volume => volume.Chapters)
|
||||
.ThenInclude(chapter => chapter.Body)
|
||||
.ThenInclude(localizationKey => localizationKey.Texts)
|
||||
.Include(n => n.CoverImage)
|
||||
.Include(n => n.CoverImage).Include(novel => novel.Volumes).ThenInclude(volume => volume.Chapters)
|
||||
.ThenInclude(chapter => chapter.Name)
|
||||
.FirstOrDefaultAsync(n =>
|
||||
n.ExternalId == metadata.ExternalId &&
|
||||
n.Source.Key == metadata.SourceDescriptor.Key);
|
||||
@@ -393,14 +398,12 @@ public class NovelUpdateService
|
||||
// Publish novel created event for new novels
|
||||
if (existingNovel == null)
|
||||
{
|
||||
await _eventBus.Publish(new NovelCreatedEvent
|
||||
{
|
||||
NovelId = novel.Id,
|
||||
Title = novel.Name.Texts.First(t => t.Language == novel.RawLanguage).Text,
|
||||
OriginalLanguage = novel.RawLanguage,
|
||||
Source = novel.Source.Key,
|
||||
AuthorName = novel.Author.Name.Texts.First(t => t.Language == novel.RawLanguage).Text
|
||||
});
|
||||
await _publishEndpoint.Publish<INovelCreated>(new NovelCreated(
|
||||
novel.Id,
|
||||
novel.Name.Texts.First(t => t.Language == novel.RawLanguage).Text,
|
||||
novel.RawLanguage,
|
||||
novel.Source.Key,
|
||||
novel.Author.Name.Texts.First(t => t.Language == novel.RawLanguage).Text));
|
||||
}
|
||||
|
||||
// Publish chapter created events for new chapters
|
||||
@@ -408,51 +411,60 @@ public class NovelUpdateService
|
||||
{
|
||||
foreach (var chapter in volume.Chapters.Where(c => !existingChapterIds.Contains(c.Id)))
|
||||
{
|
||||
await _eventBus.Publish(new ChapterCreatedEvent
|
||||
{
|
||||
ChapterId = chapter.Id,
|
||||
NovelId = novel.Id,
|
||||
VolumeId = volume.Id,
|
||||
VolumeOrder = volume.Order,
|
||||
ChapterOrder = chapter.Order,
|
||||
ChapterTitle = chapter.Name.Texts.First(t => t.Language == novel.RawLanguage).Text
|
||||
});
|
||||
await _publishEndpoint.Publish<IChapterCreated>(new ChapterCreated(
|
||||
chapter.Id,
|
||||
novel.Id,
|
||||
volume.Id,
|
||||
(uint)volume.Order,
|
||||
chapter.Order,
|
||||
chapter.Name.Texts.First(t => t.Language == novel.RawLanguage).Text));
|
||||
}
|
||||
}
|
||||
|
||||
// Count chapters that need pulling
|
||||
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)
|
||||
{
|
||||
await _eventBus.Publish(new FileUploadRequestCreatedEvent
|
||||
{
|
||||
RequestId = novel.CoverImage.Id,
|
||||
FileData = metadata.CoverImage.Data,
|
||||
FilePath = $"Novels/{novel.Id}/Images/cover.jpg"
|
||||
});
|
||||
await _publishEndpoint.Publish<IFileUploadRequestCreated>(new FileUploadRequestCreated(
|
||||
importId,
|
||||
novel.CoverImage.Id,
|
||||
$"Novels/{novel.Id}/Images/cover.jpg",
|
||||
metadata.CoverImage.Data));
|
||||
}
|
||||
|
||||
// Publish chapter pull events for chapters without body content
|
||||
foreach (var volume in novel.Volumes)
|
||||
{
|
||||
var chaptersNeedingPull = volume.Chapters
|
||||
var volumeChaptersNeedingPull = volume.Chapters
|
||||
.Where(c => c.Body?.Texts == null || !c.Body.Texts.Any())
|
||||
.ToList();
|
||||
|
||||
foreach (var chapter in chaptersNeedingPull)
|
||||
foreach (var chapter in volumeChaptersNeedingPull)
|
||||
{
|
||||
await _eventBus.Publish(new ChapterPullRequestedEvent
|
||||
{
|
||||
NovelId = novel.Id,
|
||||
VolumeId = volume.Id,
|
||||
ChapterOrder = chapter.Order
|
||||
});
|
||||
await _publishEndpoint.Publish<IChapterPullRequested>(new ChapterPullRequested(
|
||||
importId,
|
||||
novel.Id,
|
||||
volume.Id,
|
||||
chapter.Order));
|
||||
}
|
||||
}
|
||||
|
||||
return novel;
|
||||
}
|
||||
|
||||
public async Task<Chapter> PullChapterContents(uint novelId, uint volumeId, uint chapterOrder)
|
||||
public async Task<(Chapter chapter, int imageCount)> PullChapterContents(Guid importId, uint novelId, uint volumeId, uint chapterOrder)
|
||||
{
|
||||
var novel = await _dbContext.Novels.Where(novel => novel.Id == novelId)
|
||||
.Include(novel => novel.Volumes)
|
||||
@@ -518,15 +530,14 @@ public class NovelUpdateService
|
||||
foreach (var image in chapter.Images)
|
||||
{
|
||||
var data = rawChapter.ImageData.FirstOrDefault(img => img.Url == image.OriginalPath);
|
||||
await _eventBus.Publish(new FileUploadRequestCreatedEvent()
|
||||
{
|
||||
FileData = data.Data,
|
||||
FilePath = $"{novel.Id}/Images/Chapter-{chapter.Id}/{imgCount++}.jpg",
|
||||
RequestId = image.Id
|
||||
});
|
||||
await _publishEndpoint.Publish<IFileUploadRequestCreated>(new FileUploadRequestCreated(
|
||||
importId,
|
||||
image.Id,
|
||||
$"Novels/{novel.Id}/Images/Chapter-{chapter.Id}/{imgCount++}.jpg",
|
||||
data.Data));
|
||||
}
|
||||
|
||||
return chapter;
|
||||
return (chapter, chapter.Images.Count);
|
||||
}
|
||||
|
||||
public async Task UpdateImage(Guid imageId, string newUrl)
|
||||
@@ -557,25 +568,35 @@ public class NovelUpdateService
|
||||
await _dbContext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task<NovelUpdateRequestedEvent> QueueNovelImport(string novelUrl)
|
||||
public async Task<NovelImportRequested> QueueNovelImport(string novelUrl)
|
||||
{
|
||||
var importNovelRequestEvent = new NovelUpdateRequestedEvent()
|
||||
var importId = Guid.NewGuid();
|
||||
var activeImport = new ActiveImport
|
||||
{
|
||||
NovelUrl = novelUrl
|
||||
ImportId = importId,
|
||||
NovelUrl = novelUrl,
|
||||
StartedAt = _clock.GetCurrentInstant()
|
||||
};
|
||||
await _eventBus.Publish(importNovelRequestEvent);
|
||||
|
||||
try
|
||||
{
|
||||
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);
|
||||
await _publishEndpoint.Publish<INovelImportRequested>(importNovelRequestEvent);
|
||||
return importNovelRequestEvent;
|
||||
}
|
||||
|
||||
public async Task<ChapterPullRequestedEvent> QueueChapterPull(uint novelId, uint volumeId, uint chapterOrder)
|
||||
public async Task<ChapterPullRequested> QueueChapterPull(Guid importId, uint novelId, uint volumeId, uint chapterOrder)
|
||||
{
|
||||
var chapterPullEvent = new ChapterPullRequestedEvent()
|
||||
{
|
||||
NovelId = novelId,
|
||||
VolumeId = volumeId,
|
||||
ChapterOrder = chapterOrder
|
||||
};
|
||||
await _eventBus.Publish(chapterPullEvent);
|
||||
var chapterPullEvent = new ChapterPullRequested(importId, novelId, volumeId, chapterOrder);
|
||||
await _publishEndpoint.Publish<IChapterPullRequested>(chapterPullEvent);
|
||||
return chapterPullEvent;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
"Microsoft.AspNetCore": "Warning",
|
||||
"Microsoft.EntityFrameworkCore": "Warning"
|
||||
}
|
||||
},
|
||||
"Novelpia": {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using FictionArchive.Service.Shared.Services.EventBus;
|
||||
using MassTransit;
|
||||
using Newtonsoft.Json;
|
||||
using Quartz;
|
||||
|
||||
@@ -6,15 +6,15 @@ namespace FictionArchive.Service.SchedulerService.Models.JobTemplates;
|
||||
|
||||
public class EventJobTemplate : IJob
|
||||
{
|
||||
private readonly IEventBus _eventBus;
|
||||
private readonly IBus _bus;
|
||||
private readonly ILogger<EventJobTemplate> _logger;
|
||||
|
||||
public const string EventTypeParameter = "RoutingKey";
|
||||
public const string EventDataParameter = "MessageData";
|
||||
|
||||
public EventJobTemplate(IEventBus eventBus, ILogger<EventJobTemplate> logger)
|
||||
public EventJobTemplate(IBus bus, ILogger<EventJobTemplate> logger)
|
||||
{
|
||||
_eventBus = eventBus;
|
||||
_bus = bus;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ public class EventJobTemplate : IJob
|
||||
var eventData = context.MergedJobDataMap.GetString(EventDataParameter);
|
||||
var eventType = context.MergedJobDataMap.GetString(EventTypeParameter);
|
||||
var eventObject = JsonConvert.DeserializeObject(eventData);
|
||||
await _eventBus.Publish(eventObject, eventType);
|
||||
await _bus.Publish(eventObject);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
@@ -2,7 +2,6 @@ using FictionArchive.Service.SchedulerService.GraphQL;
|
||||
using FictionArchive.Service.SchedulerService.Services;
|
||||
using FictionArchive.Service.Shared;
|
||||
using FictionArchive.Service.Shared.Extensions;
|
||||
using FictionArchive.Service.Shared.Services.EventBus.Implementations;
|
||||
using Quartz;
|
||||
using Quartz.Impl.AdoJobStore;
|
||||
|
||||
@@ -38,10 +37,7 @@ public class Program
|
||||
|
||||
if (!isSchemaExport)
|
||||
{
|
||||
builder.Services.AddRabbitMQ(opt =>
|
||||
{
|
||||
builder.Configuration.GetSection("RabbitMQ").Bind(opt);
|
||||
});
|
||||
builder.Services.AddFictionArchiveMassTransit(builder.Configuration);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
using System.Data;
|
||||
using FictionArchive.Service.SchedulerService.Models;
|
||||
using FictionArchive.Service.SchedulerService.Models.JobTemplates;
|
||||
using FictionArchive.Service.Shared.Services.EventBus;
|
||||
using Quartz;
|
||||
using Quartz.Impl.Matchers;
|
||||
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
"Microsoft.AspNetCore": "Warning",
|
||||
"Microsoft.EntityFrameworkCore": "Warning"
|
||||
}
|
||||
},
|
||||
"RabbitMQ": {
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
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; }
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
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;
|
||||
@@ -0,0 +1,11 @@
|
||||
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;
|
||||
@@ -0,0 +1,11 @@
|
||||
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;
|
||||
@@ -0,0 +1,14 @@
|
||||
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;
|
||||
@@ -0,0 +1,12 @@
|
||||
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; }
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
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;
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace FictionArchive.Service.Shared.Contracts.Events;
|
||||
|
||||
public interface INovelImportRequested
|
||||
{
|
||||
Guid ImportId { get; }
|
||||
string NovelUrl { get; }
|
||||
}
|
||||
|
||||
public record NovelImportRequested(Guid ImportId, string NovelUrl) : INovelImportRequested;
|
||||
@@ -0,0 +1,10 @@
|
||||
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;
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace FictionArchive.Service.Shared.Contracts.Events;
|
||||
|
||||
public interface ITranslationRequestCompleted
|
||||
{
|
||||
Guid? TranslationRequestId { get; }
|
||||
string? TranslatedText { get; }
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
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; }
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
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; }
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
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,7 @@
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime" Version="9.0.4" />
|
||||
<PackageReference Include="Polly" Version="8.6.5" />
|
||||
<PackageReference Include="RabbitMQ.Client" Version="7.2.0" />
|
||||
<PackageReference Include="MassTransit.RabbitMQ" Version="8.*" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.11" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace FictionArchive.Service.Shared.Services.EventBus;
|
||||
|
||||
public class EventBusBuilder<TEventBus> where TEventBus : class, IEventBus
|
||||
{
|
||||
private readonly IServiceCollection _services;
|
||||
private readonly SubscriptionManager _subscriptionManager;
|
||||
|
||||
public EventBusBuilder(IServiceCollection services)
|
||||
{
|
||||
_services = services;
|
||||
_services.AddSingleton<IEventBus, TEventBus>();
|
||||
|
||||
_subscriptionManager = new SubscriptionManager();
|
||||
_services.AddSingleton<SubscriptionManager>(_subscriptionManager);
|
||||
}
|
||||
|
||||
public EventBusBuilder<TEventBus> Subscribe<TEvent, TEventHandler>() where TEvent : IIntegrationEvent where TEventHandler : class, IIntegrationEventHandler<TEvent>
|
||||
{
|
||||
_services.AddKeyedTransient<IIntegrationEventHandler, TEventHandler>(typeof(TEvent).Name);
|
||||
_subscriptionManager.RegisterSubscription<TEvent>();
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace FictionArchive.Service.Shared.Services.EventBus;
|
||||
|
||||
public static class EventBusExtensions
|
||||
{
|
||||
public static EventBusBuilder<TEventBus> AddEventBus<TEventBus>(this IServiceCollection services)
|
||||
where TEventBus : class, IEventBus
|
||||
{
|
||||
return new EventBusBuilder<TEventBus>(services);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
namespace FictionArchive.Service.Shared.Services.EventBus;
|
||||
|
||||
public interface IEventBus
|
||||
{
|
||||
Task Publish<TEvent>(TEvent integrationEvent) where TEvent : IIntegrationEvent;
|
||||
Task Publish(object integrationEvent, string eventType);
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
using NodaTime;
|
||||
|
||||
namespace FictionArchive.Service.Shared.Services.EventBus;
|
||||
|
||||
public interface IIntegrationEvent
|
||||
{
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
namespace FictionArchive.Service.Shared.Services.EventBus;
|
||||
|
||||
public interface IIntegrationEventHandler<in TEvent> : IIntegrationEventHandler where TEvent : IIntegrationEvent
|
||||
{
|
||||
Task Handle(TEvent @event);
|
||||
Task IIntegrationEventHandler.Handle(IIntegrationEvent @event) => Handle((TEvent)@event);
|
||||
}
|
||||
|
||||
public interface IIntegrationEventHandler
|
||||
{
|
||||
Task Handle(IIntegrationEvent @event);
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
using RabbitMQ.Client;
|
||||
|
||||
namespace FictionArchive.Service.Shared.Services.EventBus.Implementations;
|
||||
|
||||
public class RabbitMQConnectionProvider
|
||||
{
|
||||
private readonly IConnectionFactory _connectionFactory;
|
||||
|
||||
private IConnection Connection { get; set; }
|
||||
private IChannel DefaultChannel { get; set; }
|
||||
|
||||
public RabbitMQConnectionProvider(IConnectionFactory connectionFactory)
|
||||
{
|
||||
_connectionFactory = connectionFactory;
|
||||
}
|
||||
|
||||
public async Task<IConnection> GetConnectionAsync()
|
||||
{
|
||||
if (Connection == null)
|
||||
{
|
||||
Connection = await _connectionFactory.CreateConnectionAsync();
|
||||
}
|
||||
|
||||
return Connection;
|
||||
}
|
||||
|
||||
public async Task<IChannel> GetDefaultChannelAsync()
|
||||
{
|
||||
if (DefaultChannel == null)
|
||||
{
|
||||
DefaultChannel = await (await GetConnectionAsync()).CreateChannelAsync();
|
||||
}
|
||||
return DefaultChannel;
|
||||
}
|
||||
}
|
||||
@@ -1,137 +0,0 @@
|
||||
using System.Text;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Newtonsoft.Json;
|
||||
using NodaTime;
|
||||
using NodaTime.Serialization.JsonNet;
|
||||
using RabbitMQ.Client;
|
||||
using RabbitMQ.Client.Events;
|
||||
|
||||
namespace FictionArchive.Service.Shared.Services.EventBus.Implementations;
|
||||
|
||||
public class RabbitMQEventBus : IEventBus, IHostedService
|
||||
{
|
||||
private readonly IServiceScopeFactory _serviceScopeFactory;
|
||||
private readonly RabbitMQConnectionProvider _connectionProvider;
|
||||
private readonly RabbitMQOptions _options;
|
||||
private readonly SubscriptionManager _subscriptionManager;
|
||||
private readonly ILogger<RabbitMQEventBus> _logger;
|
||||
|
||||
private readonly JsonSerializerSettings _jsonSerializerSettings;
|
||||
|
||||
private const string ExchangeName = "fiction-archive-event-bus";
|
||||
private const string CreatedAtHeader = "X-Created-At";
|
||||
private const string EventIdHeader = "X-Event-Id";
|
||||
|
||||
public RabbitMQEventBus(IServiceScopeFactory serviceScopeFactory, RabbitMQConnectionProvider connectionProvider, IOptions<RabbitMQOptions> options, SubscriptionManager subscriptionManager, ILogger<RabbitMQEventBus> logger)
|
||||
{
|
||||
_serviceScopeFactory = serviceScopeFactory;
|
||||
_connectionProvider = connectionProvider;
|
||||
_subscriptionManager = subscriptionManager;
|
||||
_logger = logger;
|
||||
_options = options.Value;
|
||||
_jsonSerializerSettings = new JsonSerializerSettings().ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
|
||||
}
|
||||
|
||||
public async Task Publish<TEvent>(TEvent integrationEvent) where TEvent : IIntegrationEvent
|
||||
{
|
||||
var routingKey = typeof(TEvent).Name;
|
||||
|
||||
await Publish(integrationEvent, routingKey);
|
||||
}
|
||||
|
||||
public async Task Publish(object integrationEvent, string eventType)
|
||||
{
|
||||
var channel = await _connectionProvider.GetDefaultChannelAsync();
|
||||
var body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(integrationEvent));
|
||||
|
||||
// headers
|
||||
var props = new BasicProperties();
|
||||
props.Headers = new Dictionary<string, object?>()
|
||||
{
|
||||
{ CreatedAtHeader, Instant.FromDateTimeUtc(DateTime.UtcNow).ToString() },
|
||||
{ EventIdHeader, Guid.NewGuid().ToString() }
|
||||
};
|
||||
|
||||
await channel.BasicPublishAsync(ExchangeName, eventType, true, props, body);
|
||||
_logger.LogInformation("Published event {EventName}", eventType);
|
||||
}
|
||||
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_ = Task.Factory.StartNew(async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var channel = await _connectionProvider.GetDefaultChannelAsync();
|
||||
await channel.ExchangeDeclareAsync(ExchangeName, ExchangeType.Direct,
|
||||
cancellationToken: cancellationToken);
|
||||
|
||||
await channel.BasicQosAsync(prefetchSize: 0, prefetchCount: 1, global: false, cancellationToken: cancellationToken);
|
||||
await channel.QueueDeclareAsync(_options.ClientIdentifier, true, false, false,
|
||||
cancellationToken: cancellationToken);
|
||||
var consumer = new AsyncEventingBasicConsumer(channel);
|
||||
consumer.ReceivedAsync += (sender, @event) =>
|
||||
{
|
||||
return OnReceivedEvent(sender, @event, channel);
|
||||
};
|
||||
|
||||
await channel.BasicConsumeAsync(_options.ClientIdentifier, false, consumer, cancellationToken: cancellationToken);
|
||||
|
||||
foreach (var subscription in _subscriptionManager.Subscriptions)
|
||||
{
|
||||
await channel.QueueBindAsync(_options.ClientIdentifier, ExchangeName, subscription.Key,
|
||||
cancellationToken: cancellationToken);
|
||||
_logger.LogInformation("Subscribed to {SubscriptionKey}", subscription.Key);
|
||||
}
|
||||
|
||||
_logger.LogInformation("RabbitMQ EventBus started.");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "An error occurred while starting the RabbitMQ EventBus");
|
||||
}
|
||||
}, cancellationToken);
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private async Task OnReceivedEvent(object sender, BasicDeliverEventArgs @event, IChannel channel)
|
||||
{
|
||||
var eventName = @event.RoutingKey;
|
||||
_logger.LogInformation("Received event {EventName}", eventName);
|
||||
try
|
||||
{
|
||||
if (!_subscriptionManager.Subscriptions.ContainsKey(eventName))
|
||||
{
|
||||
_logger.LogWarning("Received event without subscription entry.");
|
||||
return;
|
||||
}
|
||||
|
||||
var eventBody = Encoding.UTF8.GetString(@event.Body.Span);
|
||||
var eventObject = JsonConvert.DeserializeObject(eventBody, _subscriptionManager.Subscriptions[eventName], _jsonSerializerSettings) as IIntegrationEvent;
|
||||
|
||||
using var scope = _serviceScopeFactory.CreateScope();
|
||||
|
||||
foreach (var service in scope.ServiceProvider.GetKeyedServices<IIntegrationEventHandler>(eventName))
|
||||
{
|
||||
await service.Handle(eventObject);
|
||||
}
|
||||
_logger.LogInformation("Finished handling event with name {EventName}", eventName);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "An error occurred while handling an event.");
|
||||
}
|
||||
finally
|
||||
{
|
||||
await channel.BasicAckAsync(@event.DeliveryTag, false);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using RabbitMQ.Client;
|
||||
|
||||
namespace FictionArchive.Service.Shared.Services.EventBus.Implementations;
|
||||
|
||||
public static class RabbitMQExtensions
|
||||
{
|
||||
public static EventBusBuilder<RabbitMQEventBus> AddRabbitMQ(this IServiceCollection services, Action<RabbitMQOptions> configure)
|
||||
{
|
||||
services.Configure(configure);
|
||||
services.AddSingleton<IConnectionFactory, ConnectionFactory>(provider =>
|
||||
{
|
||||
var options = provider.GetService<IOptions<RabbitMQOptions>>();
|
||||
ConnectionFactory factory = new ConnectionFactory();
|
||||
factory.Uri = new Uri(options.Value.ConnectionString);
|
||||
return factory;
|
||||
});
|
||||
services.AddSingleton<RabbitMQConnectionProvider>();
|
||||
services.AddHostedService<RabbitMQEventBus>();
|
||||
return services.AddEventBus<RabbitMQEventBus>();
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
namespace FictionArchive.Service.Shared.Services.EventBus.Implementations;
|
||||
|
||||
public class RabbitMQOptions
|
||||
{
|
||||
public string ConnectionString { get; set; }
|
||||
public string ClientIdentifier { get; set; }
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
namespace FictionArchive.Service.Shared.Services.EventBus;
|
||||
|
||||
public class SubscriptionManager
|
||||
{
|
||||
public Dictionary<string, Type> Subscriptions { get; } = new Dictionary<string, Type>();
|
||||
|
||||
public void RegisterSubscription<TEvent>()
|
||||
{
|
||||
Subscriptions.Add(typeof(TEvent).Name, typeof(TEvent));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
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");
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
using FictionArchive.Service.Shared.Contracts.Events;
|
||||
|
||||
namespace FictionArchive.Service.TranslationService.Contracts;
|
||||
|
||||
public record TranslationRequestCompleted(
|
||||
Guid? TranslationRequestId,
|
||||
string? TranslatedText) : ITranslationRequestCompleted;
|
||||
@@ -1,18 +0,0 @@
|
||||
using FictionArchive.Common.Enums;
|
||||
using FictionArchive.Service.Shared.Services.EventBus;
|
||||
using FictionArchive.Service.TranslationService.Models.Enums;
|
||||
|
||||
namespace FictionArchive.Service.TranslationService.Models.IntegrationEvents;
|
||||
|
||||
public class TranslationRequestCompletedEvent : IIntegrationEvent
|
||||
{
|
||||
/// <summary>
|
||||
/// Maps this event back to a triggering request.
|
||||
/// </summary>
|
||||
public Guid? TranslationRequestId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The resulting text.
|
||||
/// </summary>
|
||||
public string? TranslatedText { get; set; }
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
using FictionArchive.Common.Enums;
|
||||
using FictionArchive.Service.Shared.Services.EventBus;
|
||||
|
||||
namespace FictionArchive.Service.TranslationService.Models.IntegrationEvents;
|
||||
|
||||
public class TranslationRequestCreatedEvent : IIntegrationEvent
|
||||
{
|
||||
public Guid TranslationRequestId { get; set; }
|
||||
public Language From { get; set; }
|
||||
public Language To { get; set; }
|
||||
public string Body { get; set; }
|
||||
public string TranslationEngineKey { get; set; }
|
||||
}
|
||||
@@ -2,16 +2,13 @@ using DeepL;
|
||||
using FictionArchive.Common.Extensions;
|
||||
using FictionArchive.Service.Shared;
|
||||
using FictionArchive.Service.Shared.Extensions;
|
||||
using FictionArchive.Service.Shared.Services.EventBus.Implementations;
|
||||
using FictionArchive.Service.Shared.Services.GraphQL;
|
||||
using FictionArchive.Service.TranslationService.Consumers;
|
||||
using FictionArchive.Service.TranslationService.GraphQL;
|
||||
using FictionArchive.Service.TranslationService.Models.IntegrationEvents;
|
||||
using FictionArchive.Service.TranslationService.Services;
|
||||
using FictionArchive.Service.TranslationService.Services.Database;
|
||||
using FictionArchive.Service.TranslationService.Services.EventHandlers;
|
||||
using FictionArchive.Service.TranslationService.Services.TranslationEngines;
|
||||
using FictionArchive.Service.TranslationService.Services.TranslationEngines.DeepLTranslate;
|
||||
using RabbitMQ.Client;
|
||||
|
||||
namespace FictionArchive.Service.TranslationService;
|
||||
|
||||
@@ -26,15 +23,16 @@ public class Program
|
||||
|
||||
builder.Services.AddHealthChecks();
|
||||
|
||||
#region Event Bus
|
||||
#region MassTransit
|
||||
|
||||
if (!isSchemaExport)
|
||||
{
|
||||
builder.Services.AddRabbitMQ(opt =>
|
||||
builder.Services.AddFictionArchiveMassTransit(
|
||||
builder.Configuration,
|
||||
x =>
|
||||
{
|
||||
builder.Configuration.GetSection("RabbitMQ").Bind(opt);
|
||||
})
|
||||
.Subscribe<TranslationRequestCreatedEvent, TranslationRequestCreatedEventHandler>();
|
||||
x.AddConsumer<TranslationRequestCreatedConsumer>();
|
||||
});
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
using FictionArchive.Service.Shared.Services.EventBus;
|
||||
using FictionArchive.Service.TranslationService.Models.Enums;
|
||||
using FictionArchive.Service.TranslationService.Models.IntegrationEvents;
|
||||
|
||||
namespace FictionArchive.Service.TranslationService.Services.EventHandlers;
|
||||
|
||||
public class TranslationRequestCreatedEventHandler : IIntegrationEventHandler<TranslationRequestCreatedEvent>
|
||||
{
|
||||
private readonly ILogger<TranslationRequestCreatedEventHandler> _logger;
|
||||
private readonly TranslationEngineService _translationEngineService;
|
||||
private readonly IEventBus _eventBus;
|
||||
|
||||
public TranslationRequestCreatedEventHandler(ILogger<TranslationRequestCreatedEventHandler> logger, TranslationEngineService translationEngineService)
|
||||
{
|
||||
_logger = logger;
|
||||
_translationEngineService = translationEngineService;
|
||||
}
|
||||
|
||||
public async Task Handle(TranslationRequestCreatedEvent @event)
|
||||
{
|
||||
var result = await _translationEngineService.Translate(@event.From, @event.To, @event.Body, @event.TranslationEngineKey);
|
||||
if (result.Status == TranslationRequestStatus.Success)
|
||||
{
|
||||
await _eventBus.Publish(new TranslationRequestCompletedEvent()
|
||||
{
|
||||
TranslatedText = result.TranslatedText,
|
||||
TranslationRequestId = @event.TranslationRequestId,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,28 +1,21 @@
|
||||
using System.Text;
|
||||
using FictionArchive.Common.Enums;
|
||||
using FictionArchive.Service.Shared.Services.EventBus;
|
||||
using FictionArchive.Service.Shared.Services.EventBus.Implementations;
|
||||
using FictionArchive.Service.TranslationService.Models;
|
||||
using FictionArchive.Service.TranslationService.Models.Database;
|
||||
using FictionArchive.Service.TranslationService.Models.Enums;
|
||||
using FictionArchive.Service.TranslationService.Models.IntegrationEvents;
|
||||
using FictionArchive.Service.TranslationService.Services.Database;
|
||||
using FictionArchive.Service.TranslationService.Services.TranslationEngines;
|
||||
using RabbitMQ.Client;
|
||||
|
||||
namespace FictionArchive.Service.TranslationService.Services;
|
||||
|
||||
public class TranslationEngineService
|
||||
{
|
||||
private readonly IEnumerable<ITranslationEngine> _translationEngines;
|
||||
private readonly IEventBus _eventBus;
|
||||
private readonly TranslationServiceDbContext _dbContext;
|
||||
|
||||
public TranslationEngineService(IEnumerable<ITranslationEngine> translationEngines, TranslationServiceDbContext dbContext, IEventBus eventBus)
|
||||
public TranslationEngineService(IEnumerable<ITranslationEngine> translationEngines, TranslationServiceDbContext dbContext)
|
||||
{
|
||||
_translationEngines = translationEngines;
|
||||
_dbContext = dbContext;
|
||||
_eventBus = eventBus;
|
||||
}
|
||||
|
||||
public async Task<TranslationResult> Translate(Language from, Language to, string text, string translationEngineKey)
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
"Microsoft.AspNetCore": "Warning",
|
||||
"Microsoft.EntityFrameworkCore": "Warning"
|
||||
}
|
||||
},
|
||||
"DeepL": {
|
||||
|
||||
@@ -1,53 +1,56 @@
|
||||
using FictionArchive.Service.Shared.Services.EventBus;
|
||||
using FictionArchive.Service.Shared.Contracts.Events;
|
||||
using FictionArchive.Service.UserNovelDataService.Models.Database;
|
||||
using FictionArchive.Service.UserNovelDataService.Models.IntegrationEvents;
|
||||
using FictionArchive.Service.UserNovelDataService.Services;
|
||||
using MassTransit;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace FictionArchive.Service.UserNovelDataService.Services.EventHandlers;
|
||||
namespace FictionArchive.Service.UserNovelDataService.Consumers;
|
||||
|
||||
public class ChapterCreatedEventHandler : IIntegrationEventHandler<ChapterCreatedEvent>
|
||||
public class ChapterCreatedConsumer : IConsumer<IChapterCreated>
|
||||
{
|
||||
private readonly ILogger<ChapterCreatedConsumer> _logger;
|
||||
private readonly UserNovelDataServiceDbContext _dbContext;
|
||||
private readonly ILogger<ChapterCreatedEventHandler> _logger;
|
||||
|
||||
public ChapterCreatedEventHandler(
|
||||
UserNovelDataServiceDbContext dbContext,
|
||||
ILogger<ChapterCreatedEventHandler> logger)
|
||||
public ChapterCreatedConsumer(
|
||||
ILogger<ChapterCreatedConsumer> logger,
|
||||
UserNovelDataServiceDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_logger = logger;
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
public async Task Handle(ChapterCreatedEvent @event)
|
||||
public async Task Consume(ConsumeContext<IChapterCreated> context)
|
||||
{
|
||||
var message = context.Message;
|
||||
|
||||
// Ensure novel exists
|
||||
var novelExists = await _dbContext.Novels.AnyAsync(n => n.Id == @event.NovelId);
|
||||
var novelExists = await _dbContext.Novels.AnyAsync(n => n.Id == message.NovelId);
|
||||
if (!novelExists)
|
||||
{
|
||||
var novel = new Novel { Id = @event.NovelId };
|
||||
var novel = new Novel { Id = message.NovelId };
|
||||
_dbContext.Novels.Add(novel);
|
||||
}
|
||||
|
||||
// Ensure volume exists
|
||||
var volumeExists = await _dbContext.Volumes.AnyAsync(v => v.Id == @event.VolumeId);
|
||||
var volumeExists = await _dbContext.Volumes.AnyAsync(v => v.Id == message.VolumeId);
|
||||
if (!volumeExists)
|
||||
{
|
||||
var volume = new Volume { Id = @event.VolumeId };
|
||||
var volume = new Volume { Id = message.VolumeId, NovelId = message.NovelId };
|
||||
_dbContext.Volumes.Add(volume);
|
||||
}
|
||||
|
||||
// Create chapter if not exists
|
||||
var chapterExists = await _dbContext.Chapters.AnyAsync(c => c.Id == @event.ChapterId);
|
||||
var chapterExists = await _dbContext.Chapters.AnyAsync(c => c.Id == message.ChapterId);
|
||||
if (chapterExists)
|
||||
{
|
||||
_logger.LogDebug("Chapter {ChapterId} already exists, skipping", @event.ChapterId);
|
||||
_logger.LogDebug("Chapter {ChapterId} already exists, skipping", message.ChapterId);
|
||||
return;
|
||||
}
|
||||
|
||||
var chapter = new Chapter { Id = @event.ChapterId };
|
||||
var chapter = new Chapter { Id = message.ChapterId, VolumeId = message.VolumeId };
|
||||
_dbContext.Chapters.Add(chapter);
|
||||
await _dbContext.SaveChangesAsync();
|
||||
|
||||
_logger.LogInformation("Created chapter stub for {ChapterId} in novel {NovelId}", @event.ChapterId, @event.NovelId);
|
||||
_logger.LogInformation("Created chapter stub for {ChapterId} in novel {NovelId}", message.ChapterId, message.NovelId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -106,4 +106,393 @@ public class Mutation
|
||||
|
||||
return new BookmarkPayload { Success = true };
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[Error<InvalidOperationException>]
|
||||
public async Task<ReadingListPayload> CreateReadingList(
|
||||
UserNovelDataServiceDbContext dbContext,
|
||||
ClaimsPrincipal claimsPrincipal,
|
||||
CreateReadingListInput input)
|
||||
{
|
||||
var oAuthProviderId = claimsPrincipal.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
||||
if (string.IsNullOrEmpty(oAuthProviderId))
|
||||
{
|
||||
throw new InvalidOperationException("Unable to determine current user identity");
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(input.Name))
|
||||
{
|
||||
throw new InvalidOperationException("Reading list name is required");
|
||||
}
|
||||
|
||||
var user = await dbContext.Users
|
||||
.FirstOrDefaultAsync(u => u.OAuthProviderId == oAuthProviderId);
|
||||
|
||||
if (user == null)
|
||||
{
|
||||
user = new User { OAuthProviderId = oAuthProviderId };
|
||||
dbContext.Users.Add(user);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
var readingList = new ReadingList
|
||||
{
|
||||
UserId = user.Id,
|
||||
Name = input.Name.Trim(),
|
||||
Description = input.Description?.Trim()
|
||||
};
|
||||
|
||||
dbContext.ReadingLists.Add(readingList);
|
||||
await dbContext.SaveChangesAsync();
|
||||
|
||||
return new ReadingListPayload
|
||||
{
|
||||
Success = true,
|
||||
ReadingList = new ReadingListDto
|
||||
{
|
||||
Id = readingList.Id,
|
||||
Name = readingList.Name,
|
||||
Description = readingList.Description,
|
||||
Items = [],
|
||||
ItemCount = 0,
|
||||
CreatedTime = readingList.CreatedTime
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[Error<InvalidOperationException>]
|
||||
public async Task<ReadingListPayload> UpdateReadingList(
|
||||
UserNovelDataServiceDbContext dbContext,
|
||||
ClaimsPrincipal claimsPrincipal,
|
||||
UpdateReadingListInput input)
|
||||
{
|
||||
var oAuthProviderId = claimsPrincipal.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
||||
if (string.IsNullOrEmpty(oAuthProviderId))
|
||||
{
|
||||
throw new InvalidOperationException("Unable to determine current user identity");
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(input.Name))
|
||||
{
|
||||
throw new InvalidOperationException("Reading list name is required");
|
||||
}
|
||||
|
||||
var user = await dbContext.Users
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(u => u.OAuthProviderId == oAuthProviderId);
|
||||
|
||||
if (user == null)
|
||||
{
|
||||
return new ReadingListPayload { Success = false };
|
||||
}
|
||||
|
||||
var readingList = await dbContext.ReadingLists
|
||||
.Include(r => r.Items)
|
||||
.FirstOrDefaultAsync(r => r.Id == input.Id && r.UserId == user.Id);
|
||||
|
||||
if (readingList == null)
|
||||
{
|
||||
return new ReadingListPayload { Success = false };
|
||||
}
|
||||
|
||||
readingList.Name = input.Name.Trim();
|
||||
readingList.Description = input.Description?.Trim();
|
||||
|
||||
await dbContext.SaveChangesAsync();
|
||||
|
||||
return new ReadingListPayload
|
||||
{
|
||||
Success = true,
|
||||
ReadingList = new ReadingListDto
|
||||
{
|
||||
Id = readingList.Id,
|
||||
Name = readingList.Name,
|
||||
Description = readingList.Description,
|
||||
Items = readingList.Items.OrderBy(i => i.Order).Select(i => new ReadingListItemDto
|
||||
{
|
||||
NovelId = i.NovelId,
|
||||
Order = i.Order,
|
||||
AddedTime = i.CreatedTime
|
||||
}),
|
||||
ItemCount = readingList.Items.Count,
|
||||
CreatedTime = readingList.CreatedTime
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[Error<InvalidOperationException>]
|
||||
public async Task<DeleteReadingListPayload> DeleteReadingList(
|
||||
UserNovelDataServiceDbContext dbContext,
|
||||
ClaimsPrincipal claimsPrincipal,
|
||||
int id)
|
||||
{
|
||||
var oAuthProviderId = claimsPrincipal.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
||||
if (string.IsNullOrEmpty(oAuthProviderId))
|
||||
{
|
||||
throw new InvalidOperationException("Unable to determine current user identity");
|
||||
}
|
||||
|
||||
var user = await dbContext.Users
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(u => u.OAuthProviderId == oAuthProviderId);
|
||||
|
||||
if (user == null)
|
||||
{
|
||||
return new DeleteReadingListPayload { Success = false };
|
||||
}
|
||||
|
||||
var readingList = await dbContext.ReadingLists
|
||||
.FirstOrDefaultAsync(r => r.Id == id && r.UserId == user.Id);
|
||||
|
||||
if (readingList == null)
|
||||
{
|
||||
return new DeleteReadingListPayload { Success = false };
|
||||
}
|
||||
|
||||
dbContext.ReadingLists.Remove(readingList);
|
||||
await dbContext.SaveChangesAsync();
|
||||
|
||||
return new DeleteReadingListPayload { Success = true };
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[Error<InvalidOperationException>]
|
||||
public async Task<ReadingListPayload> AddToReadingList(
|
||||
UserNovelDataServiceDbContext dbContext,
|
||||
ClaimsPrincipal claimsPrincipal,
|
||||
AddToReadingListInput input)
|
||||
{
|
||||
var oAuthProviderId = claimsPrincipal.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
||||
if (string.IsNullOrEmpty(oAuthProviderId))
|
||||
{
|
||||
throw new InvalidOperationException("Unable to determine current user identity");
|
||||
}
|
||||
|
||||
var user = await dbContext.Users
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(u => u.OAuthProviderId == oAuthProviderId);
|
||||
|
||||
if (user == null)
|
||||
{
|
||||
return new ReadingListPayload { Success = false };
|
||||
}
|
||||
|
||||
var readingList = await dbContext.ReadingLists
|
||||
.Include(r => r.Items)
|
||||
.FirstOrDefaultAsync(r => r.Id == input.ReadingListId && r.UserId == user.Id);
|
||||
|
||||
if (readingList == null)
|
||||
{
|
||||
return new ReadingListPayload { Success = false };
|
||||
}
|
||||
|
||||
// Idempotent: if already in list, return success
|
||||
var existingItem = readingList.Items.FirstOrDefault(i => i.NovelId == input.NovelId);
|
||||
if (existingItem != null)
|
||||
{
|
||||
return new ReadingListPayload
|
||||
{
|
||||
Success = true,
|
||||
ReadingList = new ReadingListDto
|
||||
{
|
||||
Id = readingList.Id,
|
||||
Name = readingList.Name,
|
||||
Description = readingList.Description,
|
||||
Items = readingList.Items.OrderBy(i => i.Order).Select(i => new ReadingListItemDto
|
||||
{
|
||||
NovelId = i.NovelId,
|
||||
Order = i.Order,
|
||||
AddedTime = i.CreatedTime
|
||||
}),
|
||||
ItemCount = readingList.Items.Count,
|
||||
CreatedTime = readingList.CreatedTime
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Add at the end (highest order + 1)
|
||||
var maxOrder = readingList.Items.Any() ? readingList.Items.Max(i => i.Order) : -1;
|
||||
var newItem = new ReadingListItem
|
||||
{
|
||||
ReadingListId = readingList.Id,
|
||||
NovelId = input.NovelId,
|
||||
Order = maxOrder + 1
|
||||
};
|
||||
|
||||
dbContext.ReadingListItems.Add(newItem);
|
||||
await dbContext.SaveChangesAsync();
|
||||
|
||||
// Reload to get updated items
|
||||
readingList = await dbContext.ReadingLists
|
||||
.AsNoTracking()
|
||||
.Include(r => r.Items.OrderBy(i => i.Order))
|
||||
.FirstAsync(r => r.Id == input.ReadingListId);
|
||||
|
||||
return new ReadingListPayload
|
||||
{
|
||||
Success = true,
|
||||
ReadingList = new ReadingListDto
|
||||
{
|
||||
Id = readingList.Id,
|
||||
Name = readingList.Name,
|
||||
Description = readingList.Description,
|
||||
Items = readingList.Items.Select(i => new ReadingListItemDto
|
||||
{
|
||||
NovelId = i.NovelId,
|
||||
Order = i.Order,
|
||||
AddedTime = i.CreatedTime
|
||||
}),
|
||||
ItemCount = readingList.Items.Count,
|
||||
CreatedTime = readingList.CreatedTime
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[Error<InvalidOperationException>]
|
||||
public async Task<ReadingListPayload> RemoveFromReadingList(
|
||||
UserNovelDataServiceDbContext dbContext,
|
||||
ClaimsPrincipal claimsPrincipal,
|
||||
int listId,
|
||||
uint novelId)
|
||||
{
|
||||
var oAuthProviderId = claimsPrincipal.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
||||
if (string.IsNullOrEmpty(oAuthProviderId))
|
||||
{
|
||||
throw new InvalidOperationException("Unable to determine current user identity");
|
||||
}
|
||||
|
||||
var user = await dbContext.Users
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(u => u.OAuthProviderId == oAuthProviderId);
|
||||
|
||||
if (user == null)
|
||||
{
|
||||
return new ReadingListPayload { Success = false };
|
||||
}
|
||||
|
||||
var readingList = await dbContext.ReadingLists
|
||||
.Include(r => r.Items)
|
||||
.FirstOrDefaultAsync(r => r.Id == listId && r.UserId == user.Id);
|
||||
|
||||
if (readingList == null)
|
||||
{
|
||||
return new ReadingListPayload { Success = false };
|
||||
}
|
||||
|
||||
var item = readingList.Items.FirstOrDefault(i => i.NovelId == novelId);
|
||||
if (item != null)
|
||||
{
|
||||
dbContext.ReadingListItems.Remove(item);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
// Reload to get updated items
|
||||
readingList = await dbContext.ReadingLists
|
||||
.AsNoTracking()
|
||||
.Include(r => r.Items.OrderBy(i => i.Order))
|
||||
.FirstAsync(r => r.Id == listId);
|
||||
|
||||
return new ReadingListPayload
|
||||
{
|
||||
Success = true,
|
||||
ReadingList = new ReadingListDto
|
||||
{
|
||||
Id = readingList.Id,
|
||||
Name = readingList.Name,
|
||||
Description = readingList.Description,
|
||||
Items = readingList.Items.Select(i => new ReadingListItemDto
|
||||
{
|
||||
NovelId = i.NovelId,
|
||||
Order = i.Order,
|
||||
AddedTime = i.CreatedTime
|
||||
}),
|
||||
ItemCount = readingList.Items.Count,
|
||||
CreatedTime = readingList.CreatedTime
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[Error<InvalidOperationException>]
|
||||
public async Task<ReadingListPayload> ReorderReadingListItem(
|
||||
UserNovelDataServiceDbContext dbContext,
|
||||
ClaimsPrincipal claimsPrincipal,
|
||||
ReorderReadingListItemInput input)
|
||||
{
|
||||
var oAuthProviderId = claimsPrincipal.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
||||
if (string.IsNullOrEmpty(oAuthProviderId))
|
||||
{
|
||||
throw new InvalidOperationException("Unable to determine current user identity");
|
||||
}
|
||||
|
||||
var user = await dbContext.Users
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(u => u.OAuthProviderId == oAuthProviderId);
|
||||
|
||||
if (user == null)
|
||||
{
|
||||
return new ReadingListPayload { Success = false };
|
||||
}
|
||||
|
||||
var readingList = await dbContext.ReadingLists
|
||||
.Include(r => r.Items)
|
||||
.FirstOrDefaultAsync(r => r.Id == input.ReadingListId && r.UserId == user.Id);
|
||||
|
||||
if (readingList == null)
|
||||
{
|
||||
return new ReadingListPayload { Success = false };
|
||||
}
|
||||
|
||||
var item = readingList.Items.FirstOrDefault(i => i.NovelId == input.NovelId);
|
||||
if (item == null)
|
||||
{
|
||||
throw new InvalidOperationException("Novel not found in reading list");
|
||||
}
|
||||
|
||||
var oldOrder = item.Order;
|
||||
var newOrder = input.NewOrder;
|
||||
|
||||
// Shift other items
|
||||
if (newOrder < oldOrder)
|
||||
{
|
||||
// Moving up: shift items between newOrder and oldOrder down
|
||||
foreach (var i in readingList.Items.Where(x => x.Order >= newOrder && x.Order < oldOrder))
|
||||
{
|
||||
i.Order++;
|
||||
}
|
||||
}
|
||||
else if (newOrder > oldOrder)
|
||||
{
|
||||
// Moving down: shift items between oldOrder and newOrder up
|
||||
foreach (var i in readingList.Items.Where(x => x.Order > oldOrder && x.Order <= newOrder))
|
||||
{
|
||||
i.Order--;
|
||||
}
|
||||
}
|
||||
|
||||
item.Order = newOrder;
|
||||
await dbContext.SaveChangesAsync();
|
||||
|
||||
return new ReadingListPayload
|
||||
{
|
||||
Success = true,
|
||||
ReadingList = new ReadingListDto
|
||||
{
|
||||
Id = readingList.Id,
|
||||
Name = readingList.Name,
|
||||
Description = readingList.Description,
|
||||
Items = readingList.Items.OrderBy(i => i.Order).Select(i => new ReadingListItemDto
|
||||
{
|
||||
NovelId = i.NovelId,
|
||||
Order = i.Order,
|
||||
AddedTime = i.CreatedTime
|
||||
}),
|
||||
ItemCount = readingList.Items.Count,
|
||||
CreatedTime = readingList.CreatedTime
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Security.Claims;
|
||||
using FictionArchive.Service.UserNovelDataService.Models.Database;
|
||||
using FictionArchive.Service.UserNovelDataService.Models.DTOs;
|
||||
using FictionArchive.Service.UserNovelDataService.Services;
|
||||
using HotChocolate.Authorization;
|
||||
@@ -42,4 +43,94 @@ public class Query
|
||||
CreatedTime = b.CreatedTime
|
||||
});
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
public async Task<IEnumerable<ReadingListDto>> GetReadingLists(
|
||||
UserNovelDataServiceDbContext dbContext,
|
||||
ClaimsPrincipal claimsPrincipal)
|
||||
{
|
||||
var oAuthProviderId = claimsPrincipal.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
||||
if (string.IsNullOrEmpty(oAuthProviderId))
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
var user = await dbContext.Users
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(u => u.OAuthProviderId == oAuthProviderId);
|
||||
|
||||
if (user == null)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
var lists = await dbContext.ReadingLists
|
||||
.AsNoTracking()
|
||||
.Include(r => r.Items)
|
||||
.Where(r => r.UserId == user.Id)
|
||||
.OrderByDescending(r => r.LastUpdatedTime)
|
||||
.ToListAsync();
|
||||
|
||||
return lists.Select(r => new ReadingListDto
|
||||
{
|
||||
Id = r.Id,
|
||||
Name = r.Name,
|
||||
Description = r.Description,
|
||||
ItemCount = r.Items.Count,
|
||||
Items = r.Items.Select(i => new ReadingListItemDto
|
||||
{
|
||||
NovelId = i.NovelId,
|
||||
Order = i.Order,
|
||||
AddedTime = i.CreatedTime
|
||||
}),
|
||||
CreatedTime = r.CreatedTime
|
||||
});
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
public async Task<ReadingListDto?> GetReadingList(
|
||||
UserNovelDataServiceDbContext dbContext,
|
||||
ClaimsPrincipal claimsPrincipal,
|
||||
int id)
|
||||
{
|
||||
var oAuthProviderId = claimsPrincipal.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
||||
if (string.IsNullOrEmpty(oAuthProviderId))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var user = await dbContext.Users
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(u => u.OAuthProviderId == oAuthProviderId);
|
||||
|
||||
if (user == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var readingList = await dbContext.ReadingLists
|
||||
.AsNoTracking()
|
||||
.Include(r => r.Items.OrderBy(i => i.Order))
|
||||
.FirstOrDefaultAsync(r => r.Id == id && r.UserId == user.Id);
|
||||
|
||||
if (readingList == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new ReadingListDto
|
||||
{
|
||||
Id = readingList.Id,
|
||||
Name = readingList.Name,
|
||||
Description = readingList.Description,
|
||||
ItemCount = readingList.Items.Count,
|
||||
Items = readingList.Items.Select(i => new ReadingListItemDto
|
||||
{
|
||||
NovelId = i.NovelId,
|
||||
Order = i.Order,
|
||||
AddedTime = i.CreatedTime
|
||||
}),
|
||||
CreatedTime = readingList.CreatedTime
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
289
FictionArchive.Service.UserNovelDataService/Migrations/20260120014840_AddReadingLists.Designer.cs
generated
Normal file
289
FictionArchive.Service.UserNovelDataService/Migrations/20260120014840_AddReadingLists.Designer.cs
generated
Normal file
@@ -0,0 +1,289 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using FictionArchive.Service.UserNovelDataService.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.UserNovelDataService.Migrations
|
||||
{
|
||||
[DbContext(typeof(UserNovelDataServiceDbContext))]
|
||||
[Migration("20260120014840_AddReadingLists")]
|
||||
partial class AddReadingLists
|
||||
{
|
||||
/// <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.UserNovelDataService.Models.Database.Bookmark", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("integer");
|
||||
|
||||
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<long>("ChapterId")
|
||||
.HasColumnType("bigint");
|
||||
|
||||
b.Property<Instant>("CreatedTime")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.Property<string>("Description")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<Instant>("LastUpdatedTime")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.Property<long>("NovelId")
|
||||
.HasColumnType("bigint");
|
||||
|
||||
b.Property<Guid>("UserId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("UserId", "ChapterId")
|
||||
.IsUnique();
|
||||
|
||||
b.HasIndex("UserId", "NovelId");
|
||||
|
||||
b.ToTable("Bookmarks");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("FictionArchive.Service.UserNovelDataService.Models.Database.Chapter", 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<long>("VolumeId")
|
||||
.HasColumnType("bigint");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("VolumeId");
|
||||
|
||||
b.ToTable("Chapters");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("FictionArchive.Service.UserNovelDataService.Models.Database.Novel", 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.HasKey("Id");
|
||||
|
||||
b.ToTable("Novels");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("FictionArchive.Service.UserNovelDataService.Models.Database.ReadingList", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("integer");
|
||||
|
||||
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<Instant>("CreatedTime")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.Property<string>("Description")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<Instant>("LastUpdatedTime")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<Guid>("UserId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("UserId");
|
||||
|
||||
b.ToTable("ReadingLists");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("FictionArchive.Service.UserNovelDataService.Models.Database.ReadingListItem", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("integer");
|
||||
|
||||
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<Instant>("CreatedTime")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.Property<Instant>("LastUpdatedTime")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.Property<long>("NovelId")
|
||||
.HasColumnType("bigint");
|
||||
|
||||
b.Property<int>("Order")
|
||||
.HasColumnType("integer");
|
||||
|
||||
b.Property<int>("ReadingListId")
|
||||
.HasColumnType("integer");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("ReadingListId", "NovelId")
|
||||
.IsUnique();
|
||||
|
||||
b.HasIndex("ReadingListId", "Order");
|
||||
|
||||
b.ToTable("ReadingListItems");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("FictionArchive.Service.UserNovelDataService.Models.Database.User", 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.Property<string>("OAuthProviderId")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Users");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("FictionArchive.Service.UserNovelDataService.Models.Database.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<long>("NovelId")
|
||||
.HasColumnType("bigint");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("NovelId");
|
||||
|
||||
b.ToTable("Volumes");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("FictionArchive.Service.UserNovelDataService.Models.Database.Bookmark", b =>
|
||||
{
|
||||
b.HasOne("FictionArchive.Service.UserNovelDataService.Models.Database.User", "User")
|
||||
.WithMany()
|
||||
.HasForeignKey("UserId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("User");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("FictionArchive.Service.UserNovelDataService.Models.Database.Chapter", b =>
|
||||
{
|
||||
b.HasOne("FictionArchive.Service.UserNovelDataService.Models.Database.Volume", "Volume")
|
||||
.WithMany("Chapters")
|
||||
.HasForeignKey("VolumeId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Volume");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("FictionArchive.Service.UserNovelDataService.Models.Database.ReadingList", b =>
|
||||
{
|
||||
b.HasOne("FictionArchive.Service.UserNovelDataService.Models.Database.User", "User")
|
||||
.WithMany()
|
||||
.HasForeignKey("UserId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("User");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("FictionArchive.Service.UserNovelDataService.Models.Database.ReadingListItem", b =>
|
||||
{
|
||||
b.HasOne("FictionArchive.Service.UserNovelDataService.Models.Database.ReadingList", "ReadingList")
|
||||
.WithMany("Items")
|
||||
.HasForeignKey("ReadingListId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("ReadingList");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("FictionArchive.Service.UserNovelDataService.Models.Database.Volume", b =>
|
||||
{
|
||||
b.HasOne("FictionArchive.Service.UserNovelDataService.Models.Database.Novel", "Novel")
|
||||
.WithMany("Volumes")
|
||||
.HasForeignKey("NovelId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Novel");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("FictionArchive.Service.UserNovelDataService.Models.Database.Novel", b =>
|
||||
{
|
||||
b.Navigation("Volumes");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("FictionArchive.Service.UserNovelDataService.Models.Database.ReadingList", b =>
|
||||
{
|
||||
b.Navigation("Items");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("FictionArchive.Service.UserNovelDataService.Models.Database.Volume", b =>
|
||||
{
|
||||
b.Navigation("Chapters");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using NodaTime;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace FictionArchive.Service.UserNovelDataService.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class AddReadingLists : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "ReadingLists",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<int>(type: "integer", nullable: false)
|
||||
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
|
||||
UserId = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
Name = table.Column<string>(type: "text", nullable: false),
|
||||
Description = table.Column<string>(type: "text", nullable: true),
|
||||
CreatedTime = table.Column<Instant>(type: "timestamp with time zone", nullable: false),
|
||||
LastUpdatedTime = table.Column<Instant>(type: "timestamp with time zone", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_ReadingLists", x => x.Id);
|
||||
table.ForeignKey(
|
||||
name: "FK_ReadingLists_Users_UserId",
|
||||
column: x => x.UserId,
|
||||
principalTable: "Users",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "ReadingListItems",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<int>(type: "integer", nullable: false)
|
||||
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
|
||||
ReadingListId = table.Column<int>(type: "integer", nullable: false),
|
||||
NovelId = table.Column<long>(type: "bigint", nullable: false),
|
||||
Order = table.Column<int>(type: "integer", nullable: false),
|
||||
CreatedTime = table.Column<Instant>(type: "timestamp with time zone", nullable: false),
|
||||
LastUpdatedTime = table.Column<Instant>(type: "timestamp with time zone", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_ReadingListItems", x => x.Id);
|
||||
table.ForeignKey(
|
||||
name: "FK_ReadingListItems_ReadingLists_ReadingListId",
|
||||
column: x => x.ReadingListId,
|
||||
principalTable: "ReadingLists",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_ReadingListItems_ReadingListId_NovelId",
|
||||
table: "ReadingListItems",
|
||||
columns: new[] { "ReadingListId", "NovelId" },
|
||||
unique: true);
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_ReadingListItems_ReadingListId_Order",
|
||||
table: "ReadingListItems",
|
||||
columns: new[] { "ReadingListId", "Order" });
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_ReadingLists_UserId",
|
||||
table: "ReadingLists",
|
||||
column: "UserId");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "ReadingListItems");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "ReadingLists");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -102,6 +102,70 @@ namespace FictionArchive.Service.UserNovelDataService.Migrations
|
||||
b.ToTable("Novels");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("FictionArchive.Service.UserNovelDataService.Models.Database.ReadingList", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("integer");
|
||||
|
||||
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<Instant>("CreatedTime")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.Property<string>("Description")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<Instant>("LastUpdatedTime")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<Guid>("UserId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("UserId");
|
||||
|
||||
b.ToTable("ReadingLists");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("FictionArchive.Service.UserNovelDataService.Models.Database.ReadingListItem", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("integer");
|
||||
|
||||
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<Instant>("CreatedTime")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.Property<Instant>("LastUpdatedTime")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.Property<long>("NovelId")
|
||||
.HasColumnType("bigint");
|
||||
|
||||
b.Property<int>("Order")
|
||||
.HasColumnType("integer");
|
||||
|
||||
b.Property<int>("ReadingListId")
|
||||
.HasColumnType("integer");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("ReadingListId", "NovelId")
|
||||
.IsUnique();
|
||||
|
||||
b.HasIndex("ReadingListId", "Order");
|
||||
|
||||
b.ToTable("ReadingListItems");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("FictionArchive.Service.UserNovelDataService.Models.Database.User", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
@@ -169,6 +233,28 @@ namespace FictionArchive.Service.UserNovelDataService.Migrations
|
||||
b.Navigation("Volume");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("FictionArchive.Service.UserNovelDataService.Models.Database.ReadingList", b =>
|
||||
{
|
||||
b.HasOne("FictionArchive.Service.UserNovelDataService.Models.Database.User", "User")
|
||||
.WithMany()
|
||||
.HasForeignKey("UserId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("User");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("FictionArchive.Service.UserNovelDataService.Models.Database.ReadingListItem", b =>
|
||||
{
|
||||
b.HasOne("FictionArchive.Service.UserNovelDataService.Models.Database.ReadingList", "ReadingList")
|
||||
.WithMany("Items")
|
||||
.HasForeignKey("ReadingListId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("ReadingList");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("FictionArchive.Service.UserNovelDataService.Models.Database.Volume", b =>
|
||||
{
|
||||
b.HasOne("FictionArchive.Service.UserNovelDataService.Models.Database.Novel", "Novel")
|
||||
@@ -185,6 +271,11 @@ namespace FictionArchive.Service.UserNovelDataService.Migrations
|
||||
b.Navigation("Volumes");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("FictionArchive.Service.UserNovelDataService.Models.Database.ReadingList", b =>
|
||||
{
|
||||
b.Navigation("Items");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("FictionArchive.Service.UserNovelDataService.Models.Database.Volume", b =>
|
||||
{
|
||||
b.Navigation("Chapters");
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
namespace FictionArchive.Service.UserNovelDataService.Models.DTOs;
|
||||
|
||||
public record AddToReadingListInput(int ReadingListId, uint NovelId);
|
||||
@@ -0,0 +1,3 @@
|
||||
namespace FictionArchive.Service.UserNovelDataService.Models.DTOs;
|
||||
|
||||
public record CreateReadingListInput(string Name, string? Description);
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user