Compare commits
52 Commits
feature/FA
...
epic/FA-mi
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
579e05b853 | ||
| e7435435c1 | |||
|
|
dd7aa4b044 | ||
|
|
1b9da7441c | ||
| 055ef33666 | |||
|
|
48ee43c4f6 | ||
| 98ae4ea4f2 | |||
|
|
15e1a84f55 | ||
|
|
70d4ba201a | ||
|
|
b69bcd6bf4 | ||
|
|
c97654631b | ||
|
|
1ecfd9cc99 | ||
|
|
19ae4a8089 | ||
|
|
f8a45ad891 | ||
|
|
f67c5c610c | ||
| b5d4694f12 | |||
|
|
6d47153a42 | ||
| dbbc2fd8dc | |||
|
|
176c94297b | ||
|
|
8b3faa8f6c | ||
|
|
d87bd81190 | ||
|
|
bee805c441 | ||
|
|
5013da69c2 | ||
| d8e3ec7ec9 | |||
|
|
3612c89b99 | ||
|
|
ebb2e6e7fc | ||
|
|
01d3b94050 | ||
|
|
c0290cc5af | ||
| 1d950b7721 | |||
|
|
7738bcf438 | ||
| 61e0cb69d8 | |||
|
|
02525d611a | ||
| c21fe0fbd5 | |||
|
|
bbc0b5ec7d | ||
| 5527c15ae7 | |||
|
|
1e374e6eeb | ||
| c710f14257 | |||
|
|
6c10077505 | ||
| fecb3e6f43 | |||
|
|
f0ea71e00e | ||
| 45afb57df5 | |||
|
|
6fd76f6787 | ||
| baad092f07 | |||
|
|
4fb34bdef7 | ||
| 89a2cf6db1 | |||
|
|
f830773af5 | ||
| 7185b95c65 | |||
|
|
8b44cf2f0c | ||
| ac48889f4c | |||
|
|
5c52d29da9 | ||
| 16004ad938 | |||
| 89dff0980b |
@@ -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
|
||||
|
||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -140,3 +140,6 @@ appsettings.Local.json
|
||||
schema.graphql
|
||||
*.fsp
|
||||
gateway.fgp
|
||||
|
||||
# Git worktrees
|
||||
.worktrees/
|
||||
9977
Documentation/sample_novel_page.html
Normal file
9977
Documentation/sample_novel_page.html
Normal file
File diff suppressed because it is too large
Load Diff
@@ -24,6 +24,8 @@ RUN dotnet build "./FictionArchive.API.csproj" -c $BUILD_CONFIGURATION -o /app/b
|
||||
FROM build AS publish
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
RUN dotnet publish "./FictionArchive.API.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false /p:SkipFusionBuild=true
|
||||
# Copy pre-composed gateway.fgp from CI build
|
||||
COPY FictionArchive.API/gateway.fgp /app/publish/
|
||||
|
||||
FROM base AS final
|
||||
WORKDIR /app
|
||||
|
||||
@@ -25,8 +25,6 @@ public class Program
|
||||
.ConfigureFromFile("gateway.fgp")
|
||||
.CoreBuilder.ApplySaneDefaults();
|
||||
|
||||
builder.Services.AddOidcAuthentication(builder.Configuration);
|
||||
|
||||
#endregion
|
||||
|
||||
var allowedOrigin = builder.Configuration["Cors:AllowedOrigin"] ?? "http://localhost:4321";
|
||||
|
||||
@@ -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,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,13 +0,0 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"RabbitMQ": {
|
||||
"ConnectionString": "amqp://localhost",
|
||||
"ClientIdentifier": "AuthenticationService"
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
using Amazon.S3;
|
||||
using Amazon.S3.Model;
|
||||
using FictionArchive.Common.Enums;
|
||||
using FictionArchive.Service.FileService.Contracts;
|
||||
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(
|
||||
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(
|
||||
RequestId: message.RequestId,
|
||||
Status: RequestStatus.Success,
|
||||
FileAccessUrl: fileAccessUrl,
|
||||
ErrorMessage: null));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using FictionArchive.Common.Enums;
|
||||
using FictionArchive.Service.Shared.Contracts.Events;
|
||||
|
||||
namespace FictionArchive.Service.FileService.Contracts;
|
||||
|
||||
public record FileUploadRequestStatusUpdate(
|
||||
Guid RequestId,
|
||||
RequestStatus Status,
|
||||
string? FileAccessUrl,
|
||||
string? ErrorMessage) : IFileUploadRequestStatusUpdate;
|
||||
@@ -36,7 +36,15 @@ namespace FictionArchive.Service.FileService.Controllers
|
||||
Key = decodedPath
|
||||
});
|
||||
|
||||
return new FileStreamResult(s3Response.ResponseStream, s3Response.Headers.ContentType);
|
||||
Response.Headers.CacheControl = "public, max-age=604800"; // 7 days
|
||||
Response.Headers.LastModified = s3Response.LastModified?.ToString("R");
|
||||
|
||||
if (!string.IsNullOrEmpty(s3Response.ETag))
|
||||
{
|
||||
Response.Headers.ETag = s3Response.ETag;
|
||||
}
|
||||
|
||||
return File(s3Response.ResponseStream, s3Response.Headers.ContentType);
|
||||
}
|
||||
catch (AmazonS3Exception e)
|
||||
{
|
||||
|
||||
@@ -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.Configuration.GetSection("RabbitMQ").Bind(opt);
|
||||
})
|
||||
.Subscribe<FileUploadRequestCreatedEvent, FileUploadRequestCreatedEventHandler>();
|
||||
builder.Services.AddFictionArchiveMassTransit(
|
||||
builder.Configuration,
|
||||
x =>
|
||||
{
|
||||
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": {
|
||||
|
||||
@@ -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,9 +7,10 @@ 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;
|
||||
@@ -42,6 +42,13 @@ public class NovelUpdateServiceTests
|
||||
Images = new List<Image>()
|
||||
};
|
||||
|
||||
var volume = new Volume
|
||||
{
|
||||
Order = 1,
|
||||
Name = LocalizationKey.CreateFromText("Main Story", Language.En),
|
||||
Chapters = new List<Chapter> { chapter }
|
||||
};
|
||||
|
||||
var novel = new Novel
|
||||
{
|
||||
Url = "http://demo/novel",
|
||||
@@ -52,20 +59,20 @@ public class NovelUpdateServiceTests
|
||||
Source = source,
|
||||
Name = LocalizationKey.CreateFromText("Demo Novel", Language.En),
|
||||
Description = LocalizationKey.CreateFromText("Description", Language.En),
|
||||
Chapters = new List<Chapter> { chapter },
|
||||
Volumes = new List<Volume> { volume },
|
||||
Tags = new List<NovelTag>()
|
||||
};
|
||||
|
||||
dbContext.Novels.Add(novel);
|
||||
dbContext.SaveChanges();
|
||||
|
||||
return new NovelCreateResult(novel, chapter);
|
||||
return new NovelCreateResult(novel, volume, chapter);
|
||||
}
|
||||
|
||||
private static NovelUpdateService CreateService(
|
||||
NovelServiceDbContext dbContext,
|
||||
ISourceAdapter adapter,
|
||||
IEventBus eventBus,
|
||||
IPublishEndpoint publishEndpoint,
|
||||
string pendingImageUrl = "https://pending/placeholder.jpg")
|
||||
{
|
||||
var options = Options.Create(new NovelUpdateServiceConfiguration
|
||||
@@ -73,7 +80,7 @@ public class NovelUpdateServiceTests
|
||||
PendingImageUrl = pendingImageUrl
|
||||
});
|
||||
|
||||
return new NovelUpdateService(dbContext, NullLogger<NovelUpdateService>.Instance, new[] { adapter }, eventBus, options);
|
||||
return new NovelUpdateService(dbContext, NullLogger<NovelUpdateService>.Instance, new[] { adapter }, publishEndpoint, options);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -81,7 +88,7 @@ public class NovelUpdateServiceTests
|
||||
{
|
||||
using var dbContext = CreateDbContext();
|
||||
var source = new Source { Name = "Demo", Key = "demo", Url = "http://demo" };
|
||||
var (novel, chapter) = CreateNovelWithSingleChapter(dbContext, source);
|
||||
var (novel, volume, chapter) = CreateNovelWithSingleChapter(dbContext, source);
|
||||
|
||||
var rawHtml = "<p>Hello</p><img src=\"http://img/x1.jpg\" alt=\"first\" /><img src=\"http://img/x2.jpg\" alt=\"second\" />";
|
||||
var image1 = new ImageData { Url = "http://img/x1.jpg", Data = new byte[] { 1, 2, 3 } };
|
||||
@@ -95,15 +102,15 @@ 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, chapter.Order);
|
||||
var updatedChapter = await service.PullChapterContents(novel.Id, volume.Id, chapter.Order);
|
||||
|
||||
updatedChapter.Images.Should().HaveCount(2);
|
||||
updatedChapter.Images.Select(i => i.OriginalPath).Should().BeEquivalentTo(new[] { image1.Url, image2.Url });
|
||||
@@ -131,7 +138,7 @@ public class NovelUpdateServiceTests
|
||||
{
|
||||
using var dbContext = CreateDbContext();
|
||||
var source = new Source { Name = "Demo", Key = "demo", Url = "http://demo" };
|
||||
var (novel, chapter) = CreateNovelWithSingleChapter(dbContext, source);
|
||||
var (novel, volume, chapter) = CreateNovelWithSingleChapter(dbContext, source);
|
||||
|
||||
var rawHtml = "<p>Hi</p><img src=\"http://img/x1.jpg\">";
|
||||
var image = new ImageData { Url = "http://img/x1.jpg", Data = new byte[] { 7, 8, 9 } };
|
||||
@@ -144,13 +151,11 @@ 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, chapter.Order);
|
||||
var updatedChapter = await service.PullChapterContents(novel.Id, volume.Id, chapter.Order);
|
||||
|
||||
var storedHtml = updatedChapter.Body.Texts.Single().Text;
|
||||
var doc = new HtmlDocument();
|
||||
@@ -161,7 +166,7 @@ public class NovelUpdateServiceTests
|
||||
imgNode.GetAttributeValue("src", string.Empty).Should().Be("https://pending/placeholder.jpg");
|
||||
}
|
||||
|
||||
private record NovelCreateResult(Novel Novel, Chapter Chapter);
|
||||
private record NovelCreateResult(Novel Novel, Volume Volume, Chapter Chapter);
|
||||
|
||||
#region UpdateImage Tests
|
||||
|
||||
@@ -179,8 +184,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";
|
||||
|
||||
@@ -199,7 +204,7 @@ public class NovelUpdateServiceTests
|
||||
// Arrange
|
||||
using var dbContext = CreateDbContext();
|
||||
var source = new Source { Name = "Demo", Key = "demo", Url = "http://demo" };
|
||||
var (novel, chapter) = CreateNovelWithSingleChapter(dbContext, source);
|
||||
var (novel, _, chapter) = CreateNovelWithSingleChapter(dbContext, source);
|
||||
|
||||
var image = new Image
|
||||
{
|
||||
@@ -221,8 +226,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";
|
||||
|
||||
@@ -252,7 +257,7 @@ public class NovelUpdateServiceTests
|
||||
// Arrange
|
||||
using var dbContext = CreateDbContext();
|
||||
var source = new Source { Name = "Demo", Key = "demo", Url = "http://demo" };
|
||||
var (novel, chapter) = CreateNovelWithSingleChapter(dbContext, source);
|
||||
var (_, _, chapter) = CreateNovelWithSingleChapter(dbContext, source);
|
||||
|
||||
var image1 = new Image { OriginalPath = "http://original/img1.jpg", Chapter = chapter };
|
||||
var image2 = new Image { OriginalPath = "http://original/img2.jpg", Chapter = chapter };
|
||||
@@ -270,8 +275,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,26 @@
|
||||
using FictionArchive.Service.NovelService.Services;
|
||||
using FictionArchive.Service.Shared.Contracts.Events;
|
||||
using MassTransit;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace FictionArchive.Service.NovelService.Consumers;
|
||||
|
||||
public class ChapterPullRequestedConsumer : IConsumer<IChapterPullRequested>
|
||||
{
|
||||
private readonly ILogger<ChapterPullRequestedConsumer> _logger;
|
||||
private readonly NovelUpdateService _novelUpdateService;
|
||||
|
||||
public ChapterPullRequestedConsumer(
|
||||
ILogger<ChapterPullRequestedConsumer> logger,
|
||||
NovelUpdateService novelUpdateService)
|
||||
{
|
||||
_logger = logger;
|
||||
_novelUpdateService = novelUpdateService;
|
||||
}
|
||||
|
||||
public async Task Consume(ConsumeContext<IChapterPullRequested> context)
|
||||
{
|
||||
var message = context.Message;
|
||||
await _novelUpdateService.PullChapterContents(message.NovelId, message.VolumeId, message.ChapterOrder);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using FictionArchive.Common.Enums;
|
||||
using FictionArchive.Service.NovelService.Services;
|
||||
using FictionArchive.Service.Shared.Contracts.Events;
|
||||
using MassTransit;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace FictionArchive.Service.NovelService.Consumers;
|
||||
|
||||
public class FileUploadRequestStatusUpdateConsumer : IConsumer<IFileUploadRequestStatusUpdate>
|
||||
{
|
||||
private readonly ILogger<FileUploadRequestStatusUpdateConsumer> _logger;
|
||||
private readonly NovelServiceDbContext _dbContext;
|
||||
private readonly NovelUpdateService _novelUpdateService;
|
||||
|
||||
public FileUploadRequestStatusUpdateConsumer(
|
||||
ILogger<FileUploadRequestStatusUpdateConsumer> logger,
|
||||
NovelServiceDbContext dbContext,
|
||||
NovelUpdateService novelUpdateService)
|
||||
{
|
||||
_logger = logger;
|
||||
_dbContext = dbContext;
|
||||
_novelUpdateService = novelUpdateService;
|
||||
}
|
||||
|
||||
public async Task Consume(ConsumeContext<IFileUploadRequestStatusUpdate> context)
|
||||
{
|
||||
var message = context.Message;
|
||||
|
||||
var image = await _dbContext.Images.FindAsync(message.RequestId);
|
||||
if (image == null)
|
||||
{
|
||||
// Not a request we care about.
|
||||
return;
|
||||
}
|
||||
|
||||
if (message.Status == RequestStatus.Failed)
|
||||
{
|
||||
_logger.LogError("Image upload failed for image with id {imageId}", image.Id);
|
||||
return;
|
||||
}
|
||||
else if (message.Status == RequestStatus.Success)
|
||||
{
|
||||
_logger.LogInformation("Image upload succeeded for image with id {imageId}", image.Id);
|
||||
await _novelUpdateService.UpdateImage(image.Id, message.FileAccessUrl);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using FictionArchive.Service.NovelService.Services;
|
||||
using FictionArchive.Service.Shared.Contracts.Events;
|
||||
using MassTransit;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace FictionArchive.Service.NovelService.Consumers;
|
||||
|
||||
public class NovelUpdateRequestedConsumer : IConsumer<INovelUpdateRequested>
|
||||
{
|
||||
private readonly ILogger<NovelUpdateRequestedConsumer> _logger;
|
||||
private readonly NovelUpdateService _novelUpdateService;
|
||||
|
||||
public NovelUpdateRequestedConsumer(
|
||||
ILogger<NovelUpdateRequestedConsumer> logger,
|
||||
NovelUpdateService novelUpdateService)
|
||||
{
|
||||
_logger = logger;
|
||||
_novelUpdateService = novelUpdateService;
|
||||
}
|
||||
|
||||
public async Task Consume(ConsumeContext<INovelUpdateRequested> context)
|
||||
{
|
||||
var message = context.Message;
|
||||
await _novelUpdateService.ImportNovel(message.NovelUrl);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using FictionArchive.Service.NovelService.Models.Localization;
|
||||
using FictionArchive.Service.NovelService.Services;
|
||||
using FictionArchive.Service.Shared.Contracts.Events;
|
||||
using MassTransit;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace FictionArchive.Service.NovelService.Consumers;
|
||||
|
||||
public class TranslationRequestCompletedConsumer : IConsumer<ITranslationRequestCompleted>
|
||||
{
|
||||
private readonly ILogger<TranslationRequestCompletedConsumer> _logger;
|
||||
private readonly NovelServiceDbContext _dbContext;
|
||||
|
||||
public TranslationRequestCompletedConsumer(
|
||||
ILogger<TranslationRequestCompletedConsumer> logger,
|
||||
NovelServiceDbContext dbContext)
|
||||
{
|
||||
_logger = logger;
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
public async Task Consume(ConsumeContext<ITranslationRequestCompleted> context)
|
||||
{
|
||||
var message = context.Message;
|
||||
|
||||
var localizationRequest = await _dbContext.LocalizationRequests
|
||||
.Include(r => r.KeyRequestedForTranslation)
|
||||
.ThenInclude(lk => lk.Texts)
|
||||
.FirstOrDefaultAsync(lk => lk.Id == message.TranslationRequestId);
|
||||
|
||||
if (localizationRequest == null)
|
||||
{
|
||||
// Not one of our requests, discard it
|
||||
return;
|
||||
}
|
||||
|
||||
localizationRequest.KeyRequestedForTranslation.Texts.Add(new LocalizationText
|
||||
{
|
||||
Language = localizationRequest.TranslateTo,
|
||||
Text = message.TranslatedText,
|
||||
TranslationEngine = localizationRequest.Engine
|
||||
});
|
||||
|
||||
_dbContext.LocalizationRequests.Remove(localizationRequest);
|
||||
await _dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
@@ -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,8 @@
|
||||
using FictionArchive.Service.Shared.Contracts.Events;
|
||||
|
||||
namespace FictionArchive.Service.NovelService.Contracts;
|
||||
|
||||
public record ChapterPullRequested(
|
||||
uint NovelId,
|
||||
uint VolumeId,
|
||||
uint ChapterOrder) : IChapterPullRequested;
|
||||
@@ -0,0 +1,8 @@
|
||||
using FictionArchive.Service.Shared.Contracts.Events;
|
||||
|
||||
namespace FictionArchive.Service.NovelService.Contracts;
|
||||
|
||||
public record FileUploadRequestCreated(
|
||||
Guid RequestId,
|
||||
string FilePath,
|
||||
byte[] FileData) : IFileUploadRequestCreated;
|
||||
@@ -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,6 @@
|
||||
using FictionArchive.Service.Shared.Contracts.Events;
|
||||
|
||||
namespace FictionArchive.Service.NovelService.Contracts;
|
||||
|
||||
public record NovelUpdateRequested(
|
||||
string NovelUrl) : INovelUpdateRequested;
|
||||
@@ -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;
|
||||
@@ -1,12 +1,12 @@
|
||||
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 HotChocolate.Authorization;
|
||||
using HotChocolate.Types;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace FictionArchive.Service.NovelService.GraphQL;
|
||||
@@ -14,16 +14,26 @@ namespace FictionArchive.Service.NovelService.GraphQL;
|
||||
public class Mutation
|
||||
{
|
||||
[Authorize]
|
||||
public async Task<NovelUpdateRequestedEvent> ImportNovel(string novelUrl, NovelUpdateService service)
|
||||
public async Task<NovelUpdateRequested> ImportNovel(string novelUrl, NovelUpdateService service)
|
||||
{
|
||||
return await service.QueueNovelImport(novelUrl);
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
public async Task<ChapterPullRequestedEvent> FetchChapterContents(uint novelId,
|
||||
uint chapterNumber,
|
||||
public async Task<ChapterPullRequested> FetchChapterContents(
|
||||
uint novelId,
|
||||
uint volumeId,
|
||||
uint chapterOrder,
|
||||
NovelUpdateService service)
|
||||
{
|
||||
return await service.QueueChapterPull(novelId, chapterNumber);
|
||||
return await service.QueueChapterPull(novelId, volumeId, chapterOrder);
|
||||
}
|
||||
|
||||
[Error<KeyNotFoundException>]
|
||||
[Authorize]
|
||||
public async Task<bool> DeleteNovel(uint novelId, NovelUpdateService service)
|
||||
{
|
||||
await service.DeleteNovel(novelId);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -77,32 +77,45 @@ public class Query
|
||||
}
|
||||
: null,
|
||||
|
||||
Chapters = novel.Chapters.Select(chapter => new ChapterDto
|
||||
Volumes = novel.Volumes.OrderBy(v => v.Order).Select(volume => new VolumeDto
|
||||
{
|
||||
Id = chapter.Id,
|
||||
CreatedTime = chapter.CreatedTime,
|
||||
LastUpdatedTime = chapter.LastUpdatedTime,
|
||||
Revision = chapter.Revision,
|
||||
Order = chapter.Order,
|
||||
Url = chapter.Url,
|
||||
Name = chapter.Name.Texts
|
||||
Id = volume.Id,
|
||||
CreatedTime = volume.CreatedTime,
|
||||
LastUpdatedTime = volume.LastUpdatedTime,
|
||||
Order = volume.Order,
|
||||
Name = volume.Name.Texts
|
||||
.Where(t => t.Language == preferredLanguage)
|
||||
.Select(t => t.Text)
|
||||
.FirstOrDefault()
|
||||
?? chapter.Name.Texts.Select(t => t.Text).FirstOrDefault()
|
||||
?? volume.Name.Texts.Select(t => t.Text).FirstOrDefault()
|
||||
?? "",
|
||||
Body = chapter.Body.Texts
|
||||
.Where(t => t.Language == preferredLanguage)
|
||||
.Select(t => t.Text)
|
||||
.FirstOrDefault()
|
||||
?? chapter.Body.Texts.Select(t => t.Text).FirstOrDefault()
|
||||
?? "",
|
||||
Images = chapter.Images.Select(image => new ImageDto
|
||||
Chapters = volume.Chapters.OrderBy(c => c.Order).Select(chapter => new ChapterDto
|
||||
{
|
||||
Id = image.Id,
|
||||
CreatedTime = image.CreatedTime,
|
||||
LastUpdatedTime = image.LastUpdatedTime,
|
||||
NewPath = image.NewPath
|
||||
Id = chapter.Id,
|
||||
CreatedTime = chapter.CreatedTime,
|
||||
LastUpdatedTime = chapter.LastUpdatedTime,
|
||||
Revision = chapter.Revision,
|
||||
Order = chapter.Order,
|
||||
Url = chapter.Url,
|
||||
Name = chapter.Name.Texts
|
||||
.Where(t => t.Language == preferredLanguage)
|
||||
.Select(t => t.Text)
|
||||
.FirstOrDefault()
|
||||
?? chapter.Name.Texts.Select(t => t.Text).FirstOrDefault()
|
||||
?? "",
|
||||
Body = chapter.Body.Texts
|
||||
.Where(t => t.Language == preferredLanguage)
|
||||
.Select(t => t.Text)
|
||||
.FirstOrDefault()
|
||||
?? chapter.Body.Texts.Select(t => t.Text).FirstOrDefault()
|
||||
?? "",
|
||||
Images = chapter.Images.Select(image => new ImageDto
|
||||
{
|
||||
Id = image.Id,
|
||||
CreatedTime = image.CreatedTime,
|
||||
LastUpdatedTime = image.LastUpdatedTime,
|
||||
NewPath = image.NewPath
|
||||
}).ToList()
|
||||
}).ToList()
|
||||
}).ToList(),
|
||||
|
||||
@@ -140,11 +153,12 @@ public class Query
|
||||
public IQueryable<ChapterReaderDto> GetChapter(
|
||||
NovelServiceDbContext dbContext,
|
||||
uint novelId,
|
||||
uint volumeOrder,
|
||||
uint chapterOrder,
|
||||
Language preferredLanguage = Language.En)
|
||||
{
|
||||
return dbContext.Chapters
|
||||
.Where(c => c.Novel.Id == novelId && c.Order == chapterOrder)
|
||||
.Where(c => c.Volume.Novel.Id == novelId && c.Volume.Order == volumeOrder && c.Order == chapterOrder)
|
||||
.Select(chapter => new ChapterReaderDto
|
||||
{
|
||||
Id = chapter.Id,
|
||||
@@ -176,24 +190,74 @@ public class Query
|
||||
NewPath = image.NewPath
|
||||
}).ToList(),
|
||||
|
||||
NovelId = chapter.Novel.Id,
|
||||
NovelName = chapter.Novel.Name.Texts
|
||||
NovelId = chapter.Volume.Novel.Id,
|
||||
NovelName = chapter.Volume.Novel.Name.Texts
|
||||
.Where(t => t.Language == preferredLanguage)
|
||||
.Select(t => t.Text)
|
||||
.FirstOrDefault()
|
||||
?? chapter.Novel.Name.Texts.Select(t => t.Text).FirstOrDefault()
|
||||
?? chapter.Volume.Novel.Name.Texts.Select(t => t.Text).FirstOrDefault()
|
||||
?? "",
|
||||
TotalChapters = chapter.Novel.Chapters.Count,
|
||||
PrevChapterOrder = chapter.Novel.Chapters
|
||||
|
||||
// Volume context
|
||||
VolumeId = chapter.Volume.Id,
|
||||
VolumeName = chapter.Volume.Name.Texts
|
||||
.Where(t => t.Language == preferredLanguage)
|
||||
.Select(t => t.Text)
|
||||
.FirstOrDefault()
|
||||
?? chapter.Volume.Name.Texts.Select(t => t.Text).FirstOrDefault()
|
||||
?? "",
|
||||
VolumeOrder = chapter.Volume.Order,
|
||||
TotalChaptersInVolume = chapter.Volume.Chapters.Count,
|
||||
|
||||
// Previous chapter: first try same volume, then last chapter of previous volume
|
||||
PrevChapterVolumeOrder = chapter.Volume.Chapters
|
||||
.Where(c => c.Order < chapterOrder)
|
||||
.OrderByDescending(c => c.Order)
|
||||
.Select(c => (int?)chapter.Volume.Order)
|
||||
.FirstOrDefault()
|
||||
?? chapter.Volume.Novel.Volumes
|
||||
.Where(v => v.Order < chapter.Volume.Order)
|
||||
.OrderByDescending(v => v.Order)
|
||||
.SelectMany(v => v.Chapters.OrderByDescending(c => c.Order).Take(1))
|
||||
.Select(c => (int?)c.Volume.Order)
|
||||
.FirstOrDefault(),
|
||||
|
||||
PrevChapterOrder = chapter.Volume.Chapters
|
||||
.Where(c => c.Order < chapterOrder)
|
||||
.OrderByDescending(c => c.Order)
|
||||
.Select(c => (uint?)c.Order)
|
||||
.FirstOrDefault(),
|
||||
NextChapterOrder = chapter.Novel.Chapters
|
||||
.FirstOrDefault()
|
||||
?? chapter.Volume.Novel.Volumes
|
||||
.Where(v => v.Order < chapter.Volume.Order)
|
||||
.OrderByDescending(v => v.Order)
|
||||
.SelectMany(v => v.Chapters.OrderByDescending(c => c.Order).Take(1))
|
||||
.Select(c => (uint?)c.Order)
|
||||
.FirstOrDefault(),
|
||||
|
||||
// Next chapter: first try same volume, then first chapter of next volume
|
||||
NextChapterVolumeOrder = chapter.Volume.Chapters
|
||||
.Where(c => c.Order > chapterOrder)
|
||||
.OrderBy(c => c.Order)
|
||||
.Select(c => (int?)chapter.Volume.Order)
|
||||
.FirstOrDefault()
|
||||
?? chapter.Volume.Novel.Volumes
|
||||
.Where(v => v.Order > chapter.Volume.Order)
|
||||
.OrderBy(v => v.Order)
|
||||
.SelectMany(v => v.Chapters.OrderBy(c => c.Order).Take(1))
|
||||
.Select(c => (int?)c.Volume.Order)
|
||||
.FirstOrDefault(),
|
||||
|
||||
NextChapterOrder = chapter.Volume.Chapters
|
||||
.Where(c => c.Order > chapterOrder)
|
||||
.OrderBy(c => c.Order)
|
||||
.Select(c => (uint?)c.Order)
|
||||
.FirstOrDefault()
|
||||
?? chapter.Volume.Novel.Volumes
|
||||
.Where(v => v.Order > chapter.Volume.Order)
|
||||
.OrderBy(v => v.Order)
|
||||
.SelectMany(v => v.Chapters.OrderBy(c => c.Order).Take(1))
|
||||
.Select(c => (uint?)c.Order)
|
||||
.FirstOrDefault()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
605
FictionArchive.Service.NovelService/Migrations/20251229203027_AddVolumes.Designer.cs
generated
Normal file
605
FictionArchive.Service.NovelService/Migrations/20251229203027_AddVolumes.Designer.cs
generated
Normal file
@@ -0,0 +1,605 @@
|
||||
// <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("20251229203027_AddVolumes")]
|
||||
partial class AddVolumes
|
||||
{
|
||||
/// <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.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("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,195 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using NodaTime;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace FictionArchive.Service.NovelService.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class AddVolumes : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
// 1. Create the Volume table
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Volume",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<long>(type: "bigint", nullable: false)
|
||||
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
|
||||
Order = table.Column<int>(type: "integer", nullable: false),
|
||||
NameId = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
NovelId = table.Column<long>(type: "bigint", 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_Volume", x => x.Id);
|
||||
table.ForeignKey(
|
||||
name: "FK_Volume_LocalizationKeys_NameId",
|
||||
column: x => x.NameId,
|
||||
principalTable: "LocalizationKeys",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
table.ForeignKey(
|
||||
name: "FK_Volume_Novels_NovelId",
|
||||
column: x => x.NovelId,
|
||||
principalTable: "Novels",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_Volume_NameId",
|
||||
table: "Volume",
|
||||
column: "NameId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_Volume_NovelId_Order",
|
||||
table: "Volume",
|
||||
columns: new[] { "NovelId", "Order" },
|
||||
unique: true);
|
||||
|
||||
// 2. Add nullable VolumeId column to Chapter (keep NovelId for now)
|
||||
migrationBuilder.AddColumn<long>(
|
||||
name: "VolumeId",
|
||||
table: "Chapter",
|
||||
type: "bigint",
|
||||
nullable: true);
|
||||
|
||||
// 3. Data migration: Create volumes and link chapters for each novel
|
||||
migrationBuilder.Sql(@"
|
||||
DO $$
|
||||
DECLARE
|
||||
novel_rec RECORD;
|
||||
loc_key_id uuid;
|
||||
volume_id bigint;
|
||||
BEGIN
|
||||
FOR novel_rec IN SELECT ""Id"", ""RawLanguage"" FROM ""Novels"" LOOP
|
||||
-- Create LocalizationKey for volume name
|
||||
loc_key_id := gen_random_uuid();
|
||||
INSERT INTO ""LocalizationKeys"" (""Id"", ""CreatedTime"", ""LastUpdatedTime"")
|
||||
VALUES (loc_key_id, NOW(), NOW());
|
||||
|
||||
-- Create LocalizationText for 'Main Story' in novel's raw language
|
||||
INSERT INTO ""LocalizationText"" (""Id"", ""LocalizationKeyId"", ""Language"", ""Text"", ""CreatedTime"", ""LastUpdatedTime"")
|
||||
VALUES (gen_random_uuid(), loc_key_id, novel_rec.""RawLanguage"", 'Main Story', NOW(), NOW());
|
||||
|
||||
-- Create Volume for this novel
|
||||
INSERT INTO ""Volume"" (""Order"", ""NameId"", ""NovelId"", ""CreatedTime"", ""LastUpdatedTime"")
|
||||
VALUES (1, loc_key_id, novel_rec.""Id"", NOW(), NOW())
|
||||
RETURNING ""Id"" INTO volume_id;
|
||||
|
||||
-- Link all chapters of this novel to the new volume
|
||||
UPDATE ""Chapter"" SET ""VolumeId"" = volume_id WHERE ""NovelId"" = novel_rec.""Id"";
|
||||
END LOOP;
|
||||
END $$;
|
||||
");
|
||||
|
||||
// 4. Drop old FK and index for NovelId
|
||||
migrationBuilder.DropForeignKey(
|
||||
name: "FK_Chapter_Novels_NovelId",
|
||||
table: "Chapter");
|
||||
|
||||
migrationBuilder.DropIndex(
|
||||
name: "IX_Chapter_NovelId",
|
||||
table: "Chapter");
|
||||
|
||||
// 5. Drop NovelId column from Chapter
|
||||
migrationBuilder.DropColumn(
|
||||
name: "NovelId",
|
||||
table: "Chapter");
|
||||
|
||||
// 6. Make VolumeId non-nullable
|
||||
migrationBuilder.AlterColumn<long>(
|
||||
name: "VolumeId",
|
||||
table: "Chapter",
|
||||
type: "bigint",
|
||||
nullable: false,
|
||||
oldClrType: typeof(long),
|
||||
oldType: "bigint",
|
||||
oldNullable: true);
|
||||
|
||||
// 7. Add unique index and FK for VolumeId
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_Chapter_VolumeId_Order",
|
||||
table: "Chapter",
|
||||
columns: new[] { "VolumeId", "Order" },
|
||||
unique: true);
|
||||
|
||||
migrationBuilder.AddForeignKey(
|
||||
name: "FK_Chapter_Volume_VolumeId",
|
||||
table: "Chapter",
|
||||
column: "VolumeId",
|
||||
principalTable: "Volume",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
// Add back NovelId column
|
||||
migrationBuilder.AddColumn<long>(
|
||||
name: "NovelId",
|
||||
table: "Chapter",
|
||||
type: "bigint",
|
||||
nullable: true);
|
||||
|
||||
// Migrate data back: set NovelId from Volume
|
||||
migrationBuilder.Sql(@"
|
||||
UPDATE ""Chapter"" c
|
||||
SET ""NovelId"" = v.""NovelId""
|
||||
FROM ""Volume"" v
|
||||
WHERE c.""VolumeId"" = v.""Id"";
|
||||
");
|
||||
|
||||
// Make NovelId non-nullable
|
||||
migrationBuilder.AlterColumn<long>(
|
||||
name: "NovelId",
|
||||
table: "Chapter",
|
||||
type: "bigint",
|
||||
nullable: false,
|
||||
oldClrType: typeof(long),
|
||||
oldType: "bigint",
|
||||
oldNullable: true);
|
||||
|
||||
// Drop VolumeId FK and index
|
||||
migrationBuilder.DropForeignKey(
|
||||
name: "FK_Chapter_Volume_VolumeId",
|
||||
table: "Chapter");
|
||||
|
||||
migrationBuilder.DropIndex(
|
||||
name: "IX_Chapter_VolumeId_Order",
|
||||
table: "Chapter");
|
||||
|
||||
// Drop VolumeId column
|
||||
migrationBuilder.DropColumn(
|
||||
name: "VolumeId",
|
||||
table: "Chapter");
|
||||
|
||||
// Recreate NovelId index and FK
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_Chapter_NovelId",
|
||||
table: "Chapter",
|
||||
column: "NovelId");
|
||||
|
||||
migrationBuilder.AddForeignKey(
|
||||
name: "FK_Chapter_Novels_NovelId",
|
||||
table: "Chapter",
|
||||
column: "NovelId",
|
||||
principalTable: "Novels",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
|
||||
// Note: Volume LocalizationKeys are not cleaned up in Down migration
|
||||
// as they may have been modified. Manual cleanup may be needed.
|
||||
migrationBuilder.DropTable(
|
||||
name: "Volume");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -153,9 +153,6 @@ namespace FictionArchive.Service.NovelService.Migrations
|
||||
b.Property<Guid>("NameId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<long>("NovelId")
|
||||
.HasColumnType("bigint");
|
||||
|
||||
b.Property<long>("Order")
|
||||
.HasColumnType("bigint");
|
||||
|
||||
@@ -165,13 +162,17 @@ namespace FictionArchive.Service.NovelService.Migrations
|
||||
b.Property<string>("Url")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<long>("VolumeId")
|
||||
.HasColumnType("bigint");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("BodyId");
|
||||
|
||||
b.HasIndex("NameId");
|
||||
|
||||
b.HasIndex("NovelId");
|
||||
b.HasIndex("VolumeId", "Order")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("Chapter");
|
||||
});
|
||||
@@ -357,6 +358,39 @@ namespace FictionArchive.Service.NovelService.Migrations
|
||||
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("NovelNovelTag", b =>
|
||||
{
|
||||
b.Property<long>("NovelsId")
|
||||
@@ -427,9 +461,9 @@ namespace FictionArchive.Service.NovelService.Migrations
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("FictionArchive.Service.NovelService.Models.Novels.Novel", "Novel")
|
||||
b.HasOne("FictionArchive.Service.NovelService.Models.Novels.Volume", "Volume")
|
||||
.WithMany("Chapters")
|
||||
.HasForeignKey("NovelId")
|
||||
.HasForeignKey("VolumeId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
@@ -437,7 +471,7 @@ namespace FictionArchive.Service.NovelService.Migrations
|
||||
|
||||
b.Navigation("Name");
|
||||
|
||||
b.Navigation("Novel");
|
||||
b.Navigation("Volume");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("FictionArchive.Service.NovelService.Models.Novels.Novel", b =>
|
||||
@@ -509,6 +543,25 @@ namespace FictionArchive.Service.NovelService.Migrations
|
||||
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)
|
||||
@@ -535,6 +588,11 @@ namespace FictionArchive.Service.NovelService.Migrations
|
||||
});
|
||||
|
||||
modelBuilder.Entity("FictionArchive.Service.NovelService.Models.Novels.Novel", b =>
|
||||
{
|
||||
b.Navigation("Volumes");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("FictionArchive.Service.NovelService.Models.Novels.Volume", b =>
|
||||
{
|
||||
b.Navigation("Chapters");
|
||||
});
|
||||
|
||||
@@ -12,7 +12,16 @@ public class ChapterReaderDto : BaseDto<uint>
|
||||
// Navigation context
|
||||
public uint NovelId { get; init; }
|
||||
public required string NovelName { get; init; }
|
||||
public int TotalChapters { get; init; }
|
||||
|
||||
// Volume context
|
||||
public uint VolumeId { get; init; }
|
||||
public required string VolumeName { get; init; }
|
||||
public int VolumeOrder { get; init; }
|
||||
public int TotalChaptersInVolume { get; init; }
|
||||
|
||||
// Cross-volume navigation (VolumeOrder + ChapterOrder identify a chapter)
|
||||
public int? PrevChapterVolumeOrder { get; init; }
|
||||
public uint? PrevChapterOrder { get; init; }
|
||||
public int? NextChapterVolumeOrder { get; init; }
|
||||
public uint? NextChapterOrder { get; init; }
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ public class NovelDto : BaseDto<uint>
|
||||
public required string ExternalId { get; init; }
|
||||
public required string Name { get; init; }
|
||||
public required string Description { get; init; }
|
||||
public required List<ChapterDto> Chapters { get; init; }
|
||||
public required List<VolumeDto> Volumes { get; init; }
|
||||
public required List<NovelTagDto> Tags { get; init; }
|
||||
public ImageDto? CoverImage { get; init; }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace FictionArchive.Service.NovelService.Models.DTOs;
|
||||
|
||||
public class VolumeDto : BaseDto<uint>
|
||||
{
|
||||
public int Order { get; init; }
|
||||
public required string Name { get; init; }
|
||||
public required List<ChapterDto> Chapters { get; init; }
|
||||
}
|
||||
@@ -1,9 +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 ChapterNumber { 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,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; }
|
||||
}
|
||||
@@ -20,7 +20,7 @@ public class Chapter : BaseEntity<uint>
|
||||
|
||||
#region Navigation Properties
|
||||
|
||||
public Novel Novel { get; set; }
|
||||
public Volume Volume { get; set; }
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -21,7 +21,7 @@ public class Novel : BaseEntity<uint>
|
||||
public LocalizationKey Name { get; set; }
|
||||
public LocalizationKey Description { get; set; }
|
||||
|
||||
public List<Chapter> Chapters { get; set; }
|
||||
public List<Volume> Volumes { get; set; }
|
||||
public List<NovelTag> Tags { get; set; }
|
||||
public Image? CoverImage { get; set; }
|
||||
}
|
||||
24
FictionArchive.Service.NovelService/Models/Novels/Volume.cs
Normal file
24
FictionArchive.Service.NovelService/Models/Novels/Volume.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using FictionArchive.Service.NovelService.Models.Localization;
|
||||
using FictionArchive.Service.Shared.Models;
|
||||
|
||||
namespace FictionArchive.Service.NovelService.Models.Novels;
|
||||
|
||||
[Table("Volume")]
|
||||
public class Volume : BaseEntity<uint>
|
||||
{
|
||||
/// <summary>
|
||||
/// Signed int to allow special ordering like -1 for "Author Notes" at top.
|
||||
/// </summary>
|
||||
public int Order { get; set; }
|
||||
|
||||
public LocalizationKey Name { get; set; }
|
||||
|
||||
public List<Chapter> Chapters { get; set; }
|
||||
|
||||
#region Navigation Properties
|
||||
|
||||
public Novel Novel { get; set; }
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -16,7 +16,7 @@ public class NovelMetadata
|
||||
public Language RawLanguage { get; set; }
|
||||
public NovelStatus RawStatus { get; set; }
|
||||
|
||||
public List<ChapterMetadata> Chapters { get; set; }
|
||||
public List<VolumeMetadata> Volumes { get; set; }
|
||||
public List<string> SourceTags { get; set; }
|
||||
public List<string> SystemTags { get; set; }
|
||||
public SourceDescriptor SourceDescriptor { get; set; }
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace FictionArchive.Service.NovelService.Models.SourceAdapters;
|
||||
|
||||
public class VolumeMetadata
|
||||
{
|
||||
public int Order { get; set; }
|
||||
public string Name { get; set; }
|
||||
public List<ChapterMetadata> Chapters { get; set; }
|
||||
}
|
||||
@@ -1,14 +1,12 @@
|
||||
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.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 Microsoft.EntityFrameworkCore;
|
||||
|
||||
@@ -25,18 +23,19 @@ public class Program
|
||||
|
||||
builder.Services.AddMemoryCache();
|
||||
|
||||
#region Event Bus
|
||||
#region MassTransit
|
||||
|
||||
if (!isSchemaExport)
|
||||
{
|
||||
builder.Services.AddRabbitMQ(opt =>
|
||||
{
|
||||
builder.Configuration.GetSection("RabbitMQ").Bind(opt);
|
||||
})
|
||||
.Subscribe<TranslationRequestCompletedEvent, TranslationRequestCompletedEventHandler>()
|
||||
.Subscribe<NovelUpdateRequestedEvent, NovelUpdateRequestedEventHandler>()
|
||||
.Subscribe<ChapterPullRequestedEvent, ChapterPullRequestedEventHandler>()
|
||||
.Subscribe<FileUploadRequestStatusUpdateEvent, FileUploadRequestStatusUpdateEventHandler>();
|
||||
builder.Services.AddFictionArchiveMassTransit(
|
||||
builder.Configuration,
|
||||
x =>
|
||||
{
|
||||
x.AddConsumer<TranslationRequestCompletedConsumer>();
|
||||
x.AddConsumer<FileUploadRequestStatusUpdateConsumer>();
|
||||
x.AddConsumer<NovelUpdateRequestedConsumer>();
|
||||
x.AddConsumer<ChapterPullRequestedConsumer>();
|
||||
});
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -44,7 +43,6 @@ public class Program
|
||||
#region GraphQL
|
||||
|
||||
builder.Services.AddDefaultGraphQl<Query, Mutation>()
|
||||
.ModifyCostOptions(opt => opt.MaxFieldCost = 5000)
|
||||
.AddAuthorization();
|
||||
|
||||
#endregion
|
||||
@@ -63,12 +61,14 @@ public class Program
|
||||
builder.Services.AddHttpClient<NovelpiaAuthMessageHandler>(client =>
|
||||
{
|
||||
client.BaseAddress = new Uri("https://novelpia.com");
|
||||
});
|
||||
})
|
||||
.AddStandardResilienceHandler();
|
||||
builder.Services.AddHttpClient<ISourceAdapter, NovelpiaAdapter>(client =>
|
||||
{
|
||||
client.BaseAddress = new Uri("https://novelpia.com");
|
||||
})
|
||||
.AddHttpMessageHandler<NovelpiaAuthMessageHandler>();
|
||||
.AddHttpMessageHandler<NovelpiaAuthMessageHandler>()
|
||||
.AddStandardResilienceHandler();
|
||||
|
||||
builder.Services.Configure<NovelUpdateServiceConfiguration>(builder.Configuration.GetSection("UpdateService"));
|
||||
builder.Services.AddTransient<NovelUpdateService>();
|
||||
|
||||
@@ -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.ChapterNumber);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ public class NovelServiceDbContext(DbContextOptions options, ILogger<NovelServic
|
||||
: FictionArchiveDbContext(options, logger)
|
||||
{
|
||||
public DbSet<Novel> Novels { get; set; }
|
||||
public DbSet<Volume> Volumes { get; set; }
|
||||
public DbSet<Chapter> Chapters { get; set; }
|
||||
public DbSet<Source> Sources { get; set; }
|
||||
public DbSet<TranslationEngine> TranslationEngines { get; set; }
|
||||
@@ -25,5 +26,15 @@ public class NovelServiceDbContext(DbContextOptions options, ILogger<NovelServic
|
||||
modelBuilder.Entity<Novel>()
|
||||
.HasIndex("ExternalId", "SourceId")
|
||||
.IsUnique();
|
||||
|
||||
// Volume.Order is unique per Novel
|
||||
modelBuilder.Entity<Volume>()
|
||||
.HasIndex("NovelId", "Order")
|
||||
.IsUnique();
|
||||
|
||||
// Chapter.Order is unique per Volume
|
||||
modelBuilder.Entity<Chapter>()
|
||||
.HasIndex("VolumeId", "Order")
|
||||
.IsUnique();
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,15 @@
|
||||
using FictionArchive.Common.Enums;
|
||||
using FictionArchive.Service.FileService.IntegrationEvents;
|
||||
using FictionArchive.Service.NovelService.Contracts;
|
||||
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;
|
||||
|
||||
@@ -20,15 +20,15 @@ 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;
|
||||
|
||||
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)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_logger = logger;
|
||||
_sourceAdapters = sourceAdapters;
|
||||
_eventBus = eventBus;
|
||||
_publishEndpoint = publishEndpoint;
|
||||
_novelUpdateServiceConfiguration = novelUpdateServiceConfiguration.Value;
|
||||
}
|
||||
|
||||
@@ -190,6 +190,48 @@ public class NovelUpdateService
|
||||
return existingChapters.Concat(newChapters).ToList();
|
||||
}
|
||||
|
||||
private static List<Volume> SynchronizeVolumes(
|
||||
List<VolumeMetadata> metadataVolumes,
|
||||
Language rawLanguage,
|
||||
List<Volume>? existingVolumes)
|
||||
{
|
||||
existingVolumes ??= new List<Volume>();
|
||||
var result = new List<Volume>();
|
||||
|
||||
foreach (var metaVolume in metadataVolumes)
|
||||
{
|
||||
// Match volumes by Order (unique per novel)
|
||||
var existingVolume = existingVolumes.FirstOrDefault(v => v.Order == metaVolume.Order);
|
||||
|
||||
if (existingVolume != null)
|
||||
{
|
||||
// Volume exists - sync its chapters
|
||||
existingVolume.Chapters = SynchronizeChapters(
|
||||
metaVolume.Chapters,
|
||||
rawLanguage,
|
||||
existingVolume.Chapters);
|
||||
result.Add(existingVolume);
|
||||
}
|
||||
else
|
||||
{
|
||||
// New volume - create it with synced chapters
|
||||
var newVolume = new Volume
|
||||
{
|
||||
Order = metaVolume.Order,
|
||||
Name = LocalizationKey.CreateFromText(metaVolume.Name, rawLanguage),
|
||||
Chapters = SynchronizeChapters(metaVolume.Chapters, rawLanguage, null)
|
||||
};
|
||||
result.Add(newVolume);
|
||||
}
|
||||
}
|
||||
|
||||
// Keep existing volumes not in metadata (user-created volumes)
|
||||
var metaOrders = metadataVolumes.Select(v => v.Order).ToHashSet();
|
||||
result.AddRange(existingVolumes.Where(v => !metaOrders.Contains(v.Order)));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static (Image? image, bool shouldPublishEvent) HandleCoverImage(
|
||||
ImageData? newCoverData,
|
||||
Image? existingCoverImage)
|
||||
@@ -232,7 +274,7 @@ public class NovelUpdateService
|
||||
metadata.SystemTags,
|
||||
metadata.RawLanguage);
|
||||
|
||||
var chapters = SynchronizeChapters(metadata.Chapters, metadata.RawLanguage, null);
|
||||
var volumes = SynchronizeVolumes(metadata.Volumes, metadata.RawLanguage, null);
|
||||
|
||||
var novel = new Novel
|
||||
{
|
||||
@@ -243,7 +285,7 @@ public class NovelUpdateService
|
||||
CoverImage = metadata.CoverImage != null
|
||||
? new Image { OriginalPath = metadata.CoverImage.Url }
|
||||
: null,
|
||||
Chapters = chapters,
|
||||
Volumes = volumes,
|
||||
Description = LocalizationKey.CreateFromText(metadata.Description, metadata.RawLanguage),
|
||||
Name = LocalizationKey.CreateFromText(metadata.Name, metadata.RawLanguage),
|
||||
RawStatus = metadata.RawStatus,
|
||||
@@ -281,16 +323,20 @@ public class NovelUpdateService
|
||||
// Step 3: Check for existing novel by ExternalId + Source.Key
|
||||
var existingNovel = await _dbContext.Novels
|
||||
.Include(n => n.Author)
|
||||
.ThenInclude(a => a.Name)
|
||||
.ThenInclude(lk => lk.Texts)
|
||||
.ThenInclude(a => a.Name)
|
||||
.ThenInclude(lk => lk.Texts)
|
||||
.Include(n => n.Source)
|
||||
.Include(n => n.Name)
|
||||
.ThenInclude(lk => lk.Texts)
|
||||
.ThenInclude(lk => lk.Texts)
|
||||
.Include(n => n.Description)
|
||||
.ThenInclude(lk => lk.Texts)
|
||||
.ThenInclude(lk => lk.Texts)
|
||||
.Include(n => n.Tags)
|
||||
.Include(n => n.Chapters)
|
||||
.Include(n => n.CoverImage)
|
||||
.Include(n => n.Volumes)
|
||||
.ThenInclude(volume => volume.Chapters)
|
||||
.ThenInclude(chapter => chapter.Body)
|
||||
.ThenInclude(localizationKey => localizationKey.Texts)
|
||||
.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);
|
||||
@@ -298,6 +344,12 @@ public class NovelUpdateService
|
||||
Novel novel;
|
||||
bool shouldPublishCoverEvent;
|
||||
|
||||
// Capture existing chapter IDs to detect new chapters later
|
||||
var existingChapterIds = existingNovel?.Volumes
|
||||
.SelectMany(v => v.Chapters)
|
||||
.Select(c => c.Id)
|
||||
.ToHashSet() ?? new HashSet<uint>();
|
||||
|
||||
if (existingNovel == null)
|
||||
{
|
||||
// CREATE PATH: New novel
|
||||
@@ -325,11 +377,11 @@ public class NovelUpdateService
|
||||
metadata.SystemTags,
|
||||
metadata.RawLanguage);
|
||||
|
||||
// Synchronize chapters (add only)
|
||||
novel.Chapters = SynchronizeChapters(
|
||||
metadata.Chapters,
|
||||
// Synchronize volumes (and their chapters)
|
||||
novel.Volumes = SynchronizeVolumes(
|
||||
metadata.Volumes,
|
||||
metadata.RawLanguage,
|
||||
existingNovel.Chapters);
|
||||
existingNovel.Volumes);
|
||||
|
||||
// Handle cover image
|
||||
(novel.CoverImage, shouldPublishCoverEvent) = HandleCoverImage(
|
||||
@@ -339,51 +391,93 @@ public class NovelUpdateService
|
||||
|
||||
await _dbContext.SaveChangesAsync();
|
||||
|
||||
// Publish novel created event for new novels
|
||||
if (existingNovel == null)
|
||||
{
|
||||
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
|
||||
foreach (var volume in novel.Volumes)
|
||||
{
|
||||
foreach (var chapter in volume.Chapters.Where(c => !existingChapterIds.Contains(c.Id)))
|
||||
{
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
// 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(
|
||||
novel.CoverImage.Id,
|
||||
$"Novels/{novel.Id}/Images/cover.jpg",
|
||||
metadata.CoverImage.Data));
|
||||
}
|
||||
|
||||
// Publish chapter pull events for chapters without body content
|
||||
var chaptersNeedingPull = novel.Chapters
|
||||
.Where(c => c.Body?.Texts == null || !c.Body.Texts.Any())
|
||||
.ToList();
|
||||
|
||||
foreach (var chapter in chaptersNeedingPull)
|
||||
foreach (var volume in novel.Volumes)
|
||||
{
|
||||
await _eventBus.Publish(new ChapterPullRequestedEvent
|
||||
var chaptersNeedingPull = volume.Chapters
|
||||
.Where(c => c.Body?.Texts == null || !c.Body.Texts.Any())
|
||||
.ToList();
|
||||
|
||||
foreach (var chapter in chaptersNeedingPull)
|
||||
{
|
||||
NovelId = novel.Id,
|
||||
ChapterNumber = chapter.Order
|
||||
});
|
||||
await _publishEndpoint.Publish<IChapterPullRequested>(new ChapterPullRequested(
|
||||
novel.Id,
|
||||
volume.Id,
|
||||
chapter.Order));
|
||||
}
|
||||
}
|
||||
|
||||
return novel;
|
||||
}
|
||||
|
||||
public async Task<Chapter> PullChapterContents(uint novelId, uint chapterNumber)
|
||||
public async Task<Chapter> PullChapterContents(uint novelId, uint volumeId, uint chapterOrder)
|
||||
{
|
||||
var novel = await _dbContext.Novels.Where(novel => novel.Id == novelId)
|
||||
.Include(novel => novel.Chapters)
|
||||
.Include(novel => novel.Volumes)
|
||||
.ThenInclude(volume => volume.Chapters)
|
||||
.ThenInclude(chapter => chapter.Body)
|
||||
.ThenInclude(body => body.Texts)
|
||||
.Include(novel => novel.Source).Include(novel => novel.Chapters).ThenInclude(chapter => chapter.Images)
|
||||
.Include(novel => novel.Source)
|
||||
.Include(novel => novel.Volumes)
|
||||
.ThenInclude(volume => volume.Chapters)
|
||||
.ThenInclude(chapter => chapter.Images)
|
||||
.FirstOrDefaultAsync();
|
||||
var chapter = novel.Chapters.Where(chapter => chapter.Order == chapterNumber).FirstOrDefault();
|
||||
var volume = novel.Volumes.FirstOrDefault(v => v.Id == volumeId);
|
||||
var chapter = volume.Chapters.FirstOrDefault(c => c.Order == chapterOrder);
|
||||
var adapter = _sourceAdapters.FirstOrDefault(adapter => adapter.SourceDescriptor.Key == novel.Source.Key);
|
||||
var rawChapter = await adapter.GetRawChapter(chapter.Url);
|
||||
var localizationText = new LocalizationText()
|
||||
|
||||
// If we already have the raw for this, overwrite it for now. Revisions will come later.
|
||||
var localizationText = chapter.Body.Texts.FirstOrDefault(text => text.Language == novel.RawLanguage);
|
||||
if (localizationText == null)
|
||||
{
|
||||
Text = rawChapter.Text,
|
||||
Language = novel.RawLanguage
|
||||
};
|
||||
chapter.Body.Texts.Add(localizationText);
|
||||
localizationText = new LocalizationText()
|
||||
{
|
||||
Text = rawChapter.Text,
|
||||
Language = novel.RawLanguage
|
||||
};
|
||||
chapter.Body.Texts.Add(localizationText);
|
||||
}
|
||||
else
|
||||
{
|
||||
localizationText.Text = rawChapter.Text;
|
||||
}
|
||||
|
||||
chapter.Images = rawChapter.ImageData.Select(img => new Image()
|
||||
{
|
||||
OriginalPath = img.Url
|
||||
@@ -417,12 +511,10 @@ 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(
|
||||
image.Id,
|
||||
$"Novels/{novel.Id}/Images/Chapter-{chapter.Id}/{imgCount++}.jpg",
|
||||
data.Data));
|
||||
}
|
||||
|
||||
return chapter;
|
||||
@@ -456,24 +548,74 @@ public class NovelUpdateService
|
||||
await _dbContext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task<NovelUpdateRequestedEvent> QueueNovelImport(string novelUrl)
|
||||
public async Task<NovelUpdateRequested> QueueNovelImport(string novelUrl)
|
||||
{
|
||||
var importNovelRequestEvent = new NovelUpdateRequestedEvent()
|
||||
{
|
||||
NovelUrl = novelUrl
|
||||
};
|
||||
await _eventBus.Publish(importNovelRequestEvent);
|
||||
var importNovelRequestEvent = new NovelUpdateRequested(novelUrl);
|
||||
await _publishEndpoint.Publish<INovelUpdateRequested>(importNovelRequestEvent);
|
||||
return importNovelRequestEvent;
|
||||
}
|
||||
|
||||
public async Task<ChapterPullRequestedEvent> QueueChapterPull(uint novelId, uint chapterNumber)
|
||||
public async Task<ChapterPullRequested> QueueChapterPull(uint novelId, uint volumeId, uint chapterOrder)
|
||||
{
|
||||
var chapterPullEvent = new ChapterPullRequestedEvent()
|
||||
{
|
||||
NovelId = novelId,
|
||||
ChapterNumber = chapterNumber
|
||||
};
|
||||
await _eventBus.Publish(chapterPullEvent);
|
||||
var chapterPullEvent = new ChapterPullRequested(novelId, volumeId, chapterOrder);
|
||||
await _publishEndpoint.Publish<IChapterPullRequested>(chapterPullEvent);
|
||||
return chapterPullEvent;
|
||||
}
|
||||
|
||||
public async Task DeleteNovel(uint novelId)
|
||||
{
|
||||
var novel = await _dbContext.Novels
|
||||
.Include(n => n.CoverImage)
|
||||
.Include(n => n.Name).ThenInclude(k => k.Texts)
|
||||
.Include(n => n.Description).ThenInclude(k => k.Texts)
|
||||
.Include(n => n.Volumes).ThenInclude(v => v.Name).ThenInclude(k => k.Texts)
|
||||
.Include(n => n.Volumes).ThenInclude(v => v.Chapters).ThenInclude(c => c.Images)
|
||||
.Include(n => n.Volumes).ThenInclude(v => v.Chapters).ThenInclude(c => c.Name).ThenInclude(k => k.Texts)
|
||||
.Include(n => n.Volumes).ThenInclude(v => v.Chapters).ThenInclude(c => c.Body).ThenInclude(k => k.Texts)
|
||||
.FirstOrDefaultAsync(n => n.Id == novelId);
|
||||
|
||||
if (novel == null)
|
||||
throw new KeyNotFoundException($"Novel with ID '{novelId}' not found");
|
||||
|
||||
// Collect all LocalizationKey IDs for cleanup
|
||||
var locKeyIds = new List<Guid> { novel.Name.Id, novel.Description.Id };
|
||||
foreach (var volume in novel.Volumes)
|
||||
{
|
||||
locKeyIds.Add(volume.Name.Id);
|
||||
locKeyIds.AddRange(volume.Chapters.Select(c => c.Name.Id));
|
||||
locKeyIds.AddRange(volume.Chapters.Select(c => c.Body.Id));
|
||||
}
|
||||
|
||||
// 1. Remove LocalizationRequests referencing these keys
|
||||
var locRequests = await _dbContext.LocalizationRequests
|
||||
.Where(r => locKeyIds.Contains(r.KeyRequestedForTranslation.Id))
|
||||
.ToListAsync();
|
||||
_dbContext.LocalizationRequests.RemoveRange(locRequests);
|
||||
|
||||
// 2. Remove LocalizationTexts (NO ACTION FK - won't cascade)
|
||||
_dbContext.RemoveRange(novel.Name.Texts);
|
||||
_dbContext.RemoveRange(novel.Description.Texts);
|
||||
foreach (var volume in novel.Volumes)
|
||||
{
|
||||
_dbContext.RemoveRange(volume.Name.Texts);
|
||||
foreach (var chapter in volume.Chapters)
|
||||
{
|
||||
_dbContext.RemoveRange(chapter.Name.Texts);
|
||||
_dbContext.RemoveRange(chapter.Body.Texts);
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Remove Images (NO ACTION FK - won't cascade)
|
||||
if (novel.CoverImage != null)
|
||||
_dbContext.Images.Remove(novel.CoverImage);
|
||||
foreach (var volume in novel.Volumes)
|
||||
{
|
||||
foreach (var chapter in volume.Chapters)
|
||||
_dbContext.Images.RemoveRange(chapter.Images);
|
||||
}
|
||||
|
||||
// 4. Remove novel - cascades: volumes, chapters, localization keys, tag mappings
|
||||
_dbContext.Novels.Remove(novel);
|
||||
await _dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,7 +66,7 @@ public class NovelpiaAdapter : ISourceAdapter
|
||||
ExternalId = novelId.ToString(),
|
||||
SystemTags = new List<string>(),
|
||||
SourceTags = new List<string>(),
|
||||
Chapters = new List<ChapterMetadata>(),
|
||||
Volumes = new List<VolumeMetadata>(),
|
||||
SourceDescriptor = SourceDescriptor
|
||||
};
|
||||
|
||||
@@ -133,15 +133,16 @@ public class NovelpiaAdapter : ISourceAdapter
|
||||
novel.SourceTags.Add(tag);
|
||||
}
|
||||
|
||||
// Author's posts (from notice_table in the page HTML)
|
||||
var authorsPosts = ParseAuthorsPosts(novelData);
|
||||
|
||||
// Chapters
|
||||
uint page = 0;
|
||||
List<ChapterMetadata> chapters = new List<ChapterMetadata>();
|
||||
List<uint> seenChapterIds = new List<uint>();
|
||||
uint chapterOrder = 0;
|
||||
uint chapterOrder = 1;
|
||||
while (true)
|
||||
{
|
||||
await Task.Delay(500);
|
||||
_logger.LogInformation("Next chapter batch");
|
||||
var response = await _httpClient.PostAsync(EpisodeListEndpoint, new FormUrlEncodedContent(new Dictionary<string, string>
|
||||
{
|
||||
{"novel_no", novelId.ToString()},
|
||||
@@ -170,7 +171,25 @@ public class NovelpiaAdapter : ISourceAdapter
|
||||
}
|
||||
page++;
|
||||
}
|
||||
novel.Chapters = chapters;
|
||||
|
||||
// Add Author's Posts volume if there are any
|
||||
if (authorsPosts.Count > 0)
|
||||
{
|
||||
novel.Volumes.Add(new VolumeMetadata
|
||||
{
|
||||
Order = 0,
|
||||
Name = "Author's Posts",
|
||||
Chapters = authorsPosts
|
||||
});
|
||||
}
|
||||
|
||||
// Main Story volume
|
||||
novel.Volumes.Add(new VolumeMetadata
|
||||
{
|
||||
Order = 1,
|
||||
Name = "Main Story",
|
||||
Chapters = chapters
|
||||
});
|
||||
|
||||
return novel;
|
||||
}
|
||||
@@ -243,4 +262,40 @@ public class NovelpiaAdapter : ISourceAdapter
|
||||
}
|
||||
return await image.Content.ReadAsByteArrayAsync();
|
||||
}
|
||||
|
||||
private List<ChapterMetadata> ParseAuthorsPosts(string novelHtml)
|
||||
{
|
||||
var posts = new List<ChapterMetadata>();
|
||||
|
||||
// Find the notice_table section
|
||||
var noticeTableMatch = Regex.Match(novelHtml,
|
||||
@"(?s)<table[^>]*class=""notice_table[^""]*""[^>]*>(.*?)</table>");
|
||||
if (!noticeTableMatch.Success)
|
||||
return posts;
|
||||
|
||||
var tableContent = noticeTableMatch.Groups[1].Value;
|
||||
|
||||
// Find all td elements with onclick containing viewer URL and extract title from <b>
|
||||
// HTML structure: <td ... onclick="...location='/viewer/3330612';"><b>Title</b>
|
||||
var postMatches = Regex.Matches(tableContent,
|
||||
@"onclick=""[^""]*location='/viewer/(\d+)'[^""]*""[^>]*><b>([^<]+)</b>");
|
||||
|
||||
uint order = 1;
|
||||
foreach (Match match in postMatches)
|
||||
{
|
||||
string viewerId = match.Groups[1].Value;
|
||||
string title = WebUtility.HtmlDecode(match.Groups[2].Value.Trim());
|
||||
|
||||
posts.Add(new ChapterMetadata
|
||||
{
|
||||
Revision = 0,
|
||||
Order = order,
|
||||
Url = $"https://novelpia.com/viewer/{viewerId}",
|
||||
Name = title
|
||||
});
|
||||
order++;
|
||||
}
|
||||
|
||||
return posts;
|
||||
}
|
||||
}
|
||||
@@ -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,8 @@
|
||||
namespace FictionArchive.Service.Shared.Contracts.Events;
|
||||
|
||||
public interface IChapterPullRequested
|
||||
{
|
||||
uint NovelId { get; }
|
||||
uint VolumeId { get; }
|
||||
uint ChapterOrder { get; }
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace FictionArchive.Service.Shared.Contracts.Events;
|
||||
|
||||
public interface IFileUploadRequestCreated
|
||||
{
|
||||
Guid RequestId { get; }
|
||||
string FilePath { get; }
|
||||
byte[] FileData { get; }
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using FictionArchive.Common.Enums;
|
||||
|
||||
namespace FictionArchive.Service.Shared.Contracts.Events;
|
||||
|
||||
public interface IFileUploadRequestStatusUpdate
|
||||
{
|
||||
Guid RequestId { get; }
|
||||
RequestStatus Status { get; }
|
||||
string? FileAccessUrl { get; }
|
||||
string? ErrorMessage { get; }
|
||||
}
|
||||
@@ -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,6 @@
|
||||
namespace FictionArchive.Service.Shared.Contracts.Events;
|
||||
|
||||
public interface INovelUpdateRequested
|
||||
{
|
||||
string NovelUrl { get; }
|
||||
}
|
||||
@@ -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; }
|
||||
}
|
||||
@@ -6,6 +6,7 @@ using Microsoft.IdentityModel.Tokens;
|
||||
using FictionArchive.Service.Shared.Constants;
|
||||
using FictionArchive.Service.Shared.Models.Authentication;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace FictionArchive.Service.Shared.Extensions;
|
||||
|
||||
@@ -78,7 +79,7 @@ public static class AuthenticationExtensions
|
||||
|
||||
logger.LogDebug(
|
||||
"JWT token validated for subject: {Subject}",
|
||||
context.Principal?.FindFirst("sub")?.Value ?? "unknown");
|
||||
context.Principal?.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? "unknown");
|
||||
|
||||
return existingEvents?.OnTokenValidated?.Invoke(context) ?? Task.CompletedTask;
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ public static class GraphQLExtensions
|
||||
.AddErrorFilter<LoggingErrorFilter>()
|
||||
.AddType<UnsignedIntType>()
|
||||
.AddType<InstantType>()
|
||||
.ModifyCostOptions(opt => opt.MaxFieldCost = 10000)
|
||||
.AddMutationConventions(applyToAllMutations: true)
|
||||
.AddFiltering(opt => opt.AddDefaults().BindRuntimeType<uint, UnsignedIntOperationFilterInputType>())
|
||||
.AddSorting()
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -25,11 +25,13 @@
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="10.1.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
<PackageReference Include="NodaTime.Serialization.JsonNet" Version="3.2.0" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime" Version="9.0.4" />
|
||||
<PackageReference Include="RabbitMQ.Client" Version="7.2.0" />
|
||||
<PackageReference Include="Polly" Version="8.6.5" />
|
||||
<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.Configuration.GetSection("RabbitMQ").Bind(opt);
|
||||
})
|
||||
.Subscribe<TranslationRequestCreatedEvent, TranslationRequestCreatedEventHandler>();
|
||||
builder.Services.AddFictionArchiveMassTransit(
|
||||
builder.Configuration,
|
||||
x =>
|
||||
{
|
||||
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": {
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
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 ChapterCreatedConsumer : IConsumer<IChapterCreated>
|
||||
{
|
||||
private readonly ILogger<ChapterCreatedConsumer> _logger;
|
||||
private readonly UserNovelDataServiceDbContext _dbContext;
|
||||
|
||||
public ChapterCreatedConsumer(
|
||||
ILogger<ChapterCreatedConsumer> logger,
|
||||
UserNovelDataServiceDbContext dbContext)
|
||||
{
|
||||
_logger = logger;
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
public async Task Consume(ConsumeContext<IChapterCreated> context)
|
||||
{
|
||||
var message = context.Message;
|
||||
|
||||
// Ensure novel exists
|
||||
var novelExists = await _dbContext.Novels.AnyAsync(n => n.Id == message.NovelId);
|
||||
if (!novelExists)
|
||||
{
|
||||
var novel = new Novel { Id = message.NovelId };
|
||||
_dbContext.Novels.Add(novel);
|
||||
}
|
||||
|
||||
// Ensure volume exists
|
||||
var volumeExists = await _dbContext.Volumes.AnyAsync(v => v.Id == message.VolumeId);
|
||||
if (!volumeExists)
|
||||
{
|
||||
var volume = new Volume { Id = message.VolumeId };
|
||||
_dbContext.Volumes.Add(volume);
|
||||
}
|
||||
|
||||
// Create chapter if not exists
|
||||
var chapterExists = await _dbContext.Chapters.AnyAsync(c => c.Id == message.ChapterId);
|
||||
if (chapterExists)
|
||||
{
|
||||
_logger.LogDebug("Chapter {ChapterId} already exists, skipping", message.ChapterId);
|
||||
return;
|
||||
}
|
||||
|
||||
var chapter = new Chapter { Id = message.ChapterId };
|
||||
_dbContext.Chapters.Add(chapter);
|
||||
await _dbContext.SaveChangesAsync();
|
||||
|
||||
_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);
|
||||
}
|
||||
}
|
||||
23
FictionArchive.Service.UserNovelDataService/Dockerfile
Normal file
23
FictionArchive.Service.UserNovelDataService/Dockerfile
Normal file
@@ -0,0 +1,23 @@
|
||||
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
|
||||
USER $APP_UID
|
||||
WORKDIR /app
|
||||
EXPOSE 8080
|
||||
EXPOSE 8081
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
WORKDIR /src
|
||||
COPY ["FictionArchive.Service.UserNovelDataService/FictionArchive.Service.UserNovelDataService.csproj", "FictionArchive.Service.UserNovelDataService/"]
|
||||
RUN dotnet restore "FictionArchive.Service.UserNovelDataService/FictionArchive.Service.UserNovelDataService.csproj"
|
||||
COPY . .
|
||||
WORKDIR "/src/FictionArchive.Service.UserNovelDataService"
|
||||
RUN dotnet build "./FictionArchive.Service.UserNovelDataService.csproj" -c $BUILD_CONFIGURATION -o /app/build
|
||||
|
||||
FROM build AS publish
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
RUN dotnet publish "./FictionArchive.Service.UserNovelDataService.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.UserNovelDataService.dll"]
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user