Updated translation service and finished splitting out responsibilities for now

This commit is contained in:
gamer147
2025-11-18 10:07:23 -05:00
parent 3bb8f7f158
commit 0c1705ebe1
48 changed files with 617 additions and 1134 deletions

View File

@@ -0,0 +1,19 @@
using FictionArchive.Service.Shared.Services.Database;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace FictionArchive.Service.Shared;
public static class DatabaseExtensions
{
public static IServiceCollection RegisterDbContext<TContext>(this IServiceCollection services, Action<DbContextOptionsBuilder> optionConfiguration)
where TContext : FictionArchiveDbContext
{
services.AddDbContext<TContext>(opt =>
{
optionConfiguration?.Invoke(opt);
});
return services;
}
}

View File

@@ -0,0 +1,21 @@
using FictionArchive.Service.Shared.Services.Database;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace FictionArchive.Service.Shared.Extensions;
public static class DatabaseExtensions
{
public static IServiceCollection RegisterDbContext<TContext>(this IServiceCollection services,
string connectionString) where TContext : FictionArchiveDbContext
{
services.AddDbContext<TContext>(options =>
{
options.UseNpgsql(connectionString, o =>
{
o.UseNodaTime();
});
});
return services;
}
}

View File

@@ -0,0 +1,22 @@
using FictionArchive.Service.Shared.Services.GraphQL;
using HotChocolate.Execution.Configuration;
using HotChocolate.Types.NodaTime;
using Microsoft.Extensions.DependencyInjection;
namespace FictionArchive.Service.Shared.Extensions;
public static class GraphQLExtensions
{
public static IRequestExecutorBuilder AddDefaultGraphQl<TQuery, TMutation>(this IServiceCollection services) where TQuery : class where TMutation : class
{
return services.AddGraphQLServer()
.AddQueryType<TQuery>()
.AddMutationType<TMutation>()
.AddType<UnsignedIntType>()
.AddType<InstantType>()
.AddMutationConventions(applyToAllMutations: true)
.AddFiltering(opt => opt.AddDefaults().BindRuntimeType<uint, UnsignedIntOperationFilterInputType>())
.AddSorting()
.AddProjections();
}
}

View File

@@ -7,19 +7,32 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="GraphQL.Server.Ui.GraphiQL" Version="8.3.3" />
<PackageReference Include="HotChocolate.AspNetCore" Version="15.1.11" />
<PackageReference Include="HotChocolate.Data.EntityFramework" Version="15.1.11" />
<PackageReference Include="HotChocolate.Types.Scalars" Version="15.1.11" />
<PackageReference Include="HotChocolate.Types.NodaTime" Version="15.1.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.11">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="9.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.11">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime" Version="9.0.4" />
<PackageReference Include="RabbitMQ.Client" Version="7.2.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FictionArchive.Common\FictionArchive.Common.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Services\Messaging\Interfaces\" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,11 @@
using FictionArchive.Service.Shared.Models.Interfaces;
using NodaTime;
namespace FictionArchive.Service.Shared.Models;
public abstract class BaseEntity<TKey> : IAuditable
{
public uint Id { get; set; }
public Instant CreatedTime { get; set; }
public Instant LastUpdatedTime { get; set; }
}

View File

@@ -0,0 +1,9 @@
using NodaTime;
namespace FictionArchive.Service.Shared.Models.Interfaces;
public interface IAuditable
{
public Instant CreatedTime { get; set; }
public Instant LastUpdatedTime { get; set; }
}

View File

@@ -0,0 +1,38 @@
using FictionArchive.Service.Shared.Models.Interfaces;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using NodaTime;
namespace FictionArchive.Service.Shared.Services.Database;
public class AuditInterceptor : SaveChangesInterceptor
{
public override InterceptionResult<int> SavingChanges(
DbContextEventData eventData,
InterceptionResult<int> result)
{
var context = eventData.Context;
if (context == null)
return base.SavingChanges(eventData, result);
var entries = context.ChangeTracker.Entries<IAuditable>();
var now = Instant.FromDateTimeUtc(DateTime.UtcNow);
foreach (var e in entries)
{
if (e.State == EntityState.Added)
{
e.Entity.CreatedTime = now;
e.Entity.LastUpdatedTime = now;
}
else if (e.State == EntityState.Modified)
{
e.Entity.LastUpdatedTime = now;
}
}
return base.SavingChanges(eventData, result);
}
}

View File

@@ -0,0 +1,41 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace FictionArchive.Service.Shared.Services.Database;
/// <summary>
/// Abstract DbContext handling boilerplate shared between our contexts. Should not share actual data.
/// </summary>
public abstract class FictionArchiveDbContext : DbContext
{
protected readonly ILogger _logger;
protected FictionArchiveDbContext(DbContextOptions options, ILogger logger) : base(options)
{
_logger = logger;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.AddInterceptors(new AuditInterceptor());
base.OnConfiguring(optionsBuilder);
}
public void UpdateDatabase()
{
IEnumerable<string> pendingMigrations = Database.GetPendingMigrations();
if (!pendingMigrations.Any())
{
_logger.LogDebug("No pending migrations found, continuing.");
return;
}
foreach (string migration in pendingMigrations)
{
_logger.LogInformation("Found pending migration with name {migrationName}.", migration);
}
_logger.LogInformation("Attempting to apply pending migrations...");
Database.Migrate();
_logger.LogInformation("Migrations applied.");
}
}

View File

@@ -1,17 +0,0 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace FictionArchive.Service.Shared.Services;
/// <summary>
/// Abstract DbContext handling boilerplate shared between our contexts. Should not share actual data.
/// </summary>
public abstract class FictionArchiveDbContext : DbContext
{
protected readonly ILogger _logger;
protected FictionArchiveDbContext(DbContextOptions options, ILogger logger) : base(options)
{
_logger = logger;
}
}