⚗️ Try things out, in accordance with wiki UML
continuous-integration/drone/push Build is passing Details

pull/110/head
Alexis Drai 2 years ago
parent 90c1eba43b
commit 722a4314fa

@ -32,12 +32,12 @@ namespace App
} }
// DB stuff when the app opens // DB stuff when the app opens
using (DiceAppDbContext db = new())
// Later, we'll use a GameDBRunner
using (PlayerDbManager playerDBManager = new(new DiceAppDbContext()))
{ {
// Later, we'll use the DiceAppDbContext to get a GameDbRunner
// get all the players from the DB // get all the players from the DB
IEnumerable<PlayerEntity> entities = playerDBManager.GetAll(); IEnumerable<PlayerEntity> entities = db.Players;
foreach (PlayerEntity entity in entities) foreach (PlayerEntity entity in entities)
{ {
@ -147,16 +147,19 @@ namespace App
} }
// DB stuff when the app closes // 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 // get all the players from the app's memory
IEnumerable<Player> models = gameRunner.GlobalPlayerManager.GetAll(); IEnumerable<Player> models = gameRunner.GlobalPlayerManager.GetAll();
// create a PlayerDbManager (and inject it with the DB)
PlayerDbManager playerDbManager = new(db);
foreach (Player model in models) foreach (Player model in models)
{ {
try // to persist them try // to persist them
{ // as entities ! { // 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) // 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"); } 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"); 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(); menuChoiceDice = Console.ReadLine();
// no checks, this is temporary
if (!menuChoiceDice.Equals("ok")) if (!menuChoiceDice.Equals("ok"))
{ {
IEnumerable<Die> chosenDice = gameRunner.GlobalDieManager.GetOneByName(menuChoiceDice).Value; IEnumerable<Die> chosenDice = gameRunner.GlobalDieManager.GetOneByName(menuChoiceDice).Value;

@ -1,10 +1,16 @@
using Data.EF.Players; using Data.EF.Players;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Model.Games;
namespace Data.EF namespace Data.EF
{ {
public class DiceAppDbContext : DbContext public class DiceAppDbContext : DbContext, ILoader
{ {
public virtual GameRunner LoadApp()
{
throw new NotImplementedException();
}
public DbSet<PlayerEntity>? Players { get; set; } public DbSet<PlayerEntity>? Players { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

@ -1,20 +1,27 @@
using Data.EF.Players; using Data.EF.Players;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Model.Games;
using System.Security.Cryptography.X509Certificates;
namespace Data.EF namespace Data.EF
{ {
public class DiceAppDbContextWithStub : DiceAppDbContext public class DiceAppDbContextWithStub : DiceAppDbContext
{ {
public override GameRunner LoadApp()
{
throw new NotImplementedException();
}
protected override void OnModelCreating(ModelBuilder modelBuilder) protected override void OnModelCreating(ModelBuilder modelBuilder)
{ {
base.OnModelCreating(modelBuilder); base.OnModelCreating(modelBuilder);
modelBuilder.Entity<PlayerEntity>().HasData( modelBuilder.Entity<PlayerEntity>().HasData(
new PlayerEntity { ID = Guid.Parse("e3b42372-0186-484c-9b1c-01618fbfac44"), Name = "Alice" }, 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("73265e15-3c43-45f8-8f5d-d02feaaf7620"), Name = "Bob" },
new PlayerEntity { ID = Guid.Parse("5198ba9d-44d6-4660-85f9-1843828c6f0d"), Name = "Clyde" }, 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("386cec27-fd9d-4475-8093-93c8b569bf2e"), Name = "Dahlia" }
); );
} }
} }
} }

@ -3,7 +3,7 @@ using Model;
namespace Data.EF.Players namespace Data.EF.Players
{ {
public sealed class PlayerDbManager : IManager<PlayerEntity>, IDisposable public sealed class PlayerDbManager : IManager<PlayerEntity>
{ {
private readonly DiceAppDbContext db; private readonly DiceAppDbContext db;
public PlayerDbManager(DiceAppDbContext db) public PlayerDbManager(DiceAppDbContext db)
@ -11,11 +11,6 @@ namespace Data.EF.Players
this.db = db; this.db = db;
} }
public void Dispose()
{
db.Dispose();
}
public PlayerEntity Add(PlayerEntity toAdd) public PlayerEntity Add(PlayerEntity toAdd)
{ {
if (db.Players!.Where(entity => entity.Name == toAdd.Name).Any()) if (db.Players!.Where(entity => entity.Name == toAdd.Name).Any())

Loading…
Cancel
Save