2022-06-21 19:04:11 -05:00
|
|
|
using MongoDB.Driver;
|
2022-06-20 08:50:11 -05:00
|
|
|
using server.Models;
|
2022-06-21 19:04:11 -05:00
|
|
|
using System.Text.Json;
|
2022-06-20 08:50:11 -05:00
|
|
|
|
|
|
|
|
namespace server.Persistence.Seed
|
|
|
|
|
{
|
|
|
|
|
public class Seeder
|
|
|
|
|
{
|
2022-06-20 15:29:17 -05:00
|
|
|
private readonly IMongoCollection<Season> _seasons;
|
|
|
|
|
private readonly IMongoCollection<Episode> _episodes;
|
2022-06-21 23:24:56 -05:00
|
|
|
private readonly IMongoCollection<Quote> _quotes;
|
2022-06-24 00:16:23 -05:00
|
|
|
private readonly IMongoCollection<Character> _characters;
|
2022-06-20 08:50:11 -05:00
|
|
|
|
|
|
|
|
public Seeder()
|
|
|
|
|
{
|
2022-06-21 23:29:50 -05:00
|
|
|
IConfiguration config = new ConfigurationBuilder()
|
2022-06-20 08:50:11 -05:00
|
|
|
.AddJsonFile("appsettings.json")
|
|
|
|
|
.Build();
|
|
|
|
|
|
2022-06-21 23:29:50 -05:00
|
|
|
IMongoClient client = new MongoClient(config.GetSection("MongoDBSettings:ConnectionString").Value);
|
|
|
|
|
|
|
|
|
|
IMongoDatabase database = client.GetDatabase(config.GetSection("MongoDBSettings:DatabaseName").Value);
|
|
|
|
|
|
|
|
|
|
_seasons = database.GetCollection<Season>(config.GetSection("MongoDBSettings:SeasonsCollection").Value);
|
|
|
|
|
_episodes = database.GetCollection<Episode>(config.GetSection("MongoDBSettings:EpisodesCollection").Value);
|
|
|
|
|
_quotes = database.GetCollection<Quote>(config.GetSection("MongoDBSettings:QuotesCollection").Value);
|
2022-06-24 00:16:23 -05:00
|
|
|
_characters = database.GetCollection<Character>(config.GetSection("MongoDBSettings:CharactersCollection").Value);
|
2022-06-20 08:50:11 -05:00
|
|
|
}
|
|
|
|
|
|
2022-06-20 13:08:57 -05:00
|
|
|
public async Task SeedSeasonsAsync()
|
2022-06-20 08:50:11 -05:00
|
|
|
{
|
2022-06-20 15:29:17 -05:00
|
|
|
await _seasons.DeleteManyAsync(season => true);
|
2022-06-20 13:08:57 -05:00
|
|
|
|
2022-06-21 23:24:56 -05:00
|
|
|
const string fileName = "seasons.json";
|
2022-06-21 15:52:03 -05:00
|
|
|
var seasonsFilePath = Path.Combine(AppContext.BaseDirectory, $"Persistence/Seed/Data/{fileName}");
|
2022-06-21 23:24:56 -05:00
|
|
|
var seasonsJson = await File.ReadAllTextAsync(seasonsFilePath);
|
2022-06-20 13:08:57 -05:00
|
|
|
var seasons = JsonSerializer.Deserialize<List<Season>>(seasonsJson);
|
|
|
|
|
|
|
|
|
|
if(seasons != null)
|
2022-06-20 08:50:11 -05:00
|
|
|
{
|
2022-06-20 15:29:17 -05:00
|
|
|
await _seasons.InsertManyAsync(seasons);
|
2022-06-24 00:16:23 -05:00
|
|
|
Console.WriteLine($"Seeded databases with {nameof(seasons)} from {fileName}");
|
2022-06-20 15:29:17 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SeedEpisodesAsync()
|
|
|
|
|
{
|
|
|
|
|
await _episodes.DeleteManyAsync(episode => true);
|
|
|
|
|
|
2022-06-21 23:24:56 -05:00
|
|
|
const string fileName = "episodes.json";
|
2022-06-21 15:52:03 -05:00
|
|
|
var episodesFilePath = Path.Combine(AppContext.BaseDirectory, $"Persistence/Seed/Data/{fileName}");
|
2022-06-21 23:24:56 -05:00
|
|
|
var episodesJson = await File.ReadAllTextAsync(episodesFilePath);
|
2022-06-20 15:29:17 -05:00
|
|
|
var episodes = JsonSerializer.Deserialize<List<Episode>>(episodesJson);
|
|
|
|
|
|
|
|
|
|
if (episodes != null)
|
|
|
|
|
{
|
|
|
|
|
await _episodes.InsertManyAsync(episodes);
|
2022-06-24 00:16:23 -05:00
|
|
|
Console.WriteLine($"Seeded databases with {nameof(episodes)} from {fileName}");
|
2022-06-20 08:50:11 -05:00
|
|
|
}
|
|
|
|
|
}
|
2022-06-21 23:24:56 -05:00
|
|
|
|
|
|
|
|
public async Task SeedQuotesAsync()
|
|
|
|
|
{
|
2022-06-24 00:16:23 -05:00
|
|
|
await _quotes.DeleteManyAsync(quote => true);
|
2022-06-21 23:24:56 -05:00
|
|
|
|
|
|
|
|
const string fileName = "quotes.json";
|
|
|
|
|
var quotesFilePath = Path.Combine(AppContext.BaseDirectory, $"Persistence/Seed/Data/{fileName}");
|
|
|
|
|
var quotesJson = await File.ReadAllTextAsync(quotesFilePath);
|
2022-06-21 23:31:07 -05:00
|
|
|
var quotes = JsonSerializer.Deserialize<List<Quote>>(quotesJson);
|
2022-06-21 23:24:56 -05:00
|
|
|
|
|
|
|
|
if (quotes != null)
|
|
|
|
|
{
|
2022-06-21 23:31:07 -05:00
|
|
|
await _quotes.InsertManyAsync(quotes);
|
2022-06-24 00:16:23 -05:00
|
|
|
Console.WriteLine($"Seeded databases with {nameof(quotes)} from {fileName}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SeedCharactersAsync()
|
|
|
|
|
{
|
|
|
|
|
await _characters.DeleteManyAsync(character => true);
|
|
|
|
|
|
|
|
|
|
const string fileName = "characters.json";
|
|
|
|
|
var charactersFilePath = Path.Combine(AppContext.BaseDirectory, $"Persistence/Seed/Data/{fileName}");
|
|
|
|
|
var charactersJson = await File.ReadAllTextAsync(charactersFilePath);
|
|
|
|
|
var characters = JsonSerializer.Deserialize<List<Character>>(charactersJson);
|
|
|
|
|
|
|
|
|
|
if (characters != null)
|
|
|
|
|
{
|
|
|
|
|
await _characters.InsertManyAsync(characters);
|
|
|
|
|
Console.WriteLine($"Seeded databases with {nameof(characters)} from {fileName}");
|
2022-06-21 23:24:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2022-06-20 08:50:11 -05:00
|
|
|
}
|
|
|
|
|
}
|