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()
|
||||
.AddQueryType<TQuery>()
|
||||
.AddMutationType<TMutation>()
|
||||
.AddDiagnosticEventListener<ErrorEventListener>()
|
||||
.AddType<UnsignedIntType>()
|
||||
.AddType<InstantType>()
|
||||
.AddMutationConventions(applyToAllMutations: true)
|
||||
|
||||
@@ -12,9 +12,24 @@ public class AuditInterceptor : SaveChangesInterceptor
|
||||
InterceptionResult<int> result)
|
||||
{
|
||||
var context = eventData.Context;
|
||||
SetAuditFields(context);
|
||||
|
||||
|
||||
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 base.SavingChanges(eventData, result);
|
||||
return;
|
||||
|
||||
var entries = context.ChangeTracker.Entries<IAuditable>();
|
||||
|
||||
@@ -32,7 +47,5 @@ public class AuditInterceptor : SaveChangesInterceptor
|
||||
e.Entity.LastUpdatedTime = now;
|
||||
}
|
||||
}
|
||||
|
||||
return base.SavingChanges(eventData, result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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.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<TranslationRequest> GetTranslationRequests(TranslationServiceDbContext dbContext)
|
||||
{
|
||||
return dbContext.TranslationRequests.AsQueryable();
|
||||
}
|
||||
}
|
||||
@@ -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<TranslationServiceDbContext>();
|
||||
dbContext.UpdateDatabase();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user