mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
use generic Seed() method
closes #183 per https://stackoverflow.com/questions/48330079/check-if-dbsett-is-empty
This commit is contained in:
parent
56d4c51ed3
commit
2355cba86b
1 changed files with 17 additions and 127 deletions
|
|
@ -33,142 +33,32 @@ public static void EnsureSeeded(this FilterListsDbContext context)
|
|||
SeedJunctions(context);
|
||||
}
|
||||
|
||||
private static void SeedEntities(FilterListsDbContext context)
|
||||
private static void SeedEntities(DbContext context)
|
||||
{
|
||||
SeedLanguages(context);
|
||||
SeedLicenses(context);
|
||||
SeedMaintainers(context);
|
||||
SeedSoftware(context);
|
||||
SeedSyntaxes(context);
|
||||
SeedFilterLists(context);
|
||||
Seed<Language>(context);
|
||||
Seed<License>(context);
|
||||
Seed<Maintainer>(context);
|
||||
Seed<Software>(context);
|
||||
Seed<Syntax>(context);
|
||||
Seed<FilterList>(context);
|
||||
}
|
||||
|
||||
private static void SeedJunctions(FilterListsDbContext context)
|
||||
private static void SeedJunctions(DbContext context)
|
||||
{
|
||||
SeedFilterListLanguages(context);
|
||||
SeedFilterListMaintainers(context);
|
||||
SeedForks(context);
|
||||
SeedMerges(context);
|
||||
SeedSoftwareSyntaxes(context);
|
||||
Seed<FilterListLanguage>(context);
|
||||
Seed<FilterListMaintainer>(context);
|
||||
Seed<Fork>(context);
|
||||
Seed<Merge>(context);
|
||||
Seed<SoftwareSyntax>(context);
|
||||
}
|
||||
|
||||
//TODO: fix generic method to remove duplication of entity-specific methods
|
||||
private static void Seed<T>(DbContext context)
|
||||
private static void Seed<T>(DbContext dbContext) where T : class
|
||||
{
|
||||
//if (context.?.Any()) return;
|
||||
if (dbContext.Set<T>().Any()) return;
|
||||
var rows = JsonConvert.DeserializeObject<List<T>>(
|
||||
File.ReadAllText(SeedDirectory + Path.DirectorySeparatorChar + typeof(T).Name + ".json"));
|
||||
context.AddRange(rows);
|
||||
context.SaveChanges();
|
||||
dbContext.AddRange(rows);
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
#region Entities
|
||||
|
||||
private static void SeedFilterLists(FilterListsDbContext context)
|
||||
{
|
||||
if (context.FilterLists.Any()) return;
|
||||
var types = JsonConvert.DeserializeObject<List<FilterList>>(
|
||||
File.ReadAllText(SeedDirectory + Path.DirectorySeparatorChar + typeof(FilterList).Name + ".json"));
|
||||
context.AddRange(types);
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
private static void SeedLanguages(FilterListsDbContext context)
|
||||
{
|
||||
if (context.Languages.Any()) return;
|
||||
var types = JsonConvert.DeserializeObject<List<Language>>(
|
||||
File.ReadAllText(SeedDirectory + Path.DirectorySeparatorChar + typeof(Language).Name + ".json"));
|
||||
context.AddRange(types);
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
private static void SeedLicenses(FilterListsDbContext context)
|
||||
{
|
||||
if (context.Licenses.Any()) return;
|
||||
var types = JsonConvert.DeserializeObject<List<License>>(
|
||||
File.ReadAllText(SeedDirectory + Path.DirectorySeparatorChar + typeof(License).Name + ".json"));
|
||||
context.AddRange(types);
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
private static void SeedMaintainers(FilterListsDbContext context)
|
||||
{
|
||||
if (context.Maintainers.Any()) return;
|
||||
var types = JsonConvert.DeserializeObject<List<Maintainer>>(
|
||||
File.ReadAllText(SeedDirectory + Path.DirectorySeparatorChar + typeof(Maintainer).Name + ".json"));
|
||||
context.AddRange(types);
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
private static void SeedSoftware(FilterListsDbContext context)
|
||||
{
|
||||
if (context.Software.Any()) return;
|
||||
var types = JsonConvert.DeserializeObject<List<Software>>(
|
||||
File.ReadAllText(SeedDirectory + Path.DirectorySeparatorChar + typeof(Software).Name + ".json"));
|
||||
context.AddRange(types);
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
private static void SeedSyntaxes(FilterListsDbContext context)
|
||||
{
|
||||
if (context.Syntaxes.Any()) return;
|
||||
var types = JsonConvert.DeserializeObject<List<Syntax>>(
|
||||
File.ReadAllText(SeedDirectory + Path.DirectorySeparatorChar + typeof(Syntax).Name + ".json"));
|
||||
context.AddRange(types);
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Junctions
|
||||
|
||||
private static void SeedFilterListLanguages(FilterListsDbContext context)
|
||||
{
|
||||
if (context.FilterListLanguages.Any()) return;
|
||||
var types = JsonConvert.DeserializeObject<List<FilterListLanguage>>(
|
||||
File.ReadAllText(
|
||||
SeedDirectory + Path.DirectorySeparatorChar + typeof(FilterListLanguage).Name + ".json"));
|
||||
context.AddRange(types);
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
private static void SeedFilterListMaintainers(FilterListsDbContext context)
|
||||
{
|
||||
if (context.FilterListMaintainers.Any()) return;
|
||||
var types = JsonConvert.DeserializeObject<List<FilterListMaintainer>>(
|
||||
File.ReadAllText(SeedDirectory + Path.DirectorySeparatorChar + typeof(FilterListMaintainer).Name +
|
||||
".json"));
|
||||
context.AddRange(types);
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
private static void SeedForks(FilterListsDbContext context)
|
||||
{
|
||||
if (context.Forks.Any()) return;
|
||||
var types = JsonConvert.DeserializeObject<List<Fork>>(
|
||||
File.ReadAllText(SeedDirectory + Path.DirectorySeparatorChar + typeof(Fork).Name + ".json"));
|
||||
context.AddRange(types);
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
private static void SeedMerges(FilterListsDbContext context)
|
||||
{
|
||||
if (context.Merges.Any()) return;
|
||||
var types = JsonConvert.DeserializeObject<List<Merge>>(
|
||||
File.ReadAllText(SeedDirectory + Path.DirectorySeparatorChar + typeof(Merge).Name + ".json"));
|
||||
context.AddRange(types);
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
private static void SeedSoftwareSyntaxes(FilterListsDbContext context)
|
||||
{
|
||||
if (context.SoftwareSyntaxes.Any()) return;
|
||||
var types = JsonConvert.DeserializeObject<List<SoftwareSyntax>>(
|
||||
File.ReadAllText(SeedDirectory + Path.DirectorySeparatorChar + typeof(SoftwareSyntax).Name + ".json"));
|
||||
context.AddRange(types);
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue