refactor(directory): ♻ rm unused seeding extension, temp disable seeding for broken type

This commit is contained in:
Collin M. Barrett 2020-08-27 17:16:37 -05:00
parent bef1761bb8
commit b58a5d9d9b

View file

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using FilterLists.Directory.Infrastructure.Persistence.Queries.Entities;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace FilterLists.Directory.Infrastructure.Persistence
@ -11,28 +12,21 @@ 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);
}
// TODO: fix seeding FilterListViewUrl
if (typeof(TEntity).Name == nameof(FilterListViewUrl))
return;
private static IEnumerable<TEntity> GetEntities<TEntity>()
{
var path = Path.Combine("../../data", $"{typeof(TEntity).Name}.json");
if (!File.Exists(path)) return new HashSet<TEntity>();
if (!File.Exists(path)) return;
var entitiesJson = File.ReadAllText(path);
var entities = JsonSerializer.Deserialize<IEnumerable<TEntity>>(entitiesJson, new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
});
return entities;
entityTypeBuilder.HasData((IEnumerable<object>)entities);
}
}
}