feat(dir): scaffold Change entity

This commit is contained in:
Collin M. Barrett 2021-11-03 06:04:48 -05:00
parent 0c48ff3258
commit 94dab0d15c
5 changed files with 62 additions and 0 deletions

View file

@ -4,6 +4,7 @@ namespace FilterLists.Directory.Infrastructure.Persistence.Queries.Context;
public interface IQueryContext
{
IQueryable<Change> Changes { get; }
IQueryable<FilterList> FilterLists { get; }
IQueryable<Language> Languages { get; }
IQueryable<License> Licenses { get; }

View file

@ -22,6 +22,7 @@ void IDisposable.Dispose()
_dbContext.Dispose();
}
public IQueryable<Change> Changes => _dbContext.Changes.AsNoTracking();
public IQueryable<FilterList> FilterLists => _dbContext.FilterLists.AsNoTracking();
public IQueryable<Language> Languages => _dbContext.Languages.AsNoTracking();
public IQueryable<License> Licenses => _dbContext.Licenses.AsNoTracking();

View file

@ -1,14 +1,22 @@
using FilterLists.Directory.Infrastructure.Persistence.Queries.Entities;
using Microsoft.EntityFrameworkCore;
using Npgsql;
namespace FilterLists.Directory.Infrastructure.Persistence.Queries.Context;
public class QueryDbContext : DbContext
{
static QueryDbContext()
{
NpgsqlConnection.GlobalTypeMapper.MapEnum<ChangeType>();
NpgsqlConnection.GlobalTypeMapper.MapEnum<AggregateRoot>();
}
public QueryDbContext(DbContextOptions<QueryDbContext> options) : base(options)
{
}
public DbSet<Change> Changes => Set<Change>();
public DbSet<FilterList> FilterLists => Set<FilterList>();
public DbSet<Language> Languages => Set<Language>();
public DbSet<License> Licenses => Set<License>();
@ -32,5 +40,7 @@ public override Task<int> SaveChangesAsync(
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfigurationsFromAssembly(GetType().Assembly);
modelBuilder.HasPostgresEnum<ChangeType>();
modelBuilder.HasPostgresEnum<AggregateRoot>();
}
}

View file

@ -0,0 +1,45 @@
using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace FilterLists.Directory.Infrastructure.Persistence.Queries.Entities;
public class Change
{
public int Id { get; init; }
public ChangeType Type { get; init; }
public AggregateRoot AggregateRoot { get; init; }
public int AggregateRootId { get; init; }
public JsonDocument? Aggregate { get; init; }
public DateTime CreatedAt { get; init; }
public DateTime? AppliedAt { get; init; }
public DateTime? RejectedAt { get; init; }
public string? RejectedReason { get; init; }
}
public enum ChangeType
{
Insert,
Update,
Delete
}
public enum AggregateRoot
{
FilterList,
Language,
License,
Maintainer,
Software,
Syntax,
Tag
}
internal class UpdateConfiguration : IEntityTypeConfiguration<Change>
{
public virtual void Configure(EntityTypeBuilder<Change> builder)
{
builder.Property(c => c.CreatedAt)
.HasDefaultValueSql("now()");
}
}

View file

@ -5,6 +5,7 @@
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Npgsql;
namespace FilterLists.Directory.Infrastructure.Persistence;
@ -15,6 +16,10 @@ public static async Task MigrateAsync(this IHost host)
using var scope = host.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<QueryDbContext>();
await db.Database.MigrateAsync();
await using var conn = (NpgsqlConnection)db.Database.GetDbConnection();
await conn.OpenAsync();
conn.ReloadTypes();
}
}