added seeding for episodes. update models to store date only in mongodb

This commit is contained in:
StevanFreeborn
2022-06-20 15:29:17 -05:00
parent fa2b613ebb
commit 3c9206b8c7
5 changed files with 4300 additions and 12 deletions
+1
View File
@@ -31,6 +31,7 @@ namespace server.Models
public string[]? WrittenBy { get; set; } public string[]? WrittenBy { get; set; }
[BsonElement("airDate")] [BsonElement("airDate")]
[BsonDateTimeOptions(DateOnly = true)]
public DateTime? AirDate { get; set; } public DateTime? AirDate { get; set; }
[BsonElement("usViewersInMillions")] [BsonElement("usViewersInMillions")]
+2
View File
@@ -16,9 +16,11 @@ namespace server.Models
public int? NumberOfEpisodes { get; set; } public int? NumberOfEpisodes { get; set; }
[BsonElement("dateFirstAired")] [BsonElement("dateFirstAired")]
[BsonDateTimeOptions(DateOnly = true)]
public DateTime? DateFirstAired { get; set; } public DateTime? DateFirstAired { get; set; }
[BsonElement("dateLastAired")] [BsonElement("dateLastAired")]
[BsonDateTimeOptions(DateOnly = true)]
public DateTime? DateLastAired { get; set; } public DateTime? DateLastAired { get; set; }
} }
} }
File diff suppressed because it is too large Load Diff
+26 -10
View File
@@ -7,25 +7,27 @@ namespace server.Persistence.Seed
{ {
public class Seeder public class Seeder
{ {
private readonly IMongoClient client; private readonly IMongoClient _client;
private readonly IMongoDatabase database; private readonly IMongoDatabase _database;
private readonly IConfiguration configuration; private readonly IConfiguration _config;
private readonly IMongoCollection<Season> Seasons; private readonly IMongoCollection<Season> _seasons;
private readonly IMongoCollection<Episode> _episodes;
public Seeder() public Seeder()
{ {
configuration = new ConfigurationBuilder() _config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json") .AddJsonFile("appsettings.json")
.Build(); .Build();
client = new MongoClient(configuration.GetSection("MongoDBSettings:ConnectionString").Value); _client = new MongoClient(_config.GetSection("MongoDBSettings:ConnectionString").Value);
database = client.GetDatabase(configuration.GetSection("MongoDBSettings:DatabaseName").Value); _database = _client.GetDatabase(_config.GetSection("MongoDBSettings:DatabaseName").Value);
Seasons = database.GetCollection<Season>(configuration.GetSection("MongoDBSettings:SeasonsCollection").Value); _seasons = _database.GetCollection<Season>(_config.GetSection("MongoDBSettings:SeasonsCollection").Value);
_episodes = _database.GetCollection<Episode>(_config.GetSection("MongoDBSettings:EpisodesCollection").Value);
} }
public async Task SeedSeasonsAsync() public async Task SeedSeasonsAsync()
{ {
await Seasons.DeleteManyAsync(season => true); await _seasons.DeleteManyAsync(season => true);
var seasonsFilePath = Path.Combine(AppContext.BaseDirectory, "Persistence/Seed/Data/seasons.json"); var seasonsFilePath = Path.Combine(AppContext.BaseDirectory, "Persistence/Seed/Data/seasons.json");
var seasonsJson = File.ReadAllText(seasonsFilePath); var seasonsJson = File.ReadAllText(seasonsFilePath);
@@ -33,7 +35,21 @@ namespace server.Persistence.Seed
if(seasons != null) if(seasons != null)
{ {
await Seasons.InsertManyAsync(seasons); 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);
} }
} }
} }
+13 -2
View File
@@ -4,10 +4,21 @@ using server.Persistence;
using server.Persistence.Repositories; using server.Persistence.Repositories;
using server.Persistence.Seed; using server.Persistence.Seed;
if (args.Length == 1 && args[0].ToLower() == "seed") if (args.Length == 2 && args[0].ToLower() == "seed")
{ {
var seeder = new Seeder(); var seeder = new Seeder();
_ = seeder.SeedSeasonsAsync();
if (args[1].ToLower() == "seasons")
{
await seeder.SeedSeasonsAsync();
}
if (args[1].ToLower() == "episodes")
{
await seeder.SeedEpisodesAsync();
}
} }
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);