2022-06-20 08:50:11 -05:00
|
|
|
using System.Runtime.CompilerServices;
|
2022-06-20 13:08:57 -05:00
|
|
|
using System.Text.Json;
|
2022-06-20 08:50:11 -05:00
|
|
|
using MongoDB.Driver;
|
|
|
|
|
using server.Models;
|
|
|
|
|
|
|
|
|
|
namespace server.Persistence.Seed
|
|
|
|
|
{
|
|
|
|
|
public class Seeder
|
|
|
|
|
{
|
2022-06-20 15:29:17 -05:00
|
|
|
private readonly IMongoClient _client;
|
|
|
|
|
private readonly IMongoDatabase _database;
|
|
|
|
|
private readonly IConfiguration _config;
|
|
|
|
|
private readonly IMongoCollection<Season> _seasons;
|
|
|
|
|
private readonly IMongoCollection<Episode> _episodes;
|
2022-06-20 08:50:11 -05:00
|
|
|
|
|
|
|
|
public Seeder()
|
|
|
|
|
{
|
2022-06-20 15:29:17 -05:00
|
|
|
_config = new ConfigurationBuilder()
|
2022-06-20 08:50:11 -05:00
|
|
|
.AddJsonFile("appsettings.json")
|
|
|
|
|
.Build();
|
|
|
|
|
|
2022-06-20 15:29:17 -05:00
|
|
|
_client = new MongoClient(_config.GetSection("MongoDBSettings:ConnectionString").Value);
|
|
|
|
|
_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);
|
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
|
|
|
|
|
|
|
|
var seasonsFilePath = Path.Combine(AppContext.BaseDirectory, "Persistence/Seed/Data/seasons.json");
|
|
|
|
|
var seasonsJson = File.ReadAllText(seasonsFilePath);
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SeedEpisodesAsync()
|
|
|
|
|
{
|
|
|
|
|
await _episodes.DeleteManyAsync(episode => true);
|
|
|
|
|
|
|
|
|
|
var episodesFilePath = Path.Combine(AppContext.BaseDirectory, "Persistence/Seed/Data/episodes.json");
|
|
|
|
|
var episodesJson = File.ReadAllText(episodesFilePath);
|
|
|
|
|
var episodes = JsonSerializer.Deserialize<List<Episode>>(episodesJson);
|
|
|
|
|
|
|
|
|
|
if (episodes != null)
|
|
|
|
|
{
|
|
|
|
|
await _episodes.InsertManyAsync(episodes);
|
2022-06-20 08:50:11 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|