Updated translation service and finished splitting out responsibilities for now
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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.");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user