session-companion/SessionCompanion/SessionCompanion.Database/ApplicationDbContext.cs

115 lines
5.5 KiB
C#

using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json.Linq;
using SessionCompanion.Database.Tables;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SessionCompanion.Database
{
public class ApplicationDbContext : DbContext
{
public virtual DbSet<Alignment> Alignments { get; set; }
public virtual DbSet<Background> Backgrounds { get; set; }
public virtual DbSet<Biography> Biographies { get; set; }
public virtual DbSet<Character> Characters { get; set; }
public virtual DbSet<Charisma> Charismas { get; set; }
public virtual DbSet<Class> Classes { get; set; }
public virtual DbSet<Constitution> Constitutions { get; set; }
public virtual DbSet<Dexterity> Dexterities { get; set; }
public virtual DbSet<Intelligence> Intelligences { get; set; }
public virtual DbSet<Race> Races { get; set; }
public virtual DbSet<Statistics> Statistics { get; set; }
public virtual DbSet<Strength> Strengths { get; set; }
public virtual DbSet<User> Users { get; set; }
public virtual DbSet<Wisdom> Wisdoms { get; set; }
public virtual DbSet<Spell> Spells { get; set; }
public virtual DbSet<CharacterSpell> CharacterSpells { get; set; }
public virtual DbSet<CharacterSpellSlots> CharacterSpellSlots { get; set; }
public virtual DbSet<Armor> Armors { get; set; }
public virtual DbSet<CharacterArmor> CharacterArmors { get; set; }
public virtual DbSet<Weapon> Weapons { get; set; }
public virtual DbSet<CharacterWeapon> CharacterWeapons { get; set; }
public virtual DbSet<OtherEquipment> OtherEquipment { get; set; }
public virtual DbSet<CharacterOtherEquipment> CharacterOtherEquipment { get; set; }
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }
protected IEnumerable<Weapon> SeedWeapon()
{
const string file = "../SessionCompanion.Database/JsonData/weapons.json";
List<Weapon> weapons = new List<Weapon>();
using (StreamReader reader = new StreamReader(file))
{
var json = reader.ReadToEnd();
dynamic jweapons = JArray.Parse(json);
foreach (dynamic item in jweapons)
weapons.Add(SeedFromJsons.SingleWeaponSeeder(item));
}
return weapons;
}
protected IEnumerable<Armor> SeedArmor()
{
const string file = "../SessionCompanion.Database/JsonData/armors.json";
List<Armor> armors = new List<Armor>();
using (StreamReader reader = new StreamReader(file))
{
var json = reader.ReadToEnd();
dynamic jactions = JArray.Parse(json);
int id = 1;
foreach (dynamic item in jactions)
{
armors.Add(SeedFromJsons.SingleArmorSeeder(item, id));
id++;
}
}
return armors;
}
protected IEnumerable<OtherEquipment> SeedOtherEquipment()
{
const string file = "../SessionCompanion.Database/JsonData/otherEquipments.json";
List<OtherEquipment> otherEquipment = new List<OtherEquipment>();
using (StreamReader reader = new StreamReader(file))
{
var json = reader.ReadToEnd();
dynamic jactions = JArray.Parse(json);
int id = 1;
foreach (dynamic item in jactions)
{
otherEquipment.Add(SeedFromJsons.SingleOtherEquipmentSeeder(item, id));
id++;
}
}
return otherEquipment;
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<User>().HasData(SeedData.SeedUsers());
builder.Entity<Character>().HasData(SeedData.SeedCharacter());
builder.Entity<Race>().HasData(SeedData.SeedRace());
builder.Entity<Class>().HasData(SeedData.SeedClass());
builder.Entity<Alignment>().HasData(SeedData.SeedAlignment());
builder.Entity<Background>().HasData(SeedData.SeedBackground());
builder.Entity<Biography>().HasData(SeedData.SeedBiography());
builder.Entity<Statistics>().HasData(SeedData.SeedStatistics());
builder.Entity<Weapon>().HasData(SeedWeapon());
builder.Entity<Armor>().HasData(SeedArmor());
builder.Entity<OtherEquipment>().HasData(SeedOtherEquipment());
builder.Entity<CharacterWeapon>().HasData(SeedData.SeedCharacterWeapon());
builder.Entity<CharacterArmor>().HasData(SeedData.SeedCharacterArmor());
builder.Entity<CharacterOtherEquipment>().HasData(SeedData.SeedCharacterOtherEquipment());
builder.Entity<Charisma>().HasData(SeedData.SeedCharisma());
builder.Entity<Constitution>().HasData(SeedData.SeedConstitution());
builder.Entity<Dexterity>().HasData(SeedData.SeedDexterity());
builder.Entity<Intelligence>().HasData(SeedData.SeedIntelligence());
builder.Entity<Strength>().HasData(SeedData.SeedStrength());
builder.Entity<Wisdom>().HasData(SeedData.SeedWisdom());
}
}
}