mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
refactor(dir): ♻ make IsApproved non-nullable
This commit is contained in:
parent
6e8b16dcc4
commit
5ec338f357
17 changed files with 57 additions and 26 deletions
|
|
@ -5,7 +5,8 @@ namespace FilterLists.Directory.Infrastructure.Persistence.Queries.Entities;
|
|||
|
||||
public abstract record AggregateRoot
|
||||
{
|
||||
public bool? IsApproved { get; private init; }
|
||||
// TODO: change 'set' to 'private init' when no longer seeding from json
|
||||
public bool IsApproved { get; set; }
|
||||
}
|
||||
|
||||
internal abstract class AggregateRootTypeConfiguration<TAggregateRoot> : IEntityTypeConfiguration<TAggregateRoot>
|
||||
|
|
@ -13,9 +14,6 @@ internal abstract class AggregateRootTypeConfiguration<TAggregateRoot> : IEntity
|
|||
{
|
||||
public virtual void Configure(EntityTypeBuilder<TAggregateRoot> builder)
|
||||
{
|
||||
builder.Property(e => e.IsApproved)
|
||||
.IsRequired()
|
||||
.HasDefaultValue(true); // legacy json data approved via GitHub PR
|
||||
builder.HasQueryFilter(e => e.IsApproved == true);
|
||||
builder.HasQueryFilter(e => e.IsApproved);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ public virtual void Configure(EntityTypeBuilder<Dependent> builder)
|
|||
.WithMany(fl => fl.DependencyFilterLists)
|
||||
.HasForeignKey(d => d.DependentFilterListId)
|
||||
.HasConstraintName("fk_dependents_filter_lists_dependent_filter_list_id");
|
||||
builder.HasQueryFilter(d => d.DependencyFilterList.IsApproved == true && d.DependentFilterList.IsApproved == true);
|
||||
builder.HasQueryFilter(d => d.DependencyFilterList.IsApproved && d.DependentFilterList.IsApproved);
|
||||
builder.HasDataJsonFile<Dependent>();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ public override void Configure(EntityTypeBuilder<FilterList> builder)
|
|||
builder.HasOne(f => f.License)
|
||||
.WithMany(l => l.FilterLists)
|
||||
.OnDelete(DeleteBehavior.Restrict);
|
||||
builder.HasDataJsonFile<FilterList>();
|
||||
builder.HasDataJsonFileAggregate<FilterList>();
|
||||
base.Configure(builder);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ public virtual void Configure(EntityTypeBuilder<FilterListLanguage> builder)
|
|||
.WithMany(l => l.FilterListLanguages)
|
||||
.HasForeignKey(fll => fll.Iso6391)
|
||||
.HasConstraintName("fk_filter_list_languages_languages_iso6391");
|
||||
builder.HasQueryFilter(fll => fll.FilterList.IsApproved == true && fll.Language.IsApproved == true);
|
||||
builder.HasQueryFilter(fll => fll.FilterList.IsApproved && fll.Language.IsApproved);
|
||||
builder.HasDataJsonFile<FilterListLanguage>();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ public virtual void Configure(EntityTypeBuilder<FilterListMaintainer> builder)
|
|||
|
||||
builder.ToTable($"{nr.RewriteName(nameof(FilterListMaintainer))}s");
|
||||
builder.HasKey(flm => new { flm.FilterListId, flm.MaintainerId });
|
||||
builder.HasQueryFilter(flm => flm.FilterList.IsApproved == true && flm.Maintainer.IsApproved == true);
|
||||
builder.HasQueryFilter(flm => flm.FilterList.IsApproved && flm.Maintainer.IsApproved);
|
||||
builder.HasDataJsonFile<FilterListMaintainer>();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ public virtual void Configure(EntityTypeBuilder<FilterListSyntax> builder)
|
|||
|
||||
builder.ToTable($"{nr.RewriteName(nameof(FilterListSyntax))}es");
|
||||
builder.HasKey(fls => new { fls.FilterListId, fls.SyntaxId });
|
||||
builder.HasQueryFilter(fls => fls.FilterList.IsApproved == true && fls.Syntax.IsApproved == true);
|
||||
builder.HasQueryFilter(fls => fls.FilterList.IsApproved && fls.Syntax.IsApproved);
|
||||
builder.HasDataJsonFile<FilterListSyntax>();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ public virtual void Configure(EntityTypeBuilder<FilterListTag> builder)
|
|||
|
||||
builder.ToTable($"{nr.RewriteName(nameof(FilterListTag))}s");
|
||||
builder.HasKey(flt => new { flt.FilterListId, flt.TagId });
|
||||
builder.HasQueryFilter(flt => flt.FilterList.IsApproved == true && flt.Tag.IsApproved == true);
|
||||
builder.HasQueryFilter(flt => flt.FilterList.IsApproved && flt.Tag.IsApproved);
|
||||
builder.HasDataJsonFile<FilterListTag>();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ public virtual void Configure(EntityTypeBuilder<Fork> builder)
|
|||
.WithMany(fl => fl.UpstreamFilterLists)
|
||||
.HasForeignKey(f => f.ForkFilterListId)
|
||||
.HasConstraintName("fk_forks_filter_lists_fork_filter_list_id");
|
||||
builder.HasQueryFilter(f => f.UpstreamFilterList.IsApproved == true && f.ForkFilterList.IsApproved == true);
|
||||
builder.HasQueryFilter(f => f.UpstreamFilterList.IsApproved && f.ForkFilterList.IsApproved);
|
||||
builder.HasDataJsonFile<Fork>();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ public override void Configure(EntityTypeBuilder<Language> builder)
|
|||
.HasMaxLength(2);
|
||||
builder.HasIndex(l => l.Name)
|
||||
.IsUnique();
|
||||
builder.HasDataJsonFile<Language>();
|
||||
builder.HasDataJsonFileAggregate<Language>();
|
||||
base.Configure(builder);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ public override void Configure(EntityTypeBuilder<License> builder)
|
|||
.HasDefaultValue(false);
|
||||
builder.Property(l => l.PermitsCommercialUse)
|
||||
.HasDefaultValue(false);
|
||||
builder.HasDataJsonFile<License>();
|
||||
builder.HasDataJsonFileAggregate<License>();
|
||||
base.Configure(builder);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ public override void Configure(EntityTypeBuilder<Maintainer> builder)
|
|||
{
|
||||
builder.HasIndex(m => m.Name)
|
||||
.IsUnique();
|
||||
builder.HasDataJsonFile<Maintainer>();
|
||||
builder.HasDataJsonFileAggregate<Maintainer>();
|
||||
base.Configure(builder);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ public virtual void Configure(EntityTypeBuilder<Merge> builder)
|
|||
.WithMany(fl => fl.IncludedInFilterLists)
|
||||
.HasForeignKey(m => m.IncludesFilterListId)
|
||||
.HasConstraintName("fk_merges_filter_lists_includes_filter_list_id");
|
||||
builder.HasQueryFilter(m => m.IncludedInFilterList.IsApproved == true && m.IncludesFilterList.IsApproved == true);
|
||||
builder.HasQueryFilter(m => m.IncludedInFilterList.IsApproved && m.IncludesFilterList.IsApproved);
|
||||
builder.HasDataJsonFile<Merge>();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ public override void Configure(EntityTypeBuilder<Software> builder)
|
|||
.IsUnique();
|
||||
builder.Property(s => s.SupportsAbpUrlScheme)
|
||||
.HasDefaultValue(false);
|
||||
builder.HasDataJsonFile<Software>();
|
||||
builder.HasDataJsonFileAggregate<Software>();
|
||||
base.Configure(builder);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ public virtual void Configure(EntityTypeBuilder<SoftwareSyntax> builder)
|
|||
|
||||
builder.ToTable($"{nr.RewriteName(nameof(SoftwareSyntax))}es");
|
||||
builder.HasKey(ss => new { ss.SoftwareId, ss.SyntaxId });
|
||||
builder.HasQueryFilter(ss => ss.Software.IsApproved == true && ss.Syntax.IsApproved == true);
|
||||
builder.HasQueryFilter(ss => ss.Software.IsApproved && ss.Syntax.IsApproved);
|
||||
builder.HasDataJsonFile<SoftwareSyntax>();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ public override void Configure(EntityTypeBuilder<Syntax> builder)
|
|||
{
|
||||
builder.HasIndex(s => s.Name)
|
||||
.IsUnique();
|
||||
builder.HasDataJsonFile<Syntax>();
|
||||
builder.HasDataJsonFileAggregate<Syntax>();
|
||||
base.Configure(builder);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ public override void Configure(EntityTypeBuilder<Tag> builder)
|
|||
{
|
||||
builder.HasIndex(t => t.Name)
|
||||
.IsUnique();
|
||||
builder.HasDataJsonFile<Tag>();
|
||||
builder.HasDataJsonFileAggregate<Tag>();
|
||||
base.Configure(builder);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
using System.Text.Json;
|
||||
using FilterLists.Directory.Infrastructure.Persistence.Queries.Context;
|
||||
using FilterLists.Directory.Infrastructure.Persistence.Queries.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
|
@ -19,21 +20,53 @@ public static async Task MigrateAsync(this IHost host)
|
|||
|
||||
internal static class SeedConfigurationExtension
|
||||
{
|
||||
public static void HasDataJsonFile<TEntity>(this EntityTypeBuilder entityTypeBuilder)
|
||||
public static void HasDataJsonFileAggregate<TEntity>(this EntityTypeBuilder entityTypeBuilder)
|
||||
where TEntity : AggregateRoot
|
||||
{
|
||||
var entities = Deserialize<TEntity>();
|
||||
if (entities.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var entity in entities)
|
||||
{
|
||||
entity.Approve();
|
||||
}
|
||||
|
||||
entityTypeBuilder.HasData(entities);
|
||||
}
|
||||
|
||||
public static void HasDataJsonFile<TEntity>(this EntityTypeBuilder entityTypeBuilder) where TEntity : class
|
||||
{
|
||||
var entities = Deserialize<TEntity>();
|
||||
if (entities.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
entityTypeBuilder.HasData(entities);
|
||||
}
|
||||
|
||||
private static List<TEntity> Deserialize<TEntity>()
|
||||
{
|
||||
// uncomment to short-circuit HasData() when adding a migration
|
||||
return new List<TEntity>();
|
||||
|
||||
var path = Path.Combine("../data", $"{typeof(TEntity).Name}.json");
|
||||
if (!File.Exists(path))
|
||||
{
|
||||
return;
|
||||
return new List<TEntity>();
|
||||
}
|
||||
|
||||
var entitiesJson = File.ReadAllText(path);
|
||||
var entities = JsonSerializer.Deserialize<IEnumerable<TEntity>>(entitiesJson,
|
||||
new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
|
||||
return entities is null ? new List<TEntity>() : entities.ToList();
|
||||
}
|
||||
|
||||
if (entities != null)
|
||||
{
|
||||
entityTypeBuilder.HasData((IEnumerable<object>)entities);
|
||||
}
|
||||
private static void Approve<TAggregateRoot>(this TAggregateRoot entity) where TAggregateRoot : AggregateRoot
|
||||
{
|
||||
entity.IsApproved = true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue