Compare commits

...

21 Commits

Author SHA1 Message Date
gamer147
f88f340d0a [FA-misc] Mass transit overhaul, needs testing and review 2026-01-21 23:16:31 -05:00
055ef33666 Merge pull request '[FA-24] Reading lists' (#60) from feature/FA-24_ReadingLists into master
All checks were successful
CI / build-backend (push) Successful in 1m4s
CI / build-frontend (push) Successful in 41s
Build Gateway / build-subgraphs (map[name:novel-service project:FictionArchive.Service.NovelService subgraph:Novel]) (push) Successful in 49s
Build Gateway / build-subgraphs (map[name:scheduler-service project:FictionArchive.Service.SchedulerService subgraph:Scheduler]) (push) Successful in 45s
Build Gateway / build-subgraphs (map[name:translation-service project:FictionArchive.Service.TranslationService subgraph:Translation]) (push) Successful in 46s
Build Gateway / build-subgraphs (map[name:user-service project:FictionArchive.Service.UserService subgraph:User]) (push) Successful in 43s
Build Gateway / build-subgraphs (map[name:usernoveldata-service project:FictionArchive.Service.UserNovelDataService subgraph:UserNovelData]) (push) Successful in 44s
Release / build-and-push (map[dockerfile:FictionArchive.Service.AuthenticationService/Dockerfile name:authentication-service]) (push) Successful in 2m3s
Release / build-and-push (map[dockerfile:FictionArchive.Service.FileService/Dockerfile name:file-service]) (push) Successful in 1m53s
Release / build-and-push (map[dockerfile:FictionArchive.Service.NovelService/Dockerfile name:novel-service]) (push) Successful in 1m43s
Release / build-and-push (map[dockerfile:FictionArchive.Service.SchedulerService/Dockerfile name:scheduler-service]) (push) Successful in 1m39s
Release / build-and-push (map[dockerfile:FictionArchive.Service.TranslationService/Dockerfile name:translation-service]) (push) Successful in 1m42s
Release / build-and-push (map[dockerfile:FictionArchive.Service.UserNovelDataService/Dockerfile name:usernoveldata-service]) (push) Successful in 1m38s
Release / build-and-push (map[dockerfile:FictionArchive.Service.UserService/Dockerfile name:user-service]) (push) Successful in 1m32s
Release / build-frontend (push) Successful in 1m38s
Build Gateway / build-gateway (push) Successful in 3m18s
Reviewed-on: #60
2026-01-20 03:09:45 +00:00
gamer147
48ee43c4f6 [FA-24] Reading lists
All checks were successful
CI / build-backend (pull_request) Successful in 1m32s
CI / build-frontend (pull_request) Successful in 42s
2026-01-19 22:06:34 -05:00
98ae4ea4f2 Merge pull request 'feature/FA-27_Bookmarks' (#59) from feature/FA-27_Bookmarks into master
All checks were successful
CI / build-backend (push) Successful in 1m16s
CI / build-frontend (push) Successful in 40s
Build Gateway / build-subgraphs (map[name:novel-service project:FictionArchive.Service.NovelService subgraph:Novel]) (push) Successful in 47s
Build Gateway / build-subgraphs (map[name:scheduler-service project:FictionArchive.Service.SchedulerService subgraph:Scheduler]) (push) Successful in 42s
Build Gateway / build-subgraphs (map[name:translation-service project:FictionArchive.Service.TranslationService subgraph:Translation]) (push) Successful in 45s
Build Gateway / build-subgraphs (map[name:user-service project:FictionArchive.Service.UserService subgraph:User]) (push) Successful in 43s
Build Gateway / build-subgraphs (map[name:usernoveldata-service project:FictionArchive.Service.UserNovelDataService subgraph:UserNovelData]) (push) Successful in 43s
Release / build-and-push (map[dockerfile:FictionArchive.Service.AuthenticationService/Dockerfile name:authentication-service]) (push) Successful in 2m19s
Release / build-and-push (map[dockerfile:FictionArchive.Service.FileService/Dockerfile name:file-service]) (push) Successful in 2m3s
Release / build-and-push (map[dockerfile:FictionArchive.Service.NovelService/Dockerfile name:novel-service]) (push) Successful in 1m41s
Release / build-and-push (map[dockerfile:FictionArchive.Service.SchedulerService/Dockerfile name:scheduler-service]) (push) Successful in 1m37s
Release / build-and-push (map[dockerfile:FictionArchive.Service.TranslationService/Dockerfile name:translation-service]) (push) Successful in 1m48s
Release / build-and-push (map[dockerfile:FictionArchive.Service.UserNovelDataService/Dockerfile name:usernoveldata-service]) (push) Successful in 1m34s
Release / build-and-push (map[dockerfile:FictionArchive.Service.UserService/Dockerfile name:user-service]) (push) Successful in 1m33s
Release / build-frontend (push) Successful in 1m39s
Build Gateway / build-gateway (push) Successful in 3m11s
Reviewed-on: #59
2026-01-19 22:28:03 +00:00
gamer147
15e1a84f55 [FA-27] Update CICD
All checks were successful
CI / build-backend (pull_request) Successful in 1m6s
CI / build-frontend (pull_request) Successful in 41s
2026-01-19 17:03:44 -05:00
gamer147
70d4ba201a [FA-27] Fix unit test based on changes
All checks were successful
CI / build-backend (pull_request) Successful in 1m10s
CI / build-frontend (pull_request) Successful in 43s
2026-01-19 16:47:55 -05:00
gamer147
b69bcd6bf4 [FA-27] Fix user adding not using correct id
Some checks failed
CI / build-backend (pull_request) Failing after 1m2s
CI / build-frontend (pull_request) Successful in 41s
2026-01-19 16:14:49 -05:00
gamer147
c97654631b [FA-27] Still need to test events 2026-01-19 15:40:21 -05:00
gamer147
1ecfd9cc99 [FA-27] Need to test events but seems to mostly work 2026-01-19 15:13:14 -05:00
gamer147
19ae4a8089 Add .worktrees/ to .gitignore 2026-01-19 01:36:10 -05:00
gamer147
f8a45ad891 [FA-27] Bookmark implementation 2026-01-19 00:01:16 -05:00
gamer147
f67c5c610c Merge branch 'refs/heads/master' into feature/FA-27_Bookmarks 2025-12-30 11:07:36 -05:00
b5d4694f12 Merge pull request '[FA-misc] Update docker-compose.yml' (#58) from feature/FA-misc_AddDockerComposeUserService into master
All checks were successful
CI / build-backend (push) Successful in 1m7s
CI / build-frontend (push) Successful in 40s
Build Gateway / build-subgraphs (map[name:novel-service project:FictionArchive.Service.NovelService subgraph:Novel]) (push) Successful in 55s
Build Gateway / build-subgraphs (map[name:scheduler-service project:FictionArchive.Service.SchedulerService subgraph:Scheduler]) (push) Successful in 49s
Build Gateway / build-subgraphs (map[name:translation-service project:FictionArchive.Service.TranslationService subgraph:Translation]) (push) Successful in 50s
Build Gateway / build-subgraphs (map[name:user-service project:FictionArchive.Service.UserService subgraph:User]) (push) Successful in 48s
Release / build-and-push (map[dockerfile:FictionArchive.Service.AuthenticationService/Dockerfile name:authentication-service]) (push) Successful in 2m25s
Release / build-and-push (map[dockerfile:FictionArchive.Service.FileService/Dockerfile name:file-service]) (push) Successful in 2m28s
Release / build-and-push (map[dockerfile:FictionArchive.Service.NovelService/Dockerfile name:novel-service]) (push) Successful in 2m14s
Release / build-and-push (map[dockerfile:FictionArchive.Service.SchedulerService/Dockerfile name:scheduler-service]) (push) Successful in 2m8s
Release / build-and-push (map[dockerfile:FictionArchive.Service.TranslationService/Dockerfile name:translation-service]) (push) Successful in 2m15s
Release / build-and-push (map[dockerfile:FictionArchive.Service.UserService/Dockerfile name:user-service]) (push) Successful in 1m43s
Release / build-frontend (push) Successful in 1m43s
Build Gateway / build-gateway (push) Successful in 4m1s
Reviewed-on: #58
2025-12-30 03:26:06 +00:00
gamer147
6d47153a42 [FA-misc] Update docker-compose.yml
All checks were successful
CI / build-backend (pull_request) Successful in 1m26s
CI / build-frontend (pull_request) Successful in 50s
2025-12-29 22:23:29 -05:00
dbbc2fd8dc Merge pull request 'feature/FA-6_AuthorsPosts' (#57) from feature/FA-6_AuthorsPosts into master
All checks were successful
CI / build-backend (push) Successful in 1m16s
CI / build-frontend (push) Successful in 51s
Reviewed-on: #57
2025-12-30 03:14:53 +00:00
gamer147
176c94297b [FA-6] Author's posts seem to work
All checks were successful
CI / build-backend (pull_request) Successful in 2m4s
CI / build-frontend (pull_request) Successful in 46s
2025-12-29 22:06:12 -05:00
gamer147
8b3faa8f6c [FA-6] Good spot 2025-12-29 21:40:44 -05:00
gamer147
d87bd81190 [FA-6] Volumes work probably? 2025-12-29 21:28:07 -05:00
gamer147
bee805c441 [FA-6] Need to test Novelpia import 2025-12-29 20:27:04 -05:00
gamer147
5013da69c2 [FA-27] UserNovelDataService bootstrapped, going to do author's posts first i think 2025-12-29 14:54:01 -05:00
d8e3ec7ec9 Merge pull request 'feature/FA-55_UserServiceSetup' (#56) from feature/FA-55_UserServiceSetup into master
Some checks failed
CI / build-backend (push) Successful in 1m9s
CI / build-frontend (push) Failing after 44s
Reviewed-on: #56
2025-12-29 19:38:43 +00:00
191 changed files with 17694 additions and 1044 deletions

View File

@@ -28,6 +28,9 @@ jobs:
- name: user-service - name: user-service
project: FictionArchive.Service.UserService project: FictionArchive.Service.UserService
subgraph: User subgraph: User
- name: usernoveldata-service
project: FictionArchive.Service.UserNovelDataService
subgraph: UserNovelData
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v4 uses: actions/checkout@v4
@@ -110,6 +113,12 @@ jobs:
name: user-service-subgraph name: user-service-subgraph
path: subgraphs/user 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 - name: Configure subgraph URLs for Docker
run: | run: |
for fsp in subgraphs/*/*.fsp; do for fsp in subgraphs/*/*.fsp; do

View File

@@ -27,6 +27,8 @@ jobs:
dockerfile: FictionArchive.Service.SchedulerService/Dockerfile dockerfile: FictionArchive.Service.SchedulerService/Dockerfile
- name: authentication-service - name: authentication-service
dockerfile: FictionArchive.Service.AuthenticationService/Dockerfile dockerfile: FictionArchive.Service.AuthenticationService/Dockerfile
- name: usernoveldata-service
dockerfile: FictionArchive.Service.UserNovelDataService/Dockerfile
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v4 uses: actions/checkout@v4

3
.gitignore vendored
View File

@@ -140,3 +140,6 @@ appsettings.Local.json
schema.graphql schema.graphql
*.fsp *.fsp
gateway.fgp gateway.fgp
# Git worktrees
.worktrees/

File diff suppressed because it is too large Load Diff

View File

@@ -1,7 +1,6 @@
using FictionArchive.Service.AuthenticationService.Models.Requests; using FictionArchive.Service.AuthenticationService.Models.Requests;
using FictionArchive.Service.AuthenticationService.Models.IntegrationEvents; using FictionArchive.Service.Shared.MassTransit.Contracts.Events;
using FictionArchive.Service.Shared.Services.EventBus; using MassTransit;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
namespace FictionArchive.Service.AuthenticationService.Controllers namespace FictionArchive.Service.AuthenticationService.Controllers
@@ -10,11 +9,11 @@ namespace FictionArchive.Service.AuthenticationService.Controllers
[ApiController] [ApiController]
public class AuthenticationWebhookController : ControllerBase public class AuthenticationWebhookController : ControllerBase
{ {
private readonly IEventBus _eventBus; private readonly IPublishEndpoint _publishEndpoint;
public AuthenticationWebhookController(IEventBus eventBus) public AuthenticationWebhookController(IPublishEndpoint publishEndpoint)
{ {
_eventBus = eventBus; _publishEndpoint = publishEndpoint;
} }
[HttpPost(nameof(UserRegistered))] [HttpPost(nameof(UserRegistered))]
@@ -28,7 +27,7 @@ namespace FictionArchive.Service.AuthenticationService.Controllers
EventUserUsername = payload.EventUserUsername EventUserUsername = payload.EventUserUsername
}; };
await _eventBus.Publish(authUserAddedEvent); await _publishEndpoint.Publish(authUserAddedEvent);
return Ok(); return Ok();
} }

View File

@@ -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; }
}

View File

@@ -1,5 +1,4 @@
using FictionArchive.Service.Shared; using FictionArchive.Service.Shared.MassTransit;
using FictionArchive.Service.Shared.Services.EventBus.Implementations;
namespace FictionArchive.Service.AuthenticationService; namespace FictionArchive.Service.AuthenticationService;
@@ -16,12 +15,9 @@ public class Program
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen();
#region Event Bus #region MassTransit
builder.Services.AddRabbitMQ(opt => builder.Services.AddFictionArchiveMassTransit(builder.Configuration);
{
builder.Configuration.GetSection("RabbitMQ").Bind(opt);
});
#endregion #endregion

View File

@@ -6,8 +6,10 @@
} }
}, },
"RabbitMQ": { "RabbitMQ": {
"ConnectionString": "amqp://localhost", "Host": "localhost",
"ClientIdentifier": "AuthenticationService" "VirtualHost": "/",
"Username": "guest",
"Password": "guest"
}, },
"AllowedHosts": "*" "AllowedHosts": "*"
} }

View File

@@ -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; }
}

View File

@@ -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
}

View File

@@ -2,10 +2,9 @@ using Amazon.Runtime;
using Amazon.S3; using Amazon.S3;
using FictionArchive.Common.Extensions; using FictionArchive.Common.Extensions;
using FictionArchive.Service.FileService.Models; using FictionArchive.Service.FileService.Models;
using FictionArchive.Service.FileService.Models.IntegrationEvents;
using FictionArchive.Service.FileService.Services.EventHandlers; using FictionArchive.Service.FileService.Services.EventHandlers;
using FictionArchive.Service.Shared.Extensions; using FictionArchive.Service.Shared.Extensions;
using FictionArchive.Service.Shared.Services.EventBus.Implementations; using FictionArchive.Service.Shared.MassTransit;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
namespace FictionArchive.Service.FileService; namespace FictionArchive.Service.FileService;
@@ -24,13 +23,14 @@ public class Program
builder.Services.AddHealthChecks(); builder.Services.AddHealthChecks();
#region Event Bus #region MassTransit
builder.Services.AddRabbitMQ(opt => builder.Services.AddFictionArchiveMassTransit(
builder.Configuration,
x =>
{ {
builder.Configuration.GetSection("RabbitMQ").Bind(opt); x.AddConsumer<UploadFileCommandConsumer>();
}) });
.Subscribe<FileUploadRequestCreatedEvent, FileUploadRequestCreatedEventHandler>();
#endregion #endregion

View File

@@ -2,57 +2,62 @@ using Amazon.S3;
using Amazon.S3.Model; using Amazon.S3.Model;
using FictionArchive.Common.Enums; using FictionArchive.Common.Enums;
using FictionArchive.Service.FileService.Models; using FictionArchive.Service.FileService.Models;
using FictionArchive.Service.FileService.Models.IntegrationEvents; using FictionArchive.Service.Shared.MassTransit.Contracts.Commands;
using FictionArchive.Service.Shared.Services.EventBus; using FictionArchive.Service.Shared.MassTransit.Contracts.Events;
using MassTransit;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
namespace FictionArchive.Service.FileService.Services.EventHandlers; namespace FictionArchive.Service.FileService.Services.EventHandlers;
public class FileUploadRequestCreatedEventHandler : IIntegrationEventHandler<FileUploadRequestCreatedEvent> public class UploadFileCommandConsumer : IConsumer<UploadFileCommand>
{ {
private readonly ILogger<FileUploadRequestCreatedEventHandler> _logger; private readonly ILogger<UploadFileCommandConsumer> _logger;
private readonly AmazonS3Client _amazonS3Client; private readonly AmazonS3Client _amazonS3Client;
private readonly IEventBus _eventBus;
private readonly S3Configuration _s3Configuration; private readonly S3Configuration _s3Configuration;
private readonly ProxyConfiguration _proxyConfiguration; private readonly ProxyConfiguration _proxyConfiguration;
public FileUploadRequestCreatedEventHandler(ILogger<FileUploadRequestCreatedEventHandler> logger, AmazonS3Client amazonS3Client, IEventBus eventBus, IOptions<S3Configuration> s3Configuration, IOptions<ProxyConfiguration> proxyConfiguration) public UploadFileCommandConsumer(
ILogger<UploadFileCommandConsumer> logger,
AmazonS3Client amazonS3Client,
IOptions<S3Configuration> s3Configuration,
IOptions<ProxyConfiguration> proxyConfiguration)
{ {
_logger = logger; _logger = logger;
_amazonS3Client = amazonS3Client; _amazonS3Client = amazonS3Client;
_eventBus = eventBus;
_proxyConfiguration = proxyConfiguration.Value; _proxyConfiguration = proxyConfiguration.Value;
_s3Configuration = s3Configuration.Value; _s3Configuration = s3Configuration.Value;
} }
public async Task Handle(FileUploadRequestCreatedEvent @event) public async Task Consume(ConsumeContext<UploadFileCommand> context)
{ {
var command = context.Message;
var putObjectRequest = new PutObjectRequest(); var putObjectRequest = new PutObjectRequest();
putObjectRequest.BucketName = _s3Configuration.Bucket; putObjectRequest.BucketName = _s3Configuration.Bucket;
putObjectRequest.Key = @event.FilePath; putObjectRequest.Key = command.FilePath;
putObjectRequest.UseChunkEncoding = false; // Needed to avoid an error with Garage putObjectRequest.UseChunkEncoding = false; // Needed to avoid an error with Garage
using MemoryStream memoryStream = new MemoryStream(@event.FileData); using MemoryStream memoryStream = new MemoryStream(command.FileData);
putObjectRequest.InputStream = memoryStream; putObjectRequest.InputStream = memoryStream;
var s3Response = await _amazonS3Client.PutObjectAsync(putObjectRequest); var s3Response = await _amazonS3Client.PutObjectAsync(putObjectRequest);
if (s3Response.HttpStatusCode != System.Net.HttpStatusCode.OK) if (s3Response.HttpStatusCode != System.Net.HttpStatusCode.OK)
{ {
_logger.LogError("An error occurred while uploading file to S3. Response code: {responsecode}", s3Response.HttpStatusCode); _logger.LogError("An error occurred while uploading file to S3. Response code: {responsecode}", s3Response.HttpStatusCode);
await _eventBus.Publish(new FileUploadRequestStatusUpdateEvent() await context.Publish(new FileUploadCompletedEvent
{ {
RequestId = @event.RequestId, RequestId = command.RequestId,
Status = RequestStatus.Failed, Status = RequestStatus.Failed,
ErrorMessage = "An error occurred while uploading file to S3." ErrorMessage = "An error occurred while uploading file to S3."
}); });
return; return;
} }
await _eventBus.Publish(new FileUploadRequestStatusUpdateEvent() await context.Publish(new FileUploadCompletedEvent
{ {
Status = RequestStatus.Success, Status = RequestStatus.Success,
RequestId = @event.RequestId, RequestId = command.RequestId,
FileAccessUrl = _proxyConfiguration.BaseUrl + "/" + @event.FilePath FileAccessUrl = _proxyConfiguration.BaseUrl + "/" + command.FilePath
}); });
} }
} }

View File

@@ -9,8 +9,10 @@
"BaseUrl": "https://localhost:7247/api" "BaseUrl": "https://localhost:7247/api"
}, },
"RabbitMQ": { "RabbitMQ": {
"ConnectionString": "amqp://localhost", "Host": "localhost",
"ClientIdentifier": "FileService" "VirtualHost": "/",
"Username": "guest",
"Password": "guest"
}, },
"S3": { "S3": {
"Url": "https://s3.orfl.xyz", "Url": "https://s3.orfl.xyz",

View File

@@ -1,5 +1,4 @@
using FictionArchive.Common.Enums; using FictionArchive.Common.Enums;
using FictionArchive.Service.FileService.IntegrationEvents;
using FictionArchive.Service.NovelService.Models.Configuration; using FictionArchive.Service.NovelService.Models.Configuration;
using FictionArchive.Service.NovelService.Models.Enums; using FictionArchive.Service.NovelService.Models.Enums;
using FictionArchive.Service.NovelService.Models.Images; using FictionArchive.Service.NovelService.Models.Images;
@@ -8,8 +7,9 @@ using FictionArchive.Service.NovelService.Models.Novels;
using FictionArchive.Service.NovelService.Models.SourceAdapters; using FictionArchive.Service.NovelService.Models.SourceAdapters;
using FictionArchive.Service.NovelService.Services; using FictionArchive.Service.NovelService.Services;
using FictionArchive.Service.NovelService.Services.SourceAdapters; using FictionArchive.Service.NovelService.Services.SourceAdapters;
using FictionArchive.Service.Shared.Services.EventBus; using FictionArchive.Service.Shared.MassTransit.Contracts.Commands;
using FluentAssertions; using FluentAssertions;
using MassTransit;
using HtmlAgilityPack; using HtmlAgilityPack;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
@@ -42,6 +42,13 @@ public class NovelUpdateServiceTests
Images = new List<Image>() 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 var novel = new Novel
{ {
Url = "http://demo/novel", Url = "http://demo/novel",
@@ -52,20 +59,21 @@ public class NovelUpdateServiceTests
Source = source, Source = source,
Name = LocalizationKey.CreateFromText("Demo Novel", Language.En), Name = LocalizationKey.CreateFromText("Demo Novel", Language.En),
Description = LocalizationKey.CreateFromText("Description", Language.En), Description = LocalizationKey.CreateFromText("Description", Language.En),
Chapters = new List<Chapter> { chapter }, Volumes = new List<Volume> { volume },
Tags = new List<NovelTag>() Tags = new List<NovelTag>()
}; };
dbContext.Novels.Add(novel); dbContext.Novels.Add(novel);
dbContext.SaveChanges(); dbContext.SaveChanges();
return new NovelCreateResult(novel, chapter); return new NovelCreateResult(novel, volume, chapter);
} }
private static NovelUpdateService CreateService( private static NovelUpdateService CreateService(
NovelServiceDbContext dbContext, NovelServiceDbContext dbContext,
ISourceAdapter adapter, ISourceAdapter adapter,
IEventBus eventBus, IPublishEndpoint publishEndpoint,
ISendEndpointProvider sendEndpointProvider,
string pendingImageUrl = "https://pending/placeholder.jpg") string pendingImageUrl = "https://pending/placeholder.jpg")
{ {
var options = Options.Create(new NovelUpdateServiceConfiguration var options = Options.Create(new NovelUpdateServiceConfiguration
@@ -73,7 +81,7 @@ public class NovelUpdateServiceTests
PendingImageUrl = pendingImageUrl PendingImageUrl = pendingImageUrl
}); });
return new NovelUpdateService(dbContext, NullLogger<NovelUpdateService>.Instance, new[] { adapter }, eventBus, options); return new NovelUpdateService(dbContext, NullLogger<NovelUpdateService>.Instance, new[] { adapter }, publishEndpoint, sendEndpointProvider, options);
} }
[Fact] [Fact]
@@ -81,7 +89,7 @@ public class NovelUpdateServiceTests
{ {
using var dbContext = CreateDbContext(); using var dbContext = CreateDbContext();
var source = new Source { Name = "Demo", Key = "demo", Url = "http://demo" }; 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 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 } }; var image1 = new ImageData { Url = "http://img/x1.jpg", Data = new byte[] { 1, 2, 3 } };
@@ -95,15 +103,17 @@ public class NovelUpdateServiceTests
ImageData = new List<ImageData> { image1, image2 } ImageData = new List<ImageData> { image1, image2 }
})); }));
var publishedEvents = new List<FileUploadRequestCreatedEvent>(); var publishedCommands = new List<UploadFileCommand>();
var eventBus = Substitute.For<IEventBus>(); var publishEndpoint = Substitute.For<IPublishEndpoint>();
eventBus.Publish(Arg.Do<FileUploadRequestCreatedEvent>(publishedEvents.Add)).Returns(Task.CompletedTask); var sendEndpointProvider = Substitute.For<ISendEndpointProvider>();
eventBus.Publish(Arg.Any<object>(), Arg.Any<string>()).Returns(Task.CompletedTask); var sendEndpoint = Substitute.For<ISendEndpoint>();
sendEndpointProvider.GetSendEndpoint(Arg.Any<Uri>()).Returns(Task.FromResult(sendEndpoint));
sendEndpoint.Send(Arg.Do<UploadFileCommand>(publishedCommands.Add), Arg.Any<CancellationToken>()).Returns(Task.CompletedTask);
var pendingImageUrl = "https://pending/placeholder.jpg"; var pendingImageUrl = "https://pending/placeholder.jpg";
var service = CreateService(dbContext, adapter, eventBus, pendingImageUrl); var service = CreateService(dbContext, adapter, publishEndpoint, sendEndpointProvider, 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.Should().HaveCount(2);
updatedChapter.Images.Select(i => i.OriginalPath).Should().BeEquivalentTo(new[] { image1.Url, image2.Url }); updatedChapter.Images.Select(i => i.OriginalPath).Should().BeEquivalentTo(new[] { image1.Url, image2.Url });
@@ -120,10 +130,10 @@ public class NovelUpdateServiceTests
.Should() .Should()
.BeEquivalentTo(updatedChapter.Images.Select(img => img.Id.ToString())); .BeEquivalentTo(updatedChapter.Images.Select(img => img.Id.ToString()));
publishedEvents.Should().HaveCount(2); publishedCommands.Should().HaveCount(2);
publishedEvents.Select(e => e.RequestId).Should().BeEquivalentTo(updatedChapter.Images.Select(i => i.Id)); publishedCommands.Select(e => e.RequestId).Should().BeEquivalentTo(updatedChapter.Images.Select(i => i.Id));
publishedEvents.Select(e => e.FileData).Should().BeEquivalentTo(new[] { image1.Data, image2.Data }); publishedCommands.Select(e => e.FileData).Should().BeEquivalentTo(new[] { image1.Data, image2.Data });
publishedEvents.Should().OnlyContain(e => e.FilePath.StartsWith($"{novel.Id}/Images/Chapter-{updatedChapter.Id}/")); publishedCommands.Should().OnlyContain(e => e.FilePath.StartsWith($"{novel.Id}/Images/Chapter-{updatedChapter.Id}/"));
} }
[Fact] [Fact]
@@ -131,7 +141,7 @@ public class NovelUpdateServiceTests
{ {
using var dbContext = CreateDbContext(); using var dbContext = CreateDbContext();
var source = new Source { Name = "Demo", Key = "demo", Url = "http://demo" }; 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 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 } }; var image = new ImageData { Url = "http://img/x1.jpg", Data = new byte[] { 7, 8, 9 } };
@@ -144,13 +154,14 @@ public class NovelUpdateServiceTests
ImageData = new List<ImageData> { image } ImageData = new List<ImageData> { image }
})); }));
var eventBus = Substitute.For<IEventBus>(); var publishEndpoint = Substitute.For<IPublishEndpoint>();
eventBus.Publish(Arg.Any<FileUploadRequestCreatedEvent>()).Returns(Task.CompletedTask); var sendEndpointProvider = Substitute.For<ISendEndpointProvider>();
eventBus.Publish(Arg.Any<object>(), Arg.Any<string>()).Returns(Task.CompletedTask); var sendEndpoint = Substitute.For<ISendEndpoint>();
sendEndpointProvider.GetSendEndpoint(Arg.Any<Uri>()).Returns(Task.FromResult(sendEndpoint));
var service = CreateService(dbContext, adapter, eventBus); var service = CreateService(dbContext, adapter, publishEndpoint, sendEndpointProvider);
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 storedHtml = updatedChapter.Body.Texts.Single().Text;
var doc = new HtmlDocument(); var doc = new HtmlDocument();
@@ -161,7 +172,7 @@ public class NovelUpdateServiceTests
imgNode.GetAttributeValue("src", string.Empty).Should().Be("https://pending/placeholder.jpg"); 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 #region UpdateImage Tests
@@ -179,8 +190,9 @@ public class NovelUpdateServiceTests
await dbContext.SaveChangesAsync(); await dbContext.SaveChangesAsync();
var adapter = Substitute.For<ISourceAdapter>(); var adapter = Substitute.For<ISourceAdapter>();
var eventBus = Substitute.For<IEventBus>(); var publishEndpoint = Substitute.For<IPublishEndpoint>();
var service = CreateService(dbContext, adapter, eventBus); var sendEndpointProvider = Substitute.For<ISendEndpointProvider>();
var service = CreateService(dbContext, adapter, publishEndpoint, sendEndpointProvider);
var newUrl = "https://cdn.example.com/uploaded/cover.jpg"; var newUrl = "https://cdn.example.com/uploaded/cover.jpg";
@@ -199,7 +211,7 @@ public class NovelUpdateServiceTests
// Arrange // Arrange
using var dbContext = CreateDbContext(); using var dbContext = CreateDbContext();
var source = new Source { Name = "Demo", Key = "demo", Url = "http://demo" }; 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 var image = new Image
{ {
@@ -221,8 +233,9 @@ public class NovelUpdateServiceTests
await dbContext.SaveChangesAsync(); await dbContext.SaveChangesAsync();
var adapter = Substitute.For<ISourceAdapter>(); var adapter = Substitute.For<ISourceAdapter>();
var eventBus = Substitute.For<IEventBus>(); var publishEndpoint = Substitute.For<IPublishEndpoint>();
var service = CreateService(dbContext, adapter, eventBus, pendingUrl); var sendEndpointProvider = Substitute.For<ISendEndpointProvider>();
var service = CreateService(dbContext, adapter, publishEndpoint, sendEndpointProvider, pendingUrl);
var newUrl = "https://cdn.example.com/uploaded/image.jpg"; var newUrl = "https://cdn.example.com/uploaded/image.jpg";
@@ -252,7 +265,7 @@ public class NovelUpdateServiceTests
// Arrange // Arrange
using var dbContext = CreateDbContext(); using var dbContext = CreateDbContext();
var source = new Source { Name = "Demo", Key = "demo", Url = "http://demo" }; 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 image1 = new Image { OriginalPath = "http://original/img1.jpg", Chapter = chapter };
var image2 = new Image { OriginalPath = "http://original/img2.jpg", Chapter = chapter }; var image2 = new Image { OriginalPath = "http://original/img2.jpg", Chapter = chapter };
@@ -270,8 +283,9 @@ public class NovelUpdateServiceTests
await dbContext.SaveChangesAsync(); await dbContext.SaveChangesAsync();
var adapter = Substitute.For<ISourceAdapter>(); var adapter = Substitute.For<ISourceAdapter>();
var eventBus = Substitute.For<IEventBus>(); var publishEndpoint = Substitute.For<IPublishEndpoint>();
var service = CreateService(dbContext, adapter, eventBus, pendingUrl); var sendEndpointProvider = Substitute.For<ISendEndpointProvider>();
var service = CreateService(dbContext, adapter, publishEndpoint, sendEndpointProvider, pendingUrl);
var newUrl = "https://cdn.example.com/uploaded/img1.jpg"; var newUrl = "https://cdn.example.com/uploaded/img1.jpg";

View File

@@ -1,31 +1,26 @@
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;
using FictionArchive.Service.NovelService.Services.SourceAdapters; using FictionArchive.Service.Shared.MassTransit.Contracts.Commands;
using FictionArchive.Service.Shared.Services.EventBus;
using HotChocolate.Authorization; using HotChocolate.Authorization;
using HotChocolate.Types; using HotChocolate.Types;
using Microsoft.EntityFrameworkCore;
namespace FictionArchive.Service.NovelService.GraphQL; namespace FictionArchive.Service.NovelService.GraphQL;
public class Mutation public class Mutation
{ {
[Authorize] [Authorize]
public async Task<NovelUpdateRequestedEvent> ImportNovel(string novelUrl, NovelUpdateService service) public async Task<ImportNovelCommand> ImportNovel(string novelUrl, NovelUpdateService service)
{ {
return await service.QueueNovelImport(novelUrl); return await service.QueueNovelImport(novelUrl);
} }
[Authorize] [Authorize]
public async Task<ChapterPullRequestedEvent> FetchChapterContents(uint novelId, public async Task<PullChapterContentCommand> FetchChapterContents(
uint chapterNumber, uint novelId,
uint volumeId,
uint chapterOrder,
NovelUpdateService service) NovelUpdateService service)
{ {
return await service.QueueChapterPull(novelId, chapterNumber); return await service.QueueChapterPull(novelId, volumeId, chapterOrder);
} }
[Error<KeyNotFoundException>] [Error<KeyNotFoundException>]

View File

@@ -77,7 +77,19 @@ public class Query
} }
: null, : null,
Chapters = novel.Chapters.Select(chapter => new ChapterDto Volumes = novel.Volumes.OrderBy(v => v.Order).Select(volume => new VolumeDto
{
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()
?? volume.Name.Texts.Select(t => t.Text).FirstOrDefault()
?? "",
Chapters = volume.Chapters.OrderBy(c => c.Order).Select(chapter => new ChapterDto
{ {
Id = chapter.Id, Id = chapter.Id,
CreatedTime = chapter.CreatedTime, CreatedTime = chapter.CreatedTime,
@@ -104,6 +116,7 @@ public class Query
LastUpdatedTime = image.LastUpdatedTime, LastUpdatedTime = image.LastUpdatedTime,
NewPath = image.NewPath NewPath = image.NewPath
}).ToList() }).ToList()
}).ToList()
}).ToList(), }).ToList(),
Tags = novel.Tags.Select(tag => new NovelTagDto Tags = novel.Tags.Select(tag => new NovelTagDto
@@ -140,11 +153,12 @@ public class Query
public IQueryable<ChapterReaderDto> GetChapter( public IQueryable<ChapterReaderDto> GetChapter(
NovelServiceDbContext dbContext, NovelServiceDbContext dbContext,
uint novelId, uint novelId,
uint volumeOrder,
uint chapterOrder, uint chapterOrder,
Language preferredLanguage = Language.En) Language preferredLanguage = Language.En)
{ {
return dbContext.Chapters 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 .Select(chapter => new ChapterReaderDto
{ {
Id = chapter.Id, Id = chapter.Id,
@@ -176,22 +190,72 @@ public class Query
NewPath = image.NewPath NewPath = image.NewPath
}).ToList(), }).ToList(),
NovelId = chapter.Novel.Id, NovelId = chapter.Volume.Novel.Id,
NovelName = chapter.Novel.Name.Texts NovelName = chapter.Volume.Novel.Name.Texts
.Where(t => t.Language == preferredLanguage) .Where(t => t.Language == preferredLanguage)
.Select(t => t.Text) .Select(t => t.Text)
.FirstOrDefault() .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) .Where(c => c.Order < chapterOrder)
.OrderByDescending(c => c.Order) .OrderByDescending(c => c.Order)
.Select(c => (uint?)c.Order) .Select(c => (uint?)c.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 => (uint?)c.Order)
.FirstOrDefault(), .FirstOrDefault(),
NextChapterOrder = chapter.Novel.Chapters
// Next chapter: first try same volume, then first chapter of next volume
NextChapterVolumeOrder = chapter.Volume.Chapters
.Where(c => c.Order > chapterOrder) .Where(c => c.Order > chapterOrder)
.OrderBy(c => c.Order) .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) .Select(c => (uint?)c.Order)
.FirstOrDefault() .FirstOrDefault()
}); });

View 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
}
}
}

View File

@@ -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");
}
}
}

View File

@@ -153,9 +153,6 @@ namespace FictionArchive.Service.NovelService.Migrations
b.Property<Guid>("NameId") b.Property<Guid>("NameId")
.HasColumnType("uuid"); .HasColumnType("uuid");
b.Property<long>("NovelId")
.HasColumnType("bigint");
b.Property<long>("Order") b.Property<long>("Order")
.HasColumnType("bigint"); .HasColumnType("bigint");
@@ -165,13 +162,17 @@ namespace FictionArchive.Service.NovelService.Migrations
b.Property<string>("Url") b.Property<string>("Url")
.HasColumnType("text"); .HasColumnType("text");
b.Property<long>("VolumeId")
.HasColumnType("bigint");
b.HasKey("Id"); b.HasKey("Id");
b.HasIndex("BodyId"); b.HasIndex("BodyId");
b.HasIndex("NameId"); b.HasIndex("NameId");
b.HasIndex("NovelId"); b.HasIndex("VolumeId", "Order")
.IsUnique();
b.ToTable("Chapter"); b.ToTable("Chapter");
}); });
@@ -357,6 +358,39 @@ namespace FictionArchive.Service.NovelService.Migrations
b.ToTable("TranslationEngines"); 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 => modelBuilder.Entity("NovelNovelTag", b =>
{ {
b.Property<long>("NovelsId") b.Property<long>("NovelsId")
@@ -427,9 +461,9 @@ namespace FictionArchive.Service.NovelService.Migrations
.OnDelete(DeleteBehavior.Cascade) .OnDelete(DeleteBehavior.Cascade)
.IsRequired(); .IsRequired();
b.HasOne("FictionArchive.Service.NovelService.Models.Novels.Novel", "Novel") b.HasOne("FictionArchive.Service.NovelService.Models.Novels.Volume", "Volume")
.WithMany("Chapters") .WithMany("Chapters")
.HasForeignKey("NovelId") .HasForeignKey("VolumeId")
.OnDelete(DeleteBehavior.Cascade) .OnDelete(DeleteBehavior.Cascade)
.IsRequired(); .IsRequired();
@@ -437,7 +471,7 @@ namespace FictionArchive.Service.NovelService.Migrations
b.Navigation("Name"); b.Navigation("Name");
b.Navigation("Novel"); b.Navigation("Volume");
}); });
modelBuilder.Entity("FictionArchive.Service.NovelService.Models.Novels.Novel", b => modelBuilder.Entity("FictionArchive.Service.NovelService.Models.Novels.Novel", b =>
@@ -509,6 +543,25 @@ namespace FictionArchive.Service.NovelService.Migrations
b.Navigation("Name"); 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 => modelBuilder.Entity("NovelNovelTag", b =>
{ {
b.HasOne("FictionArchive.Service.NovelService.Models.Novels.Novel", null) 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 => modelBuilder.Entity("FictionArchive.Service.NovelService.Models.Novels.Novel", b =>
{
b.Navigation("Volumes");
});
modelBuilder.Entity("FictionArchive.Service.NovelService.Models.Novels.Volume", b =>
{ {
b.Navigation("Chapters"); b.Navigation("Chapters");
}); });

View File

@@ -12,7 +12,16 @@ public class ChapterReaderDto : BaseDto<uint>
// Navigation context // Navigation context
public uint NovelId { get; init; } public uint NovelId { get; init; }
public required string NovelName { 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 uint? PrevChapterOrder { get; init; }
public int? NextChapterVolumeOrder { get; init; }
public uint? NextChapterOrder { get; init; } public uint? NextChapterOrder { get; init; }
} }

View File

@@ -14,7 +14,7 @@ public class NovelDto : BaseDto<uint>
public required string ExternalId { get; init; } public required string ExternalId { get; init; }
public required string Name { get; init; } public required string Name { get; init; }
public required string Description { 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 required List<NovelTagDto> Tags { get; init; }
public ImageDto? CoverImage { get; init; } public ImageDto? CoverImage { get; init; }
} }

View File

@@ -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; }
}

View File

@@ -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; }
}

View File

@@ -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; }
}

View File

@@ -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
}

View File

@@ -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; }
}

View File

@@ -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; }
}

View File

@@ -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; }
}

View File

@@ -20,7 +20,7 @@ public class Chapter : BaseEntity<uint>
#region Navigation Properties #region Navigation Properties
public Novel Novel { get; set; } public Volume Volume { get; set; }
#endregion #endregion
} }

View File

@@ -21,7 +21,7 @@ public class Novel : BaseEntity<uint>
public LocalizationKey Name { get; set; } public LocalizationKey Name { get; set; }
public LocalizationKey Description { 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 List<NovelTag> Tags { get; set; }
public Image? CoverImage { get; set; } public Image? CoverImage { get; set; }
} }

View 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
}

View File

@@ -16,7 +16,7 @@ public class NovelMetadata
public Language RawLanguage { get; set; } public Language RawLanguage { get; set; }
public NovelStatus RawStatus { 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> SourceTags { get; set; }
public List<string> SystemTags { get; set; } public List<string> SystemTags { get; set; }
public SourceDescriptor SourceDescriptor { get; set; } public SourceDescriptor SourceDescriptor { get; set; }

View File

@@ -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; }
}

View File

@@ -1,14 +1,13 @@
using FictionArchive.Common.Extensions; using FictionArchive.Common.Extensions;
using FictionArchive.Service.NovelService.GraphQL; using FictionArchive.Service.NovelService.GraphQL;
using FictionArchive.Service.NovelService.Models.Configuration; using FictionArchive.Service.NovelService.Models.Configuration;
using FictionArchive.Service.NovelService.Models.IntegrationEvents;
using FictionArchive.Service.NovelService.Services; using FictionArchive.Service.NovelService.Services;
using FictionArchive.Service.NovelService.Services.EventHandlers; using FictionArchive.Service.NovelService.Services.Consumers;
using FictionArchive.Service.NovelService.Services.SourceAdapters; using FictionArchive.Service.NovelService.Services.SourceAdapters;
using FictionArchive.Service.NovelService.Services.SourceAdapters.Novelpia; using FictionArchive.Service.NovelService.Services.SourceAdapters.Novelpia;
using FictionArchive.Service.Shared; using FictionArchive.Service.Shared;
using FictionArchive.Service.Shared.Extensions; using FictionArchive.Service.Shared.Extensions;
using FictionArchive.Service.Shared.Services.EventBus.Implementations; using FictionArchive.Service.Shared.MassTransit;
using FictionArchive.Service.Shared.Services.GraphQL; using FictionArchive.Service.Shared.Services.GraphQL;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@@ -25,18 +24,19 @@ public class Program
builder.Services.AddMemoryCache(); builder.Services.AddMemoryCache();
#region Event Bus #region MassTransit
if (!isSchemaExport) if (!isSchemaExport)
{ {
builder.Services.AddRabbitMQ(opt => builder.Services.AddFictionArchiveMassTransit<NovelServiceDbContext>(
builder.Configuration,
cfg =>
{ {
builder.Configuration.GetSection("RabbitMQ").Bind(opt); cfg.AddConsumer<ImportNovelCommandConsumer>();
}) cfg.AddConsumer<PullChapterContentCommandConsumer>();
.Subscribe<TranslationRequestCompletedEvent, TranslationRequestCompletedEventHandler>() cfg.AddConsumer<TranslationCompletedEventConsumer>();
.Subscribe<NovelUpdateRequestedEvent, NovelUpdateRequestedEventHandler>() cfg.AddConsumer<FileUploadCompletedEventConsumer>();
.Subscribe<ChapterPullRequestedEvent, ChapterPullRequestedEventHandler>() });
.Subscribe<FileUploadRequestStatusUpdateEvent, FileUploadRequestStatusUpdateEventHandler>();
} }
#endregion #endregion

View File

@@ -0,0 +1,53 @@
using FictionArchive.Common.Enums;
using FictionArchive.Service.Shared.MassTransit.Contracts.Events;
using MassTransit;
namespace FictionArchive.Service.NovelService.Services.Consumers;
public class FileUploadCompletedEventConsumer : IConsumer<FileUploadCompletedEvent>
{
private readonly ILogger<FileUploadCompletedEventConsumer> _logger;
private readonly NovelServiceDbContext _dbContext;
private readonly NovelUpdateService _novelUpdateService;
public FileUploadCompletedEventConsumer(
ILogger<FileUploadCompletedEventConsumer> logger,
NovelServiceDbContext dbContext,
NovelUpdateService novelUpdateService)
{
_logger = logger;
_dbContext = dbContext;
_novelUpdateService = novelUpdateService;
}
public async Task Consume(ConsumeContext<FileUploadCompletedEvent> context)
{
var @event = context.Message;
var image = await _dbContext.Images.FindAsync(@event.RequestId);
if (image == null)
{
// Not a request we care about.
_logger.LogDebug(
"FileUploadCompletedEvent received for unknown image: {RequestId}",
@event.RequestId);
return;
}
if (@event.Status == RequestStatus.Failed)
{
_logger.LogError(
"Image upload failed for image with id {ImageId}: {ErrorMessage}",
image.Id, @event.ErrorMessage);
return;
}
if (@event.Status == RequestStatus.Success)
{
_logger.LogInformation(
"Image upload succeeded for image with id {ImageId}",
image.Id);
await _novelUpdateService.UpdateImage(image.Id, @event.FileAccessUrl!);
}
}
}

View File

@@ -0,0 +1,26 @@
using FictionArchive.Service.Shared.MassTransit.Contracts.Commands;
using MassTransit;
namespace FictionArchive.Service.NovelService.Services.Consumers;
public class ImportNovelCommandConsumer : IConsumer<ImportNovelCommand>
{
private readonly ILogger<ImportNovelCommandConsumer> _logger;
private readonly NovelUpdateService _novelUpdateService;
public ImportNovelCommandConsumer(
ILogger<ImportNovelCommandConsumer> logger,
NovelUpdateService novelUpdateService)
{
_logger = logger;
_novelUpdateService = novelUpdateService;
}
public async Task Consume(ConsumeContext<ImportNovelCommand> context)
{
var command = context.Message;
_logger.LogInformation("Processing ImportNovelCommand for URL: {NovelUrl}", command.NovelUrl);
await _novelUpdateService.ImportNovel(command.NovelUrl);
}
}

View File

@@ -0,0 +1,28 @@
using FictionArchive.Service.Shared.MassTransit.Contracts.Commands;
using MassTransit;
namespace FictionArchive.Service.NovelService.Services.Consumers;
public class PullChapterContentCommandConsumer : IConsumer<PullChapterContentCommand>
{
private readonly ILogger<PullChapterContentCommandConsumer> _logger;
private readonly NovelUpdateService _novelUpdateService;
public PullChapterContentCommandConsumer(
ILogger<PullChapterContentCommandConsumer> logger,
NovelUpdateService novelUpdateService)
{
_logger = logger;
_novelUpdateService = novelUpdateService;
}
public async Task Consume(ConsumeContext<PullChapterContentCommand> context)
{
var command = context.Message;
_logger.LogInformation(
"Processing PullChapterContentCommand for Novel: {NovelId}, Volume: {VolumeId}, Chapter: {ChapterOrder}",
command.NovelId, command.VolumeId, command.ChapterOrder);
await _novelUpdateService.PullChapterContents(command.NovelId, command.VolumeId, command.ChapterOrder);
}
}

View File

@@ -0,0 +1,52 @@
using FictionArchive.Service.NovelService.Models.Localization;
using FictionArchive.Service.Shared.MassTransit.Contracts.Events;
using MassTransit;
using Microsoft.EntityFrameworkCore;
namespace FictionArchive.Service.NovelService.Services.Consumers;
public class TranslationCompletedEventConsumer : IConsumer<TranslationCompletedEvent>
{
private readonly ILogger<TranslationCompletedEventConsumer> _logger;
private readonly NovelServiceDbContext _dbContext;
public TranslationCompletedEventConsumer(
ILogger<TranslationCompletedEventConsumer> logger,
NovelServiceDbContext dbContext)
{
_logger = logger;
_dbContext = dbContext;
}
public async Task Consume(ConsumeContext<TranslationCompletedEvent> context)
{
var @event = context.Message;
var localizationRequest = await _dbContext.LocalizationRequests
.Include(r => r.KeyRequestedForTranslation)
.ThenInclude(lk => lk.Texts)
.FirstOrDefaultAsync(lk => lk.Id == @event.TranslationRequestId);
if (localizationRequest == null)
{
// Not one of our requests, discard it
_logger.LogDebug(
"TranslationCompletedEvent received for unknown request: {RequestId}",
@event.TranslationRequestId);
return;
}
localizationRequest.KeyRequestedForTranslation.Texts.Add(new LocalizationText()
{
Language = localizationRequest.TranslateTo,
Text = @event.TranslatedText,
TranslationEngine = localizationRequest.Engine
});
_dbContext.LocalizationRequests.Remove(localizationRequest);
await _dbContext.SaveChangesAsync();
_logger.LogInformation(
"Completed translation for request: {RequestId}",
@event.TranslationRequestId);
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}
}

View File

@@ -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);
}
}

View File

@@ -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();
}
}

View File

@@ -10,6 +10,7 @@ public class NovelServiceDbContext(DbContextOptions options, ILogger<NovelServic
: FictionArchiveDbContext(options, logger) : FictionArchiveDbContext(options, logger)
{ {
public DbSet<Novel> Novels { get; set; } public DbSet<Novel> Novels { get; set; }
public DbSet<Volume> Volumes { get; set; }
public DbSet<Chapter> Chapters { get; set; } public DbSet<Chapter> Chapters { get; set; }
public DbSet<Source> Sources { get; set; } public DbSet<Source> Sources { get; set; }
public DbSet<TranslationEngine> TranslationEngines { get; set; } public DbSet<TranslationEngine> TranslationEngines { get; set; }
@@ -25,5 +26,15 @@ public class NovelServiceDbContext(DbContextOptions options, ILogger<NovelServic
modelBuilder.Entity<Novel>() modelBuilder.Entity<Novel>()
.HasIndex("ExternalId", "SourceId") .HasIndex("ExternalId", "SourceId")
.IsUnique(); .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();
} }
} }

View File

@@ -1,15 +1,15 @@
using FictionArchive.Common.Enums; using FictionArchive.Common.Enums;
using FictionArchive.Service.FileService.IntegrationEvents;
using FictionArchive.Service.NovelService.Models.Configuration; using FictionArchive.Service.NovelService.Models.Configuration;
using FictionArchive.Service.NovelService.Models.Enums; using FictionArchive.Service.NovelService.Models.Enums;
using FictionArchive.Service.NovelService.Models.Images; using FictionArchive.Service.NovelService.Models.Images;
using FictionArchive.Service.NovelService.Models.IntegrationEvents;
using FictionArchive.Service.NovelService.Models.Localization; using FictionArchive.Service.NovelService.Models.Localization;
using FictionArchive.Service.NovelService.Models.Novels; using FictionArchive.Service.NovelService.Models.Novels;
using FictionArchive.Service.NovelService.Models.SourceAdapters; using FictionArchive.Service.NovelService.Models.SourceAdapters;
using FictionArchive.Service.NovelService.Services.SourceAdapters; using FictionArchive.Service.NovelService.Services.SourceAdapters;
using FictionArchive.Service.Shared.Services.EventBus; using FictionArchive.Service.Shared.MassTransit.Contracts.Commands;
using FictionArchive.Service.Shared.MassTransit.Contracts.Events;
using HtmlAgilityPack; using HtmlAgilityPack;
using MassTransit;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
@@ -20,15 +20,23 @@ public class NovelUpdateService
private readonly NovelServiceDbContext _dbContext; private readonly NovelServiceDbContext _dbContext;
private readonly ILogger<NovelUpdateService> _logger; private readonly ILogger<NovelUpdateService> _logger;
private readonly IEnumerable<ISourceAdapter> _sourceAdapters; private readonly IEnumerable<ISourceAdapter> _sourceAdapters;
private readonly IEventBus _eventBus; private readonly IPublishEndpoint _publishEndpoint;
private readonly ISendEndpointProvider _sendEndpointProvider;
private readonly NovelUpdateServiceConfiguration _novelUpdateServiceConfiguration; 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,
ISendEndpointProvider sendEndpointProvider,
IOptions<NovelUpdateServiceConfiguration> novelUpdateServiceConfiguration)
{ {
_dbContext = dbContext; _dbContext = dbContext;
_logger = logger; _logger = logger;
_sourceAdapters = sourceAdapters; _sourceAdapters = sourceAdapters;
_eventBus = eventBus; _publishEndpoint = publishEndpoint;
_sendEndpointProvider = sendEndpointProvider;
_novelUpdateServiceConfiguration = novelUpdateServiceConfiguration.Value; _novelUpdateServiceConfiguration = novelUpdateServiceConfiguration.Value;
} }
@@ -190,6 +198,48 @@ public class NovelUpdateService
return existingChapters.Concat(newChapters).ToList(); 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( private static (Image? image, bool shouldPublishEvent) HandleCoverImage(
ImageData? newCoverData, ImageData? newCoverData,
Image? existingCoverImage) Image? existingCoverImage)
@@ -232,7 +282,7 @@ public class NovelUpdateService
metadata.SystemTags, metadata.SystemTags,
metadata.RawLanguage); metadata.RawLanguage);
var chapters = SynchronizeChapters(metadata.Chapters, metadata.RawLanguage, null); var volumes = SynchronizeVolumes(metadata.Volumes, metadata.RawLanguage, null);
var novel = new Novel var novel = new Novel
{ {
@@ -243,7 +293,7 @@ public class NovelUpdateService
CoverImage = metadata.CoverImage != null CoverImage = metadata.CoverImage != null
? new Image { OriginalPath = metadata.CoverImage.Url } ? new Image { OriginalPath = metadata.CoverImage.Url }
: null, : null,
Chapters = chapters, Volumes = volumes,
Description = LocalizationKey.CreateFromText(metadata.Description, metadata.RawLanguage), Description = LocalizationKey.CreateFromText(metadata.Description, metadata.RawLanguage),
Name = LocalizationKey.CreateFromText(metadata.Name, metadata.RawLanguage), Name = LocalizationKey.CreateFromText(metadata.Name, metadata.RawLanguage),
RawStatus = metadata.RawStatus, RawStatus = metadata.RawStatus,
@@ -289,7 +339,9 @@ public class NovelUpdateService
.Include(n => n.Description) .Include(n => n.Description)
.ThenInclude(lk => lk.Texts) .ThenInclude(lk => lk.Texts)
.Include(n => n.Tags) .Include(n => n.Tags)
.Include(n => n.Chapters).ThenInclude(chapter => chapter.Body) .Include(n => n.Volumes)
.ThenInclude(volume => volume.Chapters)
.ThenInclude(chapter => chapter.Body)
.ThenInclude(localizationKey => localizationKey.Texts) .ThenInclude(localizationKey => localizationKey.Texts)
.Include(n => n.CoverImage) .Include(n => n.CoverImage)
.FirstOrDefaultAsync(n => .FirstOrDefaultAsync(n =>
@@ -299,6 +351,12 @@ public class NovelUpdateService
Novel novel; Novel novel;
bool shouldPublishCoverEvent; 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) if (existingNovel == null)
{ {
// CREATE PATH: New novel // CREATE PATH: New novel
@@ -326,11 +384,11 @@ public class NovelUpdateService
metadata.SystemTags, metadata.SystemTags,
metadata.RawLanguage); metadata.RawLanguage);
// Synchronize chapters (add only) // Synchronize volumes (and their chapters)
novel.Chapters = SynchronizeChapters( novel.Volumes = SynchronizeVolumes(
metadata.Chapters, metadata.Volumes,
metadata.RawLanguage, metadata.RawLanguage,
existingNovel.Chapters); existingNovel.Volumes);
// Handle cover image // Handle cover image
(novel.CoverImage, shouldPublishCoverEvent) = HandleCoverImage( (novel.CoverImage, shouldPublishCoverEvent) = HandleCoverImage(
@@ -340,10 +398,41 @@ public class NovelUpdateService
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync();
// Publish cover image event if needed // Publish novel created event for new novels
if (existingNovel == null)
{
await _publishEndpoint.Publish(new NovelCreatedEvent
{
NovelId = novel.Id,
Title = novel.Name.Texts.First(t => t.Language == novel.RawLanguage).Text,
OriginalLanguage = novel.RawLanguage,
Source = novel.Source.Key,
AuthorName = novel.Author.Name.Texts.First(t => t.Language == novel.RawLanguage).Text
});
}
// 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(new ChapterCreatedEvent
{
ChapterId = chapter.Id,
NovelId = novel.Id,
VolumeId = volume.Id,
VolumeOrder = volume.Order,
ChapterOrder = chapter.Order,
ChapterTitle = chapter.Name.Texts.First(t => t.Language == novel.RawLanguage).Text
});
}
}
// Send cover image upload command if needed
if (shouldPublishCoverEvent && novel.CoverImage != null && metadata.CoverImage != null) if (shouldPublishCoverEvent && novel.CoverImage != null && metadata.CoverImage != null)
{ {
await _eventBus.Publish(new FileUploadRequestCreatedEvent var uploadEndpoint = await _sendEndpointProvider.GetSendEndpoint(new Uri("queue:upload-file-command"));
await uploadEndpoint.Send(new UploadFileCommand
{ {
RequestId = novel.CoverImage.Id, RequestId = novel.CoverImage.Id,
FileData = metadata.CoverImage.Data, FileData = metadata.CoverImage.Data,
@@ -351,32 +440,42 @@ public class NovelUpdateService
}); });
} }
// Publish chapter pull events for chapters without body content // Send chapter pull commands for chapters without body content
var chaptersNeedingPull = novel.Chapters var pullChapterEndpoint = await _sendEndpointProvider.GetSendEndpoint(new Uri("queue:pull-chapter-content-command"));
foreach (var volume in novel.Volumes)
{
var chaptersNeedingPull = volume.Chapters
.Where(c => c.Body?.Texts == null || !c.Body.Texts.Any()) .Where(c => c.Body?.Texts == null || !c.Body.Texts.Any())
.ToList(); .ToList();
foreach (var chapter in chaptersNeedingPull) foreach (var chapter in chaptersNeedingPull)
{ {
await _eventBus.Publish(new ChapterPullRequestedEvent await pullChapterEndpoint.Send(new PullChapterContentCommand
{ {
NovelId = novel.Id, NovelId = novel.Id,
ChapterNumber = chapter.Order VolumeId = volume.Id,
ChapterOrder = chapter.Order
}); });
} }
}
return novel; 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) 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(chapter => chapter.Body)
.ThenInclude(body => body.Texts) .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(); .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 adapter = _sourceAdapters.FirstOrDefault(adapter => adapter.SourceDescriptor.Key == novel.Source.Key);
var rawChapter = await adapter.GetRawChapter(chapter.Url); var rawChapter = await adapter.GetRawChapter(chapter.Url);
@@ -424,12 +523,13 @@ public class NovelUpdateService
localizationText.Text = chapterDoc.DocumentNode.OuterHtml; localizationText.Text = chapterDoc.DocumentNode.OuterHtml;
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync();
// Body was updated, raise image request // Body was updated, send upload commands for images
var uploadEndpoint = await _sendEndpointProvider.GetSendEndpoint(new Uri("queue:upload-file-command"));
int imgCount = 0; int imgCount = 0;
foreach (var image in chapter.Images) foreach (var image in chapter.Images)
{ {
var data = rawChapter.ImageData.FirstOrDefault(img => img.Url == image.OriginalPath); var data = rawChapter.ImageData.FirstOrDefault(img => img.Url == image.OriginalPath);
await _eventBus.Publish(new FileUploadRequestCreatedEvent() await uploadEndpoint.Send(new UploadFileCommand
{ {
FileData = data.Data, FileData = data.Data,
FilePath = $"{novel.Id}/Images/Chapter-{chapter.Id}/{imgCount++}.jpg", FilePath = $"{novel.Id}/Images/Chapter-{chapter.Id}/{imgCount++}.jpg",
@@ -468,25 +568,28 @@ public class NovelUpdateService
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync();
} }
public async Task<NovelUpdateRequestedEvent> QueueNovelImport(string novelUrl) public async Task<ImportNovelCommand> QueueNovelImport(string novelUrl)
{ {
var importNovelRequestEvent = new NovelUpdateRequestedEvent() var command = new ImportNovelCommand
{ {
NovelUrl = novelUrl NovelUrl = novelUrl
}; };
await _eventBus.Publish(importNovelRequestEvent); var endpoint = await _sendEndpointProvider.GetSendEndpoint(new Uri("queue:import-novel-command"));
return importNovelRequestEvent; await endpoint.Send(command);
return command;
} }
public async Task<ChapterPullRequestedEvent> QueueChapterPull(uint novelId, uint chapterNumber) public async Task<PullChapterContentCommand> QueueChapterPull(uint novelId, uint volumeId, uint chapterOrder)
{ {
var chapterPullEvent = new ChapterPullRequestedEvent() var command = new PullChapterContentCommand
{ {
NovelId = novelId, NovelId = novelId,
ChapterNumber = chapterNumber VolumeId = volumeId,
ChapterOrder = chapterOrder
}; };
await _eventBus.Publish(chapterPullEvent); var endpoint = await _sendEndpointProvider.GetSendEndpoint(new Uri("queue:pull-chapter-content-command"));
return chapterPullEvent; await endpoint.Send(command);
return command;
} }
public async Task DeleteNovel(uint novelId) public async Task DeleteNovel(uint novelId)
@@ -495,9 +598,10 @@ public class NovelUpdateService
.Include(n => n.CoverImage) .Include(n => n.CoverImage)
.Include(n => n.Name).ThenInclude(k => k.Texts) .Include(n => n.Name).ThenInclude(k => k.Texts)
.Include(n => n.Description).ThenInclude(k => k.Texts) .Include(n => n.Description).ThenInclude(k => k.Texts)
.Include(n => n.Chapters).ThenInclude(c => c.Images) .Include(n => n.Volumes).ThenInclude(v => v.Name).ThenInclude(k => k.Texts)
.Include(n => n.Chapters).ThenInclude(c => c.Name).ThenInclude(k => k.Texts) .Include(n => n.Volumes).ThenInclude(v => v.Chapters).ThenInclude(c => c.Images)
.Include(n => n.Chapters).ThenInclude(c => c.Body).ThenInclude(k => k.Texts) .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); .FirstOrDefaultAsync(n => n.Id == novelId);
if (novel == null) if (novel == null)
@@ -505,8 +609,12 @@ public class NovelUpdateService
// Collect all LocalizationKey IDs for cleanup // Collect all LocalizationKey IDs for cleanup
var locKeyIds = new List<Guid> { novel.Name.Id, novel.Description.Id }; var locKeyIds = new List<Guid> { novel.Name.Id, novel.Description.Id };
locKeyIds.AddRange(novel.Chapters.Select(c => c.Name.Id)); foreach (var volume in novel.Volumes)
locKeyIds.AddRange(novel.Chapters.Select(c => c.Body.Id)); {
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 // 1. Remove LocalizationRequests referencing these keys
var locRequests = await _dbContext.LocalizationRequests var locRequests = await _dbContext.LocalizationRequests
@@ -517,19 +625,26 @@ public class NovelUpdateService
// 2. Remove LocalizationTexts (NO ACTION FK - won't cascade) // 2. Remove LocalizationTexts (NO ACTION FK - won't cascade)
_dbContext.RemoveRange(novel.Name.Texts); _dbContext.RemoveRange(novel.Name.Texts);
_dbContext.RemoveRange(novel.Description.Texts); _dbContext.RemoveRange(novel.Description.Texts);
foreach (var chapter in novel.Chapters) 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.Name.Texts);
_dbContext.RemoveRange(chapter.Body.Texts); _dbContext.RemoveRange(chapter.Body.Texts);
} }
}
// 3. Remove Images (NO ACTION FK - won't cascade) // 3. Remove Images (NO ACTION FK - won't cascade)
if (novel.CoverImage != null) if (novel.CoverImage != null)
_dbContext.Images.Remove(novel.CoverImage); _dbContext.Images.Remove(novel.CoverImage);
foreach (var chapter in novel.Chapters) foreach (var volume in novel.Volumes)
{
foreach (var chapter in volume.Chapters)
_dbContext.Images.RemoveRange(chapter.Images); _dbContext.Images.RemoveRange(chapter.Images);
}
// 4. Remove novel - cascades: chapters, localization keys, tag mappings // 4. Remove novel - cascades: volumes, chapters, localization keys, tag mappings
_dbContext.Novels.Remove(novel); _dbContext.Novels.Remove(novel);
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync();
} }

View File

@@ -66,7 +66,7 @@ public class NovelpiaAdapter : ISourceAdapter
ExternalId = novelId.ToString(), ExternalId = novelId.ToString(),
SystemTags = new List<string>(), SystemTags = new List<string>(),
SourceTags = new List<string>(), SourceTags = new List<string>(),
Chapters = new List<ChapterMetadata>(), Volumes = new List<VolumeMetadata>(),
SourceDescriptor = SourceDescriptor SourceDescriptor = SourceDescriptor
}; };
@@ -133,6 +133,9 @@ public class NovelpiaAdapter : ISourceAdapter
novel.SourceTags.Add(tag); novel.SourceTags.Add(tag);
} }
// Author's posts (from notice_table in the page HTML)
var authorsPosts = ParseAuthorsPosts(novelData);
// Chapters // Chapters
uint page = 0; uint page = 0;
List<ChapterMetadata> chapters = new List<ChapterMetadata>(); List<ChapterMetadata> chapters = new List<ChapterMetadata>();
@@ -168,7 +171,25 @@ public class NovelpiaAdapter : ISourceAdapter
} }
page++; 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; return novel;
} }
@@ -241,4 +262,40 @@ public class NovelpiaAdapter : ISourceAdapter
} }
return await image.Content.ReadAsByteArrayAsync(); 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;
}
} }

View File

@@ -16,8 +16,10 @@
"DefaultConnection": "Host=localhost;Database=FictionArchive_NovelService;Username=postgres;password=postgres" "DefaultConnection": "Host=localhost;Database=FictionArchive_NovelService;Username=postgres;password=postgres"
}, },
"RabbitMQ": { "RabbitMQ": {
"ConnectionString": "amqp://localhost", "Host": "localhost",
"ClientIdentifier": "NovelService" "VirtualHost": "/",
"Username": "guest",
"Password": "guest"
}, },
"AllowedHosts": "*", "AllowedHosts": "*",
"OIDC": { "OIDC": {

View File

@@ -0,0 +1,23 @@
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER $APP_UID
WORKDIR /app
EXPOSE 8080
EXPOSE 8081
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["FictionArchive.Service.ReportingService/FictionArchive.Service.ReportingService.csproj", "FictionArchive.Service.ReportingService/"]
RUN dotnet restore "FictionArchive.Service.ReportingService/FictionArchive.Service.ReportingService.csproj"
COPY . .
WORKDIR "/src/FictionArchive.Service.ReportingService"
RUN dotnet build "./FictionArchive.Service.ReportingService.csproj" -c $BUILD_CONFIGURATION -o /app/build
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./FictionArchive.Service.ReportingService.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "FictionArchive.Service.ReportingService.dll"]

View File

@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.11">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FictionArchive.Service.Shared\FictionArchive.Service.Shared.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,15 @@
using HotChocolate;
using HotChocolate.Authorization;
namespace FictionArchive.Service.ReportingService.GraphQL;
public class Mutation
{
/// <summary>
/// Placeholder mutation for GraphQL schema requirements.
/// The ReportingService is primarily read-only, consuming events from other services.
/// </summary>
[Authorize(Roles = ["admin"])]
[GraphQLDescription("Placeholder mutation. ReportingService is primarily read-only.")]
public bool Ping() => true;
}

View File

@@ -0,0 +1,52 @@
using System.Text.Json;
using FictionArchive.Service.ReportingService.Models.Database;
using FictionArchive.Service.ReportingService.Models.DTOs;
using FictionArchive.Service.ReportingService.Services;
using HotChocolate;
using HotChocolate.Data;
using Microsoft.EntityFrameworkCore;
namespace FictionArchive.Service.ReportingService.GraphQL;
public class Query
{
[UseProjection]
[UseFiltering]
[UseSorting]
[GraphQLName("reportingJobs")]
public IQueryable<Job> GetReportingJobs(ReportingServiceDbContext dbContext)
=> dbContext.Jobs.Include(j => j.History);
[GraphQLName("reportingJob")]
public async Task<JobDto?> GetReportingJob(Guid id, ReportingServiceDbContext dbContext)
{
var job = await dbContext.Jobs
.Include(j => j.History.OrderBy(h => h.Timestamp))
.FirstOrDefaultAsync(j => j.Id == id);
if (job == null) return null;
return new JobDto
{
Id = job.Id,
JobType = job.JobType,
Status = job.Status,
CurrentStep = job.CurrentStep,
ErrorMessage = job.ErrorMessage,
Metadata = job.Metadata != null
? JsonSerializer.Deserialize<Dictionary<string, object>>(job.Metadata.RootElement.GetRawText())
: null,
History = job.History.Select(h => new JobHistoryEntryDto
{
FromState = h.FromState,
ToState = h.ToState,
Message = h.Message,
Error = h.Error,
Timestamp = h.Timestamp
}).ToList(),
CreatedTime = job.CreatedTime,
UpdatedTime = job.UpdatedTime,
CompletedTime = job.CompletedTime
};
}
}

View File

@@ -0,0 +1,17 @@
using NodaTime;
namespace FictionArchive.Service.ReportingService.Models.DTOs;
public class JobDto
{
public Guid Id { get; set; }
public required string JobType { get; set; }
public required string Status { get; set; }
public string? CurrentStep { get; set; }
public string? ErrorMessage { get; set; }
public Dictionary<string, object>? Metadata { get; set; }
public List<JobHistoryEntryDto> History { get; set; } = new();
public Instant CreatedTime { get; set; }
public Instant UpdatedTime { get; set; }
public Instant? CompletedTime { get; set; }
}

View File

@@ -0,0 +1,12 @@
using NodaTime;
namespace FictionArchive.Service.ReportingService.Models.DTOs;
public class JobHistoryEntryDto
{
public required string FromState { get; set; }
public required string ToState { get; set; }
public string? Message { get; set; }
public string? Error { get; set; }
public Instant Timestamp { get; set; }
}

View File

@@ -0,0 +1,19 @@
using System.Text.Json;
using NodaTime;
namespace FictionArchive.Service.ReportingService.Models.Database;
public class Job
{
public Guid Id { get; set; }
public required string JobType { get; set; }
public required string Status { get; set; }
public string? CurrentStep { get; set; }
public string? ErrorMessage { get; set; }
public JsonDocument? Metadata { get; set; }
public Instant CreatedTime { get; set; }
public Instant UpdatedTime { get; set; }
public Instant? CompletedTime { get; set; }
public ICollection<JobHistoryEntry> History { get; set; } = new List<JobHistoryEntry>();
}

View File

@@ -0,0 +1,16 @@
using NodaTime;
namespace FictionArchive.Service.ReportingService.Models.Database;
public class JobHistoryEntry
{
public int Id { get; set; }
public Guid JobId { get; set; }
public required string FromState { get; set; }
public required string ToState { get; set; }
public string? Message { get; set; }
public string? Error { get; set; }
public Instant Timestamp { get; set; }
public Job Job { get; set; } = null!;
}

View File

@@ -0,0 +1,76 @@
using FictionArchive.Common.Extensions;
using FictionArchive.Service.ReportingService.GraphQL;
using FictionArchive.Service.ReportingService.Services;
using FictionArchive.Service.ReportingService.Services.Consumers;
using FictionArchive.Service.Shared;
using FictionArchive.Service.Shared.Extensions;
using FictionArchive.Service.Shared.MassTransit;
namespace FictionArchive.Service.ReportingService;
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
var isSchemaExport = SchemaExportDetector.IsSchemaExportMode(args);
builder.AddLocalAppsettings();
builder.Services.AddHealthChecks();
#region Database
builder.Services.RegisterDbContext<ReportingServiceDbContext>(
builder.Configuration.GetConnectionString("DefaultConnection")!,
skipInfrastructure: isSchemaExport);
#endregion
#region MassTransit
if (!isSchemaExport)
{
builder.Services.AddFictionArchiveMassTransit<ReportingServiceDbContext>(
builder.Configuration,
x =>
{
x.AddConsumer<JobStateChangedEventConsumer>();
});
}
#endregion
#region GraphQL
builder.Services.AddDefaultGraphQl<Query, Mutation>()
.AddAuthorization();
#endregion
// Authentication & Authorization
builder.Services.AddOidcAuthentication(builder.Configuration);
builder.Services.AddFictionArchiveAuthorization();
var app = builder.Build();
if (!isSchemaExport)
{
using var scope = app.Services.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<ReportingServiceDbContext>();
dbContext.UpdateDatabase();
}
app.UseHttpsRedirection();
app.MapHealthChecks("/healthz");
app.UseAuthentication();
app.UseAuthorization();
app.MapGraphQL();
app.RunWithGraphQLCommands(args);
}
}

View File

@@ -0,0 +1,39 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:45320",
"sslPort": 44320
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:5180",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "graphql",
"applicationUrl": "https://localhost:7320;http://localhost:5180",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@@ -0,0 +1,74 @@
using System.Text.Json;
using FictionArchive.Service.ReportingService.Models.Database;
using FictionArchive.Service.Shared.MassTransit.Contracts;
using MassTransit;
namespace FictionArchive.Service.ReportingService.Services.Consumers;
public class JobStateChangedEventConsumer : IConsumer<JobStateChangedEvent>
{
private readonly ReportingServiceDbContext _dbContext;
private readonly ILogger<JobStateChangedEventConsumer> _logger;
public JobStateChangedEventConsumer(
ReportingServiceDbContext dbContext,
ILogger<JobStateChangedEventConsumer> logger)
{
_dbContext = dbContext;
_logger = logger;
}
public async Task Consume(ConsumeContext<JobStateChangedEvent> context)
{
var @event = context.Message;
var job = await _dbContext.Jobs.FindAsync(@event.JobId);
if (job == null)
{
job = new Job
{
Id = @event.JobId,
JobType = @event.JobType,
Status = @event.ToState,
CreatedTime = @event.Timestamp,
UpdatedTime = @event.Timestamp,
Metadata = @event.Metadata != null
? JsonSerializer.SerializeToDocument(@event.Metadata)
: null
};
_dbContext.Jobs.Add(job);
}
else
{
job.Status = @event.ToState;
job.UpdatedTime = @event.Timestamp;
if (@event.Error != null)
{
job.ErrorMessage = @event.Error;
}
if (@event.ToState is "Completed" or "Failed")
{
job.CompletedTime = @event.Timestamp;
}
}
var historyEntry = new JobHistoryEntry
{
JobId = @event.JobId,
FromState = @event.FromState,
ToState = @event.ToState,
Message = @event.Message,
Error = @event.Error,
Timestamp = @event.Timestamp
};
_dbContext.JobHistoryEntries.Add(historyEntry);
await _dbContext.SaveChangesAsync();
_logger.LogDebug("Recorded job state change: {JobId} {FromState} -> {ToState}",
@event.JobId, @event.FromState, @event.ToState);
}
}

View File

@@ -0,0 +1,36 @@
using FictionArchive.Service.ReportingService.Models.Database;
using FictionArchive.Service.Shared.Services.Database;
using Microsoft.EntityFrameworkCore;
namespace FictionArchive.Service.ReportingService.Services;
public class ReportingServiceDbContext : FictionArchiveDbContext
{
public ReportingServiceDbContext(DbContextOptions options, ILogger<ReportingServiceDbContext> logger)
: base(options, logger) { }
public DbSet<Job> Jobs => Set<Job>();
public DbSet<JobHistoryEntry> JobHistoryEntries => Set<JobHistoryEntry>();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Job>(entity =>
{
entity.HasKey(e => e.Id);
entity.HasIndex(e => e.JobType);
entity.HasIndex(e => e.Status);
entity.HasIndex(e => e.CreatedTime);
});
modelBuilder.Entity<JobHistoryEntry>(entity =>
{
entity.HasKey(e => e.Id);
entity.HasOne(e => e.Job)
.WithMany(j => j.History)
.HasForeignKey(e => e.JobId)
.OnDelete(DeleteBehavior.Cascade);
});
}
}

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@@ -0,0 +1,28 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"ConnectionStrings": {
"DefaultConnection": "Host=localhost;Database=FictionArchive_ReportingService;Username=postgres;password=postgres"
},
"RabbitMQ": {
"Host": "localhost",
"VirtualHost": "/",
"Username": "guest",
"Password": "guest"
},
"OIDC": {
"Authority": "https://auth.orfl.xyz/application/o/fiction-archive/",
"ClientId": "ldi5IpEidq2WW0Ka1lehVskb2SOBjnYRaZCpEyBh",
"Audience": "ldi5IpEidq2WW0Ka1lehVskb2SOBjnYRaZCpEyBh",
"ValidIssuer": "https://auth.orfl.xyz/application/o/fiction-archive/",
"ValidateIssuer": true,
"ValidateAudience": true,
"ValidateLifetime": true,
"ValidateIssuerSigningKey": true
},
"AllowedHosts": "*"
}

View File

@@ -0,0 +1,6 @@
{
"subgraph": "Reporting",
"http": {
"baseAddress": "http://localhost:5180/graphql"
}
}

View File

@@ -19,6 +19,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="AppAny.Quartz.EntityFrameworkCore.Migrations.PostgreSQL" Version="0.5.1" /> <PackageReference Include="AppAny.Quartz.EntityFrameworkCore.Migrations.PostgreSQL" Version="0.5.1" />
<PackageReference Include="MassTransit" Version="8.4.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.11"> <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.11">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

View File

@@ -1,4 +1,4 @@
using FictionArchive.Service.Shared.Services.EventBus; using MassTransit;
using Newtonsoft.Json; using Newtonsoft.Json;
using Quartz; using Quartz;
@@ -6,15 +6,22 @@ namespace FictionArchive.Service.SchedulerService.Models.JobTemplates;
public class EventJobTemplate : IJob public class EventJobTemplate : IJob
{ {
private readonly IEventBus _eventBus; private readonly IPublishEndpoint _publishEndpoint;
private readonly ISendEndpointProvider _sendEndpointProvider;
private readonly ILogger<EventJobTemplate> _logger; private readonly ILogger<EventJobTemplate> _logger;
public const string EventTypeParameter = "RoutingKey"; public const string EventTypeParameter = "MessageType";
public const string EventDataParameter = "MessageData"; public const string EventDataParameter = "MessageData";
public const string IsCommandParameter = "IsCommand";
public const string DestinationQueueParameter = "DestinationQueue";
public EventJobTemplate(IEventBus eventBus, ILogger<EventJobTemplate> logger) public EventJobTemplate(
IPublishEndpoint publishEndpoint,
ISendEndpointProvider sendEndpointProvider,
ILogger<EventJobTemplate> logger)
{ {
_eventBus = eventBus; _publishEndpoint = publishEndpoint;
_sendEndpointProvider = sendEndpointProvider;
_logger = logger; _logger = logger;
} }
@@ -22,14 +29,47 @@ public class EventJobTemplate : IJob
{ {
try try
{ {
var eventData = context.MergedJobDataMap.GetString(EventDataParameter); var messageData = context.MergedJobDataMap.GetString(EventDataParameter);
var eventType = context.MergedJobDataMap.GetString(EventTypeParameter); var messageTypeName = context.MergedJobDataMap.GetString(EventTypeParameter);
var eventObject = JsonConvert.DeserializeObject(eventData); var isCommand = context.MergedJobDataMap.GetBoolean(IsCommandParameter);
await _eventBus.Publish(eventObject, eventType);
var messageType = Type.GetType(messageTypeName!);
if (messageType == null)
{
_logger.LogError("Could not resolve message type: {MessageType}", messageTypeName);
return;
}
var message = JsonConvert.DeserializeObject(messageData!, messageType);
if (message == null)
{
_logger.LogError("Could not deserialize message data for type: {MessageType}", messageTypeName);
return;
}
if (isCommand)
{
var destinationQueue = context.MergedJobDataMap.GetString(DestinationQueueParameter);
if (string.IsNullOrEmpty(destinationQueue))
{
_logger.LogError("Destination queue not specified for command message");
return;
}
var endpoint = await _sendEndpointProvider.GetSendEndpoint(new Uri($"queue:{destinationQueue}"));
await endpoint.Send(message, messageType);
_logger.LogInformation("Sent command {MessageType} to queue {Queue}", messageTypeName, destinationQueue);
}
else
{
await _publishEndpoint.Publish(message, messageType);
_logger.LogInformation("Published event {MessageType}", messageTypeName);
}
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "An error occurred while running an event job."); _logger.LogError(ex, "An error occurred while running an event job.");
throw; // Re-throw to let Quartz handle retries
} }
} }
} }

View File

@@ -2,7 +2,7 @@ using FictionArchive.Service.SchedulerService.GraphQL;
using FictionArchive.Service.SchedulerService.Services; using FictionArchive.Service.SchedulerService.Services;
using FictionArchive.Service.Shared; using FictionArchive.Service.Shared;
using FictionArchive.Service.Shared.Extensions; using FictionArchive.Service.Shared.Extensions;
using FictionArchive.Service.Shared.Services.EventBus.Implementations; using FictionArchive.Service.Shared.MassTransit;
using Quartz; using Quartz;
using Quartz.Impl.AdoJobStore; using Quartz.Impl.AdoJobStore;
@@ -34,14 +34,11 @@ public class Program
#endregion #endregion
#region Event Bus #region MassTransit
if (!isSchemaExport) if (!isSchemaExport)
{ {
builder.Services.AddRabbitMQ(opt => builder.Services.AddFictionArchiveMassTransit(builder.Configuration);
{
builder.Configuration.GetSection("RabbitMQ").Bind(opt);
});
} }
#endregion #endregion

View File

@@ -1,7 +1,6 @@
using System.Data; using System.Data;
using FictionArchive.Service.SchedulerService.Models; using FictionArchive.Service.SchedulerService.Models;
using FictionArchive.Service.SchedulerService.Models.JobTemplates; using FictionArchive.Service.SchedulerService.Models.JobTemplates;
using FictionArchive.Service.Shared.Services.EventBus;
using Quartz; using Quartz;
using Quartz.Impl.Matchers; using Quartz.Impl.Matchers;

View File

@@ -6,8 +6,10 @@
} }
}, },
"RabbitMQ": { "RabbitMQ": {
"ConnectionString": "amqp://localhost", "Host": "localhost",
"ClientIdentifier": "SchedulerService" "VirtualHost": "/",
"Username": "guest",
"Password": "guest"
}, },
"ConnectionStrings": { "ConnectionStrings": {
"DefaultConnection": "Host=localhost;Database=FictionArchive_SchedulerService;Username=postgres;password=postgres" "DefaultConnection": "Host=localhost;Database=FictionArchive_SchedulerService;Username=postgres;password=postgres"

View File

@@ -31,7 +31,9 @@
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" /> <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime" Version="9.0.4" /> <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime" Version="9.0.4" />
<PackageReference Include="Polly" Version="8.6.5" /> <PackageReference Include="Polly" Version="8.6.5" />
<PackageReference Include="RabbitMQ.Client" Version="7.2.0" /> <PackageReference Include="MassTransit" Version="8.4.0" />
<PackageReference Include="MassTransit.RabbitMQ" Version="8.4.0" />
<PackageReference Include="MassTransit.EntityFrameworkCore" Version="8.4.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.11" /> <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.11" />
</ItemGroup> </ItemGroup>

View File

@@ -0,0 +1,9 @@
namespace FictionArchive.Service.Shared.MassTransit.Configuration;
public class MassTransitOptions
{
public string Host { get; set; } = "localhost";
public string VirtualHost { get; set; } = "/";
public string Username { get; set; } = "guest";
public string Password { get; set; } = "guest";
}

View File

@@ -0,0 +1,6 @@
namespace FictionArchive.Service.Shared.MassTransit.Contracts.Commands;
public record ImportNovelCommand : ICommand
{
public required string NovelUrl { get; init; }
}

View File

@@ -0,0 +1,8 @@
namespace FictionArchive.Service.Shared.MassTransit.Contracts.Commands;
public record PullChapterContentCommand : ICommand
{
public required uint NovelId { get; init; }
public required uint VolumeId { get; init; }
public required uint ChapterOrder { get; init; }
}

View File

@@ -0,0 +1,12 @@
using FictionArchive.Common.Enums;
namespace FictionArchive.Service.Shared.MassTransit.Contracts.Commands;
public record TranslateTextCommand : ICommand
{
public Guid TranslationRequestId { get; init; }
public Language From { get; init; }
public Language To { get; init; }
public required string Body { get; init; }
public required string TranslationEngineKey { get; init; }
}

View File

@@ -0,0 +1,8 @@
namespace FictionArchive.Service.Shared.MassTransit.Contracts.Commands;
public record UploadFileCommand : ICommand
{
public Guid RequestId { get; init; }
public required string FilePath { get; init; }
public required byte[] FileData { get; init; }
}

View File

@@ -0,0 +1,9 @@
namespace FictionArchive.Service.Shared.MassTransit.Contracts.Events;
public record AuthUserAddedEvent : IEvent
{
public required string OAuthProviderId { get; init; }
public required string InviterOAuthProviderId { get; init; }
public required string EventUserEmail { get; init; }
public required string EventUserUsername { get; init; }
}

View File

@@ -0,0 +1,11 @@
namespace FictionArchive.Service.Shared.MassTransit.Contracts.Events;
public record ChapterCreatedEvent : IEvent
{
public required uint ChapterId { get; init; }
public required uint NovelId { get; init; }
public required uint VolumeId { get; init; }
public required int VolumeOrder { get; init; }
public required uint ChapterOrder { get; init; }
public required string ChapterTitle { get; init; }
}

View File

@@ -0,0 +1,11 @@
using FictionArchive.Common.Enums;
namespace FictionArchive.Service.Shared.MassTransit.Contracts.Events;
public record FileUploadCompletedEvent : IEvent
{
public Guid RequestId { get; init; }
public RequestStatus Status { get; init; }
public string? FileAccessUrl { get; init; }
public string? ErrorMessage { get; init; }
}

View File

@@ -0,0 +1,12 @@
using FictionArchive.Common.Enums;
namespace FictionArchive.Service.Shared.MassTransit.Contracts.Events;
public record NovelCreatedEvent : IEvent
{
public required uint NovelId { get; init; }
public required string Title { get; init; }
public required Language OriginalLanguage { get; init; }
public required string Source { get; init; }
public required string AuthorName { get; init; }
}

View File

@@ -0,0 +1,7 @@
namespace FictionArchive.Service.Shared.MassTransit.Contracts.Events;
public record TranslationCompletedEvent : IEvent
{
public Guid TranslationRequestId { get; init; }
public required string TranslatedText { get; init; }
}

View File

@@ -0,0 +1,13 @@
namespace FictionArchive.Service.Shared.MassTransit.Contracts.Events;
public record UserInvitedEvent : IEvent
{
public Guid InvitedUserId { get; init; }
public required string InvitedUsername { get; init; }
public required string InvitedEmail { get; init; }
public required string InvitedOAuthProviderId { get; init; }
public Guid InviterId { get; init; }
public required string InviterUsername { get; init; }
public required string InviterOAuthProviderId { get; init; }
}

View File

@@ -0,0 +1,6 @@
namespace FictionArchive.Service.Shared.MassTransit.Contracts;
/// <summary>
/// Marker interface for commands (do something, single consumer)
/// </summary>
public interface ICommand { }

View File

@@ -0,0 +1,6 @@
namespace FictionArchive.Service.Shared.MassTransit.Contracts;
/// <summary>
/// Marker interface for events (something happened, multiple subscribers)
/// </summary>
public interface IEvent { }

View File

@@ -0,0 +1,18 @@
using NodaTime;
namespace FictionArchive.Service.Shared.MassTransit.Contracts;
/// <summary>
/// Published by sagas on state transitions for centralized job tracking
/// </summary>
public record JobStateChangedEvent : IEvent
{
public Guid JobId { get; init; }
public required string JobType { get; init; }
public required string FromState { get; init; }
public required string ToState { get; init; }
public string? Message { get; init; }
public string? Error { get; init; }
public Instant Timestamp { get; init; }
public Dictionary<string, object>? Metadata { get; init; }
}

View File

@@ -0,0 +1,103 @@
using FictionArchive.Service.Shared.MassTransit.Configuration;
using MassTransit;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace FictionArchive.Service.Shared.MassTransit;
public static class MassTransitExtensions
{
/// <summary>
/// Adds MassTransit with RabbitMQ and Entity Framework outbox
/// </summary>
public static IServiceCollection AddFictionArchiveMassTransit<TDbContext>(
this IServiceCollection services,
IConfiguration configuration,
Action<IBusRegistrationConfigurator>? configureConsumers = null)
where TDbContext : DbContext
{
services.AddMassTransit(x =>
{
configureConsumers?.Invoke(x);
x.AddEntityFrameworkOutbox<TDbContext>(o =>
{
o.UsePostgres();
o.UseBusOutbox();
});
x.UsingRabbitMq((context, cfg) =>
{
var options = configuration.GetSection("RabbitMQ").Get<MassTransitOptions>()
?? new MassTransitOptions();
cfg.Host(options.Host, options.VirtualHost, h =>
{
h.Username(options.Username);
h.Password(options.Password);
});
// Immediate retries for transient failures
cfg.UseMessageRetry(r => r.Intervals(
TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(1)));
// Delayed redelivery for longer outages
cfg.UseDelayedRedelivery(r => r.Intervals(
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(30),
TimeSpan.FromMinutes(2),
TimeSpan.FromMinutes(10),
TimeSpan.FromMinutes(30)));
cfg.ConfigureEndpoints(context);
});
});
return services;
}
/// <summary>
/// Adds MassTransit with RabbitMQ without outbox (for services without EF)
/// </summary>
public static IServiceCollection AddFictionArchiveMassTransit(
this IServiceCollection services,
IConfiguration configuration,
Action<IBusRegistrationConfigurator>? configureConsumers = null)
{
services.AddMassTransit(x =>
{
configureConsumers?.Invoke(x);
x.UsingRabbitMq((context, cfg) =>
{
var options = configuration.GetSection("RabbitMQ").Get<MassTransitOptions>()
?? new MassTransitOptions();
cfg.Host(options.Host, options.VirtualHost, h =>
{
h.Username(options.Username);
h.Password(options.Password);
});
cfg.UseMessageRetry(r => r.Intervals(
TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(1)));
cfg.UseDelayedRedelivery(r => r.Intervals(
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(30),
TimeSpan.FromMinutes(2),
TimeSpan.FromMinutes(10),
TimeSpan.FromMinutes(30)));
cfg.ConfigureEndpoints(context);
});
});
return services;
}
}

View File

@@ -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;
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}

View File

@@ -1,7 +0,0 @@
using NodaTime;
namespace FictionArchive.Service.Shared.Services.EventBus;
public interface IIntegrationEvent
{
}

View File

@@ -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);
}

View File

@@ -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;
}
}

View File

@@ -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);
}
}
}

View File

@@ -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>();
}
}

View File

@@ -1,7 +0,0 @@
namespace FictionArchive.Service.Shared.Services.EventBus.Implementations;
public class RabbitMQOptions
{
public string ConnectionString { get; set; }
public string ClientIdentifier { get; set; }
}

View File

@@ -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));
}
}

View File

@@ -23,6 +23,7 @@
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" /> <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
<PackageReference Include="DeepL.net" Version="1.17.0" /> <PackageReference Include="DeepL.net" Version="1.17.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2"/> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2"/>
<PackageReference Include="MassTransit" Version="8.4.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@@ -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; }
}

View File

@@ -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; }
}

View File

@@ -2,16 +2,14 @@ using DeepL;
using FictionArchive.Common.Extensions; using FictionArchive.Common.Extensions;
using FictionArchive.Service.Shared; using FictionArchive.Service.Shared;
using FictionArchive.Service.Shared.Extensions; using FictionArchive.Service.Shared.Extensions;
using FictionArchive.Service.Shared.Services.EventBus.Implementations; using FictionArchive.Service.Shared.MassTransit;
using FictionArchive.Service.Shared.Services.GraphQL; using FictionArchive.Service.Shared.Services.GraphQL;
using FictionArchive.Service.TranslationService.GraphQL; using FictionArchive.Service.TranslationService.GraphQL;
using FictionArchive.Service.TranslationService.Models.IntegrationEvents;
using FictionArchive.Service.TranslationService.Services; using FictionArchive.Service.TranslationService.Services;
using FictionArchive.Service.TranslationService.Services.Database; using FictionArchive.Service.TranslationService.Services.Database;
using FictionArchive.Service.TranslationService.Services.EventHandlers; using FictionArchive.Service.TranslationService.Services.EventHandlers;
using FictionArchive.Service.TranslationService.Services.TranslationEngines; using FictionArchive.Service.TranslationService.Services.TranslationEngines;
using FictionArchive.Service.TranslationService.Services.TranslationEngines.DeepLTranslate; using FictionArchive.Service.TranslationService.Services.TranslationEngines.DeepLTranslate;
using RabbitMQ.Client;
namespace FictionArchive.Service.TranslationService; namespace FictionArchive.Service.TranslationService;
@@ -26,15 +24,16 @@ public class Program
builder.Services.AddHealthChecks(); builder.Services.AddHealthChecks();
#region Event Bus #region MassTransit
if (!isSchemaExport) if (!isSchemaExport)
{ {
builder.Services.AddRabbitMQ(opt => builder.Services.AddFictionArchiveMassTransit<TranslationServiceDbContext>(
builder.Configuration,
x =>
{ {
builder.Configuration.GetSection("RabbitMQ").Bind(opt); x.AddConsumer<TranslateTextCommandConsumer>();
}) });
.Subscribe<TranslationRequestCreatedEvent, TranslationRequestCreatedEventHandler>();
} }
#endregion #endregion

View File

@@ -0,0 +1,40 @@
using FictionArchive.Service.Shared.MassTransit.Contracts.Commands;
using FictionArchive.Service.Shared.MassTransit.Contracts.Events;
using FictionArchive.Service.TranslationService.Models.Enums;
using MassTransit;
namespace FictionArchive.Service.TranslationService.Services.EventHandlers;
public class TranslateTextCommandConsumer : IConsumer<TranslateTextCommand>
{
private readonly ILogger<TranslateTextCommandConsumer> _logger;
private readonly TranslationEngineService _translationEngineService;
public TranslateTextCommandConsumer(
ILogger<TranslateTextCommandConsumer> logger,
TranslationEngineService translationEngineService)
{
_logger = logger;
_translationEngineService = translationEngineService;
}
public async Task Consume(ConsumeContext<TranslateTextCommand> context)
{
var command = context.Message;
var result = await _translationEngineService.Translate(
command.From,
command.To,
command.Body,
command.TranslationEngineKey);
if (result.Status == TranslationRequestStatus.Success)
{
await context.Publish(new TranslationCompletedEvent
{
TranslatedText = result.TranslatedText!,
TranslationRequestId = command.TranslationRequestId,
});
}
}
}

View File

@@ -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,
});
}
}
}

View File

@@ -1,28 +1,21 @@
using System.Text;
using FictionArchive.Common.Enums; 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;
using FictionArchive.Service.TranslationService.Models.Database; using FictionArchive.Service.TranslationService.Models.Database;
using FictionArchive.Service.TranslationService.Models.Enums; using FictionArchive.Service.TranslationService.Models.Enums;
using FictionArchive.Service.TranslationService.Models.IntegrationEvents;
using FictionArchive.Service.TranslationService.Services.Database; using FictionArchive.Service.TranslationService.Services.Database;
using FictionArchive.Service.TranslationService.Services.TranslationEngines; using FictionArchive.Service.TranslationService.Services.TranslationEngines;
using RabbitMQ.Client;
namespace FictionArchive.Service.TranslationService.Services; namespace FictionArchive.Service.TranslationService.Services;
public class TranslationEngineService public class TranslationEngineService
{ {
private readonly IEnumerable<ITranslationEngine> _translationEngines; private readonly IEnumerable<ITranslationEngine> _translationEngines;
private readonly IEventBus _eventBus;
private readonly TranslationServiceDbContext _dbContext; private readonly TranslationServiceDbContext _dbContext;
public TranslationEngineService(IEnumerable<ITranslationEngine> translationEngines, TranslationServiceDbContext dbContext, IEventBus eventBus) public TranslationEngineService(IEnumerable<ITranslationEngine> translationEngines, TranslationServiceDbContext dbContext)
{ {
_translationEngines = translationEngines; _translationEngines = translationEngines;
_dbContext = dbContext; _dbContext = dbContext;
_eventBus = eventBus;
} }
public async Task<TranslationResult> Translate(Language from, Language to, string text, string translationEngineKey) public async Task<TranslationResult> Translate(Language from, Language to, string text, string translationEngineKey)

View File

@@ -12,8 +12,10 @@
"DefaultConnection": "Host=localhost;Database=FictionArchive_NovelService;Username=postgres;password=postgres" "DefaultConnection": "Host=localhost;Database=FictionArchive_NovelService;Username=postgres;password=postgres"
}, },
"RabbitMQ": { "RabbitMQ": {
"ConnectionString": "amqp://localhost", "Host": "localhost",
"ClientIdentifier": "TranslationService" "VirtualHost": "/",
"Username": "guest",
"Password": "guest"
}, },
"AllowedHosts": "*", "AllowedHosts": "*",
"OIDC": { "OIDC": {

Some files were not shown because too many files have changed in this diff Show More