public CharacterRepository(CryptsDbContext context, ICharacterStat characterStat, IWeapon weapon, ILocation location) { _context = context; _characterStat = characterStat; _weapon = weapon; _location = location; }
public static void SeedData(IServiceProvider serviceProvider, UserManager <ApplicationUser> userManager, IConfiguration _config) { using (var dbContext = new CryptsDbContext(serviceProvider.GetRequiredService <DbContextOptions <CryptsDbContext> >())) { dbContext.Database.EnsureCreated(); AddRoles(dbContext); SeedUsers(userManager, _config); } }
public static void AddRoles(CryptsDbContext dbContext) { if (dbContext.Roles.Any()) { return; } foreach (var role in Roles) { dbContext.Roles.Add(role); dbContext.SaveChanges(); } }
/// <summary> /// Seed equipment from the DnD API into the Weapons and Items tables /// </summary> /// <param name="serviceProvider">Current service provider info</param> public static void SeedEquipmentToDB(IServiceProvider serviceProvider) { using (var context = new CryptsDbContext(serviceProvider.GetRequiredService <DbContextOptions <CryptsDbContext> >())) { context.Database.EnsureCreated(); //if (context.Item.Any()) //{ //} if (context.Item.ToList().Count > 20 && context.Weapon.ToList().Count > 20) { return; } GetAndDeserializeEquipment(context); } }
public DatabaseTest() { _connection = new SqliteConnection("Filename=:memory:"); _connection.Open(); _db = new CryptsDbContext( new DbContextOptionsBuilder <CryptsDbContext>() .UseSqlite(_connection) .Options); _db.Database.EnsureCreated(); _character = new CharacterRepository(_db, _characterStat, _weapon, _location); _characterStat = new CharacterStatRepository(_db, _stat); _stat = new StatRepository(_db); _location = new LocationsRepository(_db, _enemy); _item = new ItemRepository(_db); _weapon = new WeaponRepository(_db); _enemy = new EnemyRepository(_db); }
/// <summary> /// Call for equipment list, then call each item in list to get specific information and pass into SeedData /// </summary> /// <param name="context">DB Context</param> public static void GetAndDeserializeEquipment(CryptsDbContext context) { using (WebClient wc = new WebClient()) { var client = new WebClient(); // 1st API call to get list of all items var response = client.DownloadString("https://www.dnd5eapi.co/api/equipment"); var deserialized = JsonConvert.DeserializeObject <DeserializeInital.Root>(response); foreach (var item in deserialized.results) { // 2nd API call to get details about specific items var innerResponse = client.DownloadString($"https://www.dnd5eapi.co/api/equipment/{item.index}"); var innerDeserialize = JsonConvert.DeserializeObject <Root>(innerResponse); SeedData(innerDeserialize, context); } } }
/// <summary> /// Handle the data seeding /// </summary> /// <param name="deserialized">Data from API</param> /// <param name="context">DB Context</param> public static void SeedData(Root deserialized, CryptsDbContext context) { if (deserialized.weapon_category == "" || deserialized.weapon_category == null) { Item newItem = new Item { Name = deserialized.name, Value = $"{deserialized.cost.quantity} {deserialized.cost.unit}", }; context.Entry(newItem).State = EntityState.Added; context.SaveChanges(); } else { Weapon newWeapon = new Weapon { BaseDamage = deserialized.damage != null ? deserialized.damage.damage_dice : "1d6", Name = deserialized.name, Type = deserialized.weapon_category }; context.Entry(newWeapon).State = EntityState.Added; context.SaveChanges(); } }
public ItemRepository(CryptsDbContext context) { _context = context; }
public CharacterStatRepository(CryptsDbContext context, IStat stat) { _context = context; _stat = stat; }
public LogRepository(CryptsDbContext context) { _context = context; }
public WeaponRepository(CryptsDbContext context) { _context = context; }
public LocationsRepository(CryptsDbContext context, IEnemy enemy) { _context = context; _enemy = enemy; }
public StatRepository(CryptsDbContext context) { _context = context; }
public EnemyRepository(CryptsDbContext context) { _context = context; }