mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
groundwork for ef seeding
This commit is contained in:
parent
6a884a00f0
commit
b703899912
4 changed files with 60 additions and 2 deletions
|
|
@ -1,5 +1,8 @@
|
|||
using Microsoft.AspNetCore;
|
||||
using FilterLists.Data;
|
||||
using FilterLists.Data.Contexts;
|
||||
using Microsoft.AspNetCore;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace FilterLists.Api
|
||||
{
|
||||
|
|
@ -7,7 +10,12 @@ public class Program
|
|||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
BuildWebHost(args).Run();
|
||||
var host = BuildWebHost(args);
|
||||
//using (var scope = host.Services.CreateScope())
|
||||
//{
|
||||
// DbInitializer.Initialize(scope.ServiceProvider.GetRequiredService<FilterListsDbContext>());
|
||||
//}
|
||||
host.Run();
|
||||
}
|
||||
|
||||
public static IWebHost BuildWebHost(string[] args)
|
||||
|
|
|
|||
32
src/FilterLists.Data/DbInitializer.cs
Normal file
32
src/FilterLists.Data/DbInitializer.cs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
using FilterLists.Data.Contexts;
|
||||
using FilterLists.Data.Models;
|
||||
using FilterLists.Data.Repositories.Implementations;
|
||||
|
||||
namespace FilterLists.Data
|
||||
{
|
||||
public static class DbInitializer
|
||||
{
|
||||
public static void Initialize(FilterListsDbContext filterListsDbContext)
|
||||
{
|
||||
filterListsDbContext.Database.EnsureCreated();
|
||||
InitializeList(filterListsDbContext);
|
||||
}
|
||||
|
||||
private static void InitializeList(FilterListsDbContext filterListsDbContext)
|
||||
{
|
||||
var listRepository = new ListRepository(filterListsDbContext);
|
||||
if (listRepository.Any())
|
||||
{
|
||||
listRepository.DeleteAll();
|
||||
}
|
||||
var lists = new List[]
|
||||
{
|
||||
//TODO: populate from csv or json
|
||||
};
|
||||
foreach (var list in lists)
|
||||
{
|
||||
listRepository.Create(list);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -7,5 +7,8 @@ public interface IListRepository
|
|||
{
|
||||
IEnumerable<List> GetAll();
|
||||
List GetByName(string listName);
|
||||
void Create(List list);
|
||||
void DeleteAll();
|
||||
bool Any();
|
||||
}
|
||||
}
|
||||
|
|
@ -24,5 +24,20 @@ public List GetByName(string listName)
|
|||
{
|
||||
return _filterListsDbContext.List.First(x => x.Name == listName);
|
||||
}
|
||||
|
||||
public void Create(List list)
|
||||
{
|
||||
_filterListsDbContext.List.Add(list);
|
||||
}
|
||||
|
||||
public void DeleteAll()
|
||||
{
|
||||
_filterListsDbContext.List.RemoveRange(_filterListsDbContext.List);
|
||||
}
|
||||
|
||||
public bool Any()
|
||||
{
|
||||
return _filterListsDbContext.List.Any();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue