diff --git a/Sources/App/Program.cs b/Sources/App/Program.cs index 18ab718..785dac0 100644 --- a/Sources/App/Program.cs +++ b/Sources/App/Program.cs @@ -32,12 +32,12 @@ namespace App } // DB stuff when the app opens - - // Later, we'll use a GameDBRunner - using (PlayerDbManager playerDBManager = new(new DiceAppDbContext())) + using (DiceAppDbContext db = new()) { + // Later, we'll use the DiceAppDbContext to get a GameDbRunner + // get all the players from the DB - IEnumerable entities = playerDBManager.GetAll(); + IEnumerable entities = db.Players; foreach (PlayerEntity entity in entities) { @@ -147,16 +147,19 @@ namespace App } // DB stuff when the app closes - using (PlayerDbManager playerDBManager = new(new DiceAppDbContext())) + using (DiceAppDbContext db = new()) { // get all the players from the app's memory IEnumerable models = gameRunner.GlobalPlayerManager.GetAll(); + // create a PlayerDbManager (and inject it with the DB) + PlayerDbManager playerDbManager = new(db); + foreach (Player model in models) { try // to persist them { // as entities ! - playerDBManager.Add(model.ToEntity()); + playerDbManager.Add(model.ToEntity()); } // what if there's already a player with that name? Exception (see PlayerEntity's annotations) catch (ArgumentException ex) { Debug.WriteLine($"{ex.Message}\n... Never mind"); } @@ -294,7 +297,6 @@ namespace App { Console.WriteLine("write the name of a dice group you want to add (at least one), or 'ok' if you're finished"); menuChoiceDice = Console.ReadLine(); - // no checks, this is temporary if (!menuChoiceDice.Equals("ok")) { IEnumerable chosenDice = gameRunner.GlobalDieManager.GetOneByName(menuChoiceDice).Value; diff --git a/Sources/Data/EF/DiceAppDbContext.cs b/Sources/Data/EF/DiceAppDbContext.cs index 1c35807..da142bf 100644 --- a/Sources/Data/EF/DiceAppDbContext.cs +++ b/Sources/Data/EF/DiceAppDbContext.cs @@ -1,10 +1,16 @@ using Data.EF.Players; using Microsoft.EntityFrameworkCore; +using Model.Games; namespace Data.EF { - public class DiceAppDbContext : DbContext + public class DiceAppDbContext : DbContext, ILoader { + public virtual GameRunner LoadApp() + { + throw new NotImplementedException(); + } + public DbSet? Players { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) diff --git a/Sources/Data/EF/DiceAppDbContextWithStub.cs b/Sources/Data/EF/DiceAppDbContextWithStub.cs index 0e22963..c857085 100644 --- a/Sources/Data/EF/DiceAppDbContextWithStub.cs +++ b/Sources/Data/EF/DiceAppDbContextWithStub.cs @@ -1,20 +1,27 @@ using Data.EF.Players; using Microsoft.EntityFrameworkCore; +using Model.Games; +using System.Security.Cryptography.X509Certificates; namespace Data.EF { public class DiceAppDbContextWithStub : DiceAppDbContext { + public override GameRunner LoadApp() + { + throw new NotImplementedException(); + } + protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity().HasData( - new PlayerEntity { ID = Guid.Parse("e3b42372-0186-484c-9b1c-01618fbfac44"), Name = "Alice" }, - new PlayerEntity { ID = Guid.Parse("73265e15-3c43-45f8-8f5d-d02feaaf7620"), Name = "Bob" }, - new PlayerEntity { ID = Guid.Parse("5198ba9d-44d6-4660-85f9-1843828c6f0d"), Name = "Clyde" }, - new PlayerEntity { ID = Guid.Parse("386cec27-fd9d-4475-8093-93c8b569bf2e"), Name = "Dahlia" } - ); + new PlayerEntity { ID = Guid.Parse("e3b42372-0186-484c-9b1c-01618fbfac44"), Name = "Alice" }, + new PlayerEntity { ID = Guid.Parse("73265e15-3c43-45f8-8f5d-d02feaaf7620"), Name = "Bob" }, + new PlayerEntity { ID = Guid.Parse("5198ba9d-44d6-4660-85f9-1843828c6f0d"), Name = "Clyde" }, + new PlayerEntity { ID = Guid.Parse("386cec27-fd9d-4475-8093-93c8b569bf2e"), Name = "Dahlia" } + ); } } } diff --git a/Sources/Data/EF/Players/PlayerDbManager.cs b/Sources/Data/EF/Players/PlayerDbManager.cs index 7957054..18eb812 100644 --- a/Sources/Data/EF/Players/PlayerDbManager.cs +++ b/Sources/Data/EF/Players/PlayerDbManager.cs @@ -3,7 +3,7 @@ using Model; namespace Data.EF.Players { - public sealed class PlayerDbManager : IManager, IDisposable + public sealed class PlayerDbManager : IManager { private readonly DiceAppDbContext db; public PlayerDbManager(DiceAppDbContext db) @@ -11,11 +11,6 @@ namespace Data.EF.Players this.db = db; } - public void Dispose() - { - db.Dispose(); - } - public PlayerEntity Add(PlayerEntity toAdd) { if (db.Players!.Where(entity => entity.Name == toAdd.Name).Any())