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

131 lines
6.3 KiB
C#
Raw Normal View History

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; }
2020-12-04 15:49:51 +01:00
public virtual DbSet<Charisma> Charismas { get; set; }
public virtual DbSet<Class> Classes { get; set; }
2020-12-04 15:49:51 +01:00
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; }
2020-12-04 15:49:51 +01:00
public virtual DbSet<Strength> Strengths { get; set; }
public virtual DbSet<User> Users { get; set; }
2020-12-04 15:49:51 +01:00
public virtual DbSet<Wisdom> Wisdoms { get; set; }
2020-12-27 20:08:19 +01:00
public virtual DbSet<Spell> Spells { get; set; }
public virtual DbSet<CharacterSpell> CharacterSpells { get; set; }
public virtual DbSet<CharacterSpellSlots> CharacterSpellSlots { get; set; }
2020-12-27 19:47:13 +01:00
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;
}
2021-01-03 16:28:13 +01:00
protected IEnumerable<Spell> SeedSpell()
{
const string file = "../SessionCompanion.Database/JsonData/spells.json";
List<Spell> spells = new List<Spell>();
using (StreamReader reader = new StreamReader(file))
{
var json = reader.ReadToEnd();
dynamic jspells = JArray.Parse(json);
foreach (dynamic item in jspells)
spells.Add(SeedFromJsons.SingleSpellSeeder(item));
}
return spells;
}
2020-12-03 17:00:04 +01:00
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
2020-12-03 17:50:11 +01:00
builder.Entity<User>().HasData(SeedData.SeedUsers());
2020-12-18 12:00:36 +01:00
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());
2021-01-04 00:59:23 +01:00
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());
2021-01-03 16:28:13 +01:00
builder.Entity<Spell>().HasData(SeedSpell());
builder.Entity<CharacterSpell>().HasData(SeedData.SeedCharacterSpell());
builder.Entity<CharacterSpellSlots>().HasData(SeedData.SeedCharacterSpellSlot());
2020-12-03 17:00:04 +01:00
}
}
}