Skip to content

Commit

Permalink
feat(directory): ✨🚧 port forward data seeding logic
Browse files Browse the repository at this point in the history
  • Loading branch information
collinbarrett committed Aug 26, 2020
1 parent d413ce0 commit 973470e
Show file tree
Hide file tree
Showing 17 changed files with 139 additions and 32 deletions.
19 changes: 19 additions & 0 deletions services/FilterLists.sln
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
FilterLists.sln.DotSettings = FilterLists.sln.DotSettings
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Data", "Data", "{8F419098-3EEB-4B4B-99A8-70555FA93DAF}"
ProjectSection(SolutionItems) = preProject
directory\data\Dependent.json = directory\data\Dependent.json
directory\data\FilterList.json = directory\data\FilterList.json
directory\data\FilterListLanguage.json = directory\data\FilterListLanguage.json
directory\data\FilterListMaintainer.json = directory\data\FilterListMaintainer.json
directory\data\FilterListTag.json = directory\data\FilterListTag.json
directory\data\Fork.json = directory\data\Fork.json
directory\data\Language.json = directory\data\Language.json
directory\data\License.json = directory\data\License.json
directory\data\Maintainer.json = directory\data\Maintainer.json
directory\data\Merge.json = directory\data\Merge.json
directory\data\Software.json = directory\data\Software.json
directory\data\SoftwareSyntax.json = directory\data\SoftwareSyntax.json
directory\data\Syntax.json = directory\data\Syntax.json
directory\data\Tag.json = directory\data\Tag.json
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -63,6 +81,7 @@ Global
{CB1BCB34-C2A1-4E94-A90E-359FB4720B96} = {AE5D6471-1B6E-4B06-A313-34EF81F35342}
{74C798F8-892A-40F2-8D2E-395E83853771} = {AE5D6471-1B6E-4B06-A313-34EF81F35342}
{4F7D51FE-A1F3-4473-A8C3-D57EBDD8E60E} = {AE5D6471-1B6E-4B06-A313-34EF81F35342}
{8F419098-3EEB-4B4B-99A8-70555FA93DAF} = {AE5D6471-1B6E-4B06-A313-34EF81F35342}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {758B57EF-7505-4BE2-90A2-E2DE2EC32909}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@ internal class DependentTypeConfiguration : IEntityTypeConfiguration<Dependent>
public virtual void Configure(EntityTypeBuilder<Dependent> builder)
{
_ = builder ?? throw new ArgumentNullException(nameof(builder));

builder.ToTable(nameof(Dependent) + "s");

builder.HasKey(d => new {d.DependencyFilterListId, d.DependentFilterListId});
builder.HasOne(d => d.DependencyFilterList)
.WithMany(fl => fl.DependentFilterLists)
.HasForeignKey(d => d.DependencyFilterListId);
builder.HasOne(d => d.DependentFilterList)
.WithMany(fl => fl.DependencyFilterLists)
.HasForeignKey(d => d.DependentFilterListId);
builder.HasDataJsonFile<Dependent>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,23 @@ internal class FilterListTypeConfiguration : IEntityTypeConfiguration<FilterList
public virtual void Configure(EntityTypeBuilder<FilterList> builder)
{
_ = builder ?? throw new ArgumentNullException(nameof(builder));

builder.OwnsMany(fl => fl.SegmentViewUrls,
o =>
{
o.ToTable(nameof(SegmentViewUrl) + "s");
o.HasKey("Id");
o.HasIndex(nameof(FilterList) + "Id", nameof(SegmentViewUrl.Position)).IsUnique();
o.OwnsMany(p => p.SegmentViewUrlMirrors,
m =>
{
m.ToTable(nameof(SegmentViewUrlMirror) + "s");
m.Property<int>("Id");
m.HasKey("Id");
});
});
o =>
{
o.ToTable(nameof(SegmentViewUrl) + "s");
o.HasKey("Id");
o.HasIndex(nameof(FilterList) + "Id", nameof(SegmentViewUrl.Position)).IsUnique();
o.OwnsMany(p => p.SegmentViewUrlMirrors,
m =>
{
m.ToTable(nameof(SegmentViewUrlMirror) + "s");
m.Property<int>("Id");
m.HasKey("Id");
})
.HasDataJsonFile<SegmentViewUrlMirror>();
})
.HasDataJsonFile<SegmentViewUrl>();
builder.HasDataJsonFile<FilterList>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ internal class FilterListLanguageTypeConfiguration : IEntityTypeConfiguration<Fi
public virtual void Configure(EntityTypeBuilder<FilterListLanguage> builder)
{
_ = builder ?? throw new ArgumentNullException(nameof(builder));

builder.ToTable(nameof(FilterListLanguage) + "s");

builder.HasKey(fll => new {fll.FilterListId, fll.LanguageIso6391});
builder.HasDataJsonFile<FilterListLanguage>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ internal class FilterListMaintainerTypeConfiguration : IEntityTypeConfiguration<
public virtual void Configure(EntityTypeBuilder<FilterListMaintainer> builder)
{
_ = builder ?? throw new ArgumentNullException(nameof(builder));

builder.ToTable(nameof(FilterListMaintainer) + "s");

builder.HasKey(flm => new {flm.FilterListId, flm.MaintainerId});
builder.HasDataJsonFile<FilterListMaintainer>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ internal class FilterListSyntaxTypeConfiguration : IEntityTypeConfiguration<Filt
public virtual void Configure(EntityTypeBuilder<FilterListSyntax> builder)
{
_ = builder ?? throw new ArgumentNullException(nameof(builder));

builder.ToTable(nameof(FilterListSyntax) + "es");

builder.HasKey(fls => new {fls.FilterListId, fls.SyntaxId});
builder.HasDataJsonFile<FilterListSyntax>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ internal class FilterListTagTypeConfiguration : IEntityTypeConfiguration<FilterL
public virtual void Configure(EntityTypeBuilder<FilterListTag> builder)
{
_ = builder ?? throw new ArgumentNullException(nameof(builder));

builder.ToTable(nameof(FilterListTag) + "s");

builder.HasKey(flt => new {flt.FilterListId, flt.TagId});
builder.HasDataJsonFile<FilterListTag>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@ internal class ForkTypeConfiguration : IEntityTypeConfiguration<Fork>
public virtual void Configure(EntityTypeBuilder<Fork> builder)
{
_ = builder ?? throw new ArgumentNullException(nameof(builder));

builder.ToTable(nameof(Fork) + "s");

builder.HasKey(f => new {f.UpstreamFilterListId, f.ForkFilterListId});
builder.HasOne(f => f.UpstreamFilterList)
.WithMany(fl => fl.ForkFilterLists)
.HasForeignKey(f => f.UpstreamFilterListId);
builder.HasOne(f => f.ForkFilterList)
.WithMany(fl => fl.UpstreamFilterLists)
.HasForeignKey(f => f.ForkFilterListId);
builder.HasDataJsonFile<Fork>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ internal class LanguageTypeConfiguration : IEntityTypeConfiguration<Language>
public virtual void Configure(EntityTypeBuilder<Language> builder)
{
_ = builder ?? throw new ArgumentNullException(nameof(builder));

builder.HasKey(l => l.Iso6391);
builder.HasDataJsonFile<Language>();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace FilterLists.Directory.Infrastructure.Persistence.Queries.Entities
{
Expand All @@ -11,4 +13,13 @@ public class License
public Uri? Url { get; private set; }
public IReadOnlyCollection<FilterList> FilterLists { get; private set; } = new HashSet<FilterList>();
}

internal class LicenseTypeConfiguration : IEntityTypeConfiguration<License>
{
public virtual void Configure(EntityTypeBuilder<License> builder)
{
_ = builder ?? throw new ArgumentNullException(nameof(builder));
builder.HasDataJsonFile<License>();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace FilterLists.Directory.Infrastructure.Persistence.Queries.Entities
{
Expand All @@ -12,4 +14,13 @@ public class Maintainer
public string? TwitterHandle { get; private set; }
public IReadOnlyCollection<FilterListMaintainer> FilterListMaintainers { get; private set; } = new HashSet<FilterListMaintainer>();
}

internal class MaintainerTypeConfiguration : IEntityTypeConfiguration<Maintainer>
{
public virtual void Configure(EntityTypeBuilder<Maintainer> builder)
{
_ = builder ?? throw new ArgumentNullException(nameof(builder));
builder.HasDataJsonFile<Maintainer>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@ internal class MergeTypeConfiguration : IEntityTypeConfiguration<Merge>
public virtual void Configure(EntityTypeBuilder<Merge> builder)
{
_ = builder ?? throw new ArgumentNullException(nameof(builder));

builder.ToTable(nameof(Merge) + "s");

builder.HasKey(m => new {m.IncludedInFilterListId, m.IncludesFilterListId});
builder.HasOne(m => m.IncludedInFilterList)
.WithMany(fl => fl.IncludesFilterLists)
.HasForeignKey(m => m.IncludedInFilterListId);
builder.HasOne(m => m.IncludesFilterList)
.WithMany(fl => fl.IncludedInFilterLists)
.HasForeignKey(m => m.IncludesFilterListId);
builder.HasDataJsonFile<Merge>();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace FilterLists.Directory.Infrastructure.Persistence.Queries.Entities
{
Expand All @@ -13,4 +15,13 @@ public class Software
public bool SupportsAbpUrlScheme { get; private set; }
public IReadOnlyCollection<SoftwareSyntax> SoftwareSyntaxes { get; private set; } = new HashSet<SoftwareSyntax>();
}

internal class SoftwareTypeConfiguration : IEntityTypeConfiguration<Software>
{
public virtual void Configure(EntityTypeBuilder<Software> builder)
{
_ = builder ?? throw new ArgumentNullException(nameof(builder));
builder.HasDataJsonFile<Software>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ internal class SoftwareSyntaxTypeConfiguration : IEntityTypeConfiguration<Softwa
public virtual void Configure(EntityTypeBuilder<SoftwareSyntax> builder)
{
_ = builder ?? throw new ArgumentNullException(nameof(builder));

builder.ToTable(nameof(SoftwareSyntax) + "es");

builder.HasKey(ss => new {ss.SoftwareId, ss.SyntaxId});
builder.HasDataJsonFile<SoftwareSyntax>();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace FilterLists.Directory.Infrastructure.Persistence.Queries.Entities
{
Expand All @@ -12,4 +14,13 @@ public class Syntax
public IReadOnlyCollection<FilterListSyntax> FilterListSyntaxes { get; private set; } = new HashSet<FilterListSyntax>();
public IReadOnlyCollection<SoftwareSyntax> SoftwareSyntaxes { get; private set; } = new HashSet<SoftwareSyntax>();
}

internal class SyntaxTypeConfiguration : IEntityTypeConfiguration<Syntax>
{
public virtual void Configure(EntityTypeBuilder<Syntax> builder)
{
_ = builder ?? throw new ArgumentNullException(nameof(builder));
builder.HasDataJsonFile<Syntax>();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace FilterLists.Directory.Infrastructure.Persistence.Queries.Entities
{
Expand All @@ -9,4 +12,13 @@ public class Tag
public string? Description { get; private set; }
public IReadOnlyCollection<FilterListTag> FilterListTags { get; private set; } = new HashSet<FilterListTag>();
}

internal class TagTypeConfiguration : IEntityTypeConfiguration<Tag>
{
public virtual void Configure(EntityTypeBuilder<Tag> builder)
{
_ = builder ?? throw new ArgumentNullException(nameof(builder));
builder.HasDataJsonFile<Tag>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace FilterLists.Directory.Infrastructure.Persistence
{
internal static class SeedExtensions
{
public static void HasDataJsonFile<TEntity>(this EntityTypeBuilder entityTypeBuilder)
{
_ = entityTypeBuilder ?? throw new ArgumentNullException(nameof(entityTypeBuilder));
var entities = GetEntities<TEntity>();
entityTypeBuilder.HasData((IEnumerable<object>)entities);
}

public static void HasDataJsonFile<TEntity>(this OwnedNavigationBuilder ownedNavigationBuilder)
{
_ = ownedNavigationBuilder ?? throw new ArgumentNullException(nameof(ownedNavigationBuilder));
var entities = GetEntities<TEntity>();
ownedNavigationBuilder.HasData((IEnumerable<object>)entities);
}

private static IEnumerable<TEntity> GetEntities<TEntity>()
{
var path = Path.Combine("../../../data", $"{typeof(TEntity).Name}.json");
if (!File.Exists(path)) return new HashSet<TEntity>();

var entitiesJson = File.ReadAllText(path);
var entities = JsonSerializer.Deserialize<IEnumerable<TEntity>>(entitiesJson, new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
});
return entities;
}
}
}

0 comments on commit 973470e

Please sign in to comment.