diff --git a/FictionArchive.Service.Shared/Extensions/GraphQLExtensions.cs b/FictionArchive.Service.Shared/Extensions/GraphQLExtensions.cs index ccd1608..3ab030c 100644 --- a/FictionArchive.Service.Shared/Extensions/GraphQLExtensions.cs +++ b/FictionArchive.Service.Shared/Extensions/GraphQLExtensions.cs @@ -12,6 +12,7 @@ public static class GraphQLExtensions return services.AddGraphQLServer() .AddQueryType() .AddMutationType() + .AddDiagnosticEventListener() .AddType() .AddType() .AddMutationConventions(applyToAllMutations: true) diff --git a/FictionArchive.Service.Shared/Services/Database/AuditInterceptor.cs b/FictionArchive.Service.Shared/Services/Database/AuditInterceptor.cs index 4e6fa70..a542fa5 100644 --- a/FictionArchive.Service.Shared/Services/Database/AuditInterceptor.cs +++ b/FictionArchive.Service.Shared/Services/Database/AuditInterceptor.cs @@ -12,9 +12,24 @@ public class AuditInterceptor : SaveChangesInterceptor InterceptionResult result) { var context = eventData.Context; + SetAuditFields(context); + + return base.SavingChanges(eventData, result); + } + + public override ValueTask> SavingChangesAsync(DbContextEventData eventData, InterceptionResult result, + CancellationToken cancellationToken = new CancellationToken()) + { + var context = eventData.Context; + SetAuditFields(context); + return base.SavingChangesAsync(eventData, result, cancellationToken); + } + + private void SetAuditFields(DbContext? context) + { if (context == null) - return base.SavingChanges(eventData, result); + return; var entries = context.ChangeTracker.Entries(); @@ -32,7 +47,5 @@ public class AuditInterceptor : SaveChangesInterceptor e.Entity.LastUpdatedTime = now; } } - - return base.SavingChanges(eventData, result); } } diff --git a/FictionArchive.Service.Shared/Services/Database/FictionArchiveDbContext.cs b/FictionArchive.Service.Shared/Services/Database/FictionArchiveDbContext.cs index 4c6b1b2..21214da 100644 --- a/FictionArchive.Service.Shared/Services/Database/FictionArchiveDbContext.cs +++ b/FictionArchive.Service.Shared/Services/Database/FictionArchiveDbContext.cs @@ -18,7 +18,6 @@ public abstract class FictionArchiveDbContext : DbContext protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.AddInterceptors(new AuditInterceptor()); - base.OnConfiguring(optionsBuilder); } public void UpdateDatabase() diff --git a/FictionArchive.Service.Shared/Services/GraphQL/ErrorEventListener.cs b/FictionArchive.Service.Shared/Services/GraphQL/ErrorEventListener.cs new file mode 100644 index 0000000..0c00c6d --- /dev/null +++ b/FictionArchive.Service.Shared/Services/GraphQL/ErrorEventListener.cs @@ -0,0 +1,20 @@ +using HotChocolate.Execution; +using HotChocolate.Execution.Instrumentation; +using Microsoft.Extensions.Logging; + +namespace FictionArchive.Service.Shared.Services.GraphQL; + +public class ErrorEventListener : ExecutionDiagnosticEventListener +{ + private readonly ILogger _logger; + + public ErrorEventListener(ILogger logger) + { + _logger = logger; + } + + public override void RequestError(IRequestContext context, Exception exception) + { + _logger.LogError(exception, "An error occurred while processing a GraphQL request."); + } +} \ No newline at end of file diff --git a/FictionArchive.Service.TranslationService/GraphQL/Query.cs b/FictionArchive.Service.TranslationService/GraphQL/Query.cs index bab091a..2b1c9f3 100644 --- a/FictionArchive.Service.TranslationService/GraphQL/Query.cs +++ b/FictionArchive.Service.TranslationService/GraphQL/Query.cs @@ -1,5 +1,8 @@ using FictionArchive.Service.TranslationService.Models; +using FictionArchive.Service.TranslationService.Models.Database; +using FictionArchive.Service.TranslationService.Services.Database; using FictionArchive.Service.TranslationService.Services.TranslationEngines; +using Microsoft.EntityFrameworkCore; namespace FictionArchive.Service.TranslationService.GraphQL; @@ -11,4 +14,13 @@ public class Query { return engines.Select(engine => engine.Descriptor); } + + [UsePaging] + [UseProjection] + [UseFiltering] + [UseSorting] + public IQueryable GetTranslationRequests(TranslationServiceDbContext dbContext) + { + return dbContext.TranslationRequests.AsQueryable(); + } } \ No newline at end of file diff --git a/FictionArchive.Service.TranslationService/Program.cs b/FictionArchive.Service.TranslationService/Program.cs index 73763d5..12c1e31 100644 --- a/FictionArchive.Service.TranslationService/Program.cs +++ b/FictionArchive.Service.TranslationService/Program.cs @@ -41,6 +41,13 @@ public class Program #endregion var app = builder.Build(); + + // Update database + using (var scope = app.Services.CreateScope()) + { + var dbContext = scope.ServiceProvider.GetRequiredService(); + dbContext.UpdateDatabase(); + } app.UseHttpsRedirection();