Fixed AuditInterceptor.cs , add GraphQL error logging, add translation request get
This commit is contained in:
@@ -12,6 +12,7 @@ public static class GraphQLExtensions
|
|||||||
return services.AddGraphQLServer()
|
return services.AddGraphQLServer()
|
||||||
.AddQueryType<TQuery>()
|
.AddQueryType<TQuery>()
|
||||||
.AddMutationType<TMutation>()
|
.AddMutationType<TMutation>()
|
||||||
|
.AddDiagnosticEventListener<ErrorEventListener>()
|
||||||
.AddType<UnsignedIntType>()
|
.AddType<UnsignedIntType>()
|
||||||
.AddType<InstantType>()
|
.AddType<InstantType>()
|
||||||
.AddMutationConventions(applyToAllMutations: true)
|
.AddMutationConventions(applyToAllMutations: true)
|
||||||
|
|||||||
@@ -12,9 +12,24 @@ public class AuditInterceptor : SaveChangesInterceptor
|
|||||||
InterceptionResult<int> result)
|
InterceptionResult<int> result)
|
||||||
{
|
{
|
||||||
var context = eventData.Context;
|
var context = eventData.Context;
|
||||||
|
SetAuditFields(context);
|
||||||
|
|
||||||
|
|
||||||
if (context == null)
|
|
||||||
return base.SavingChanges(eventData, result);
|
return base.SavingChanges(eventData, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override ValueTask<InterceptionResult<int>> SavingChangesAsync(DbContextEventData eventData, InterceptionResult<int> 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;
|
||||||
|
|
||||||
var entries = context.ChangeTracker.Entries<IAuditable>();
|
var entries = context.ChangeTracker.Entries<IAuditable>();
|
||||||
|
|
||||||
@@ -32,7 +47,5 @@ public class AuditInterceptor : SaveChangesInterceptor
|
|||||||
e.Entity.LastUpdatedTime = now;
|
e.Entity.LastUpdatedTime = now;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return base.SavingChanges(eventData, result);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ public abstract class FictionArchiveDbContext : DbContext
|
|||||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||||
{
|
{
|
||||||
optionsBuilder.AddInterceptors(new AuditInterceptor());
|
optionsBuilder.AddInterceptors(new AuditInterceptor());
|
||||||
base.OnConfiguring(optionsBuilder);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateDatabase()
|
public void UpdateDatabase()
|
||||||
|
|||||||
@@ -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<ErrorEventListener> _logger;
|
||||||
|
|
||||||
|
public ErrorEventListener(ILogger<ErrorEventListener> logger)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void RequestError(IRequestContext context, Exception exception)
|
||||||
|
{
|
||||||
|
_logger.LogError(exception, "An error occurred while processing a GraphQL request.");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,8 @@
|
|||||||
using FictionArchive.Service.TranslationService.Models;
|
using FictionArchive.Service.TranslationService.Models;
|
||||||
|
using FictionArchive.Service.TranslationService.Models.Database;
|
||||||
|
using FictionArchive.Service.TranslationService.Services.Database;
|
||||||
using FictionArchive.Service.TranslationService.Services.TranslationEngines;
|
using FictionArchive.Service.TranslationService.Services.TranslationEngines;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
namespace FictionArchive.Service.TranslationService.GraphQL;
|
namespace FictionArchive.Service.TranslationService.GraphQL;
|
||||||
|
|
||||||
@@ -11,4 +14,13 @@ public class Query
|
|||||||
{
|
{
|
||||||
return engines.Select(engine => engine.Descriptor);
|
return engines.Select(engine => engine.Descriptor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[UsePaging]
|
||||||
|
[UseProjection]
|
||||||
|
[UseFiltering]
|
||||||
|
[UseSorting]
|
||||||
|
public IQueryable<TranslationRequest> GetTranslationRequests(TranslationServiceDbContext dbContext)
|
||||||
|
{
|
||||||
|
return dbContext.TranslationRequests.AsQueryable();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -42,6 +42,13 @@ public class Program
|
|||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
|
// Update database
|
||||||
|
using (var scope = app.Services.CreateScope())
|
||||||
|
{
|
||||||
|
var dbContext = scope.ServiceProvider.GetRequiredService<TranslationServiceDbContext>();
|
||||||
|
dbContext.UpdateDatabase();
|
||||||
|
}
|
||||||
|
|
||||||
app.UseHttpsRedirection();
|
app.UseHttpsRedirection();
|
||||||
|
|
||||||
app.MapHealthChecks("/healthz");
|
app.MapHealthChecks("/healthz");
|
||||||
|
|||||||
Reference in New Issue
Block a user