🩹 Try-catch db connectionsin prototype
continuous-integration/drone/push Build is passing Details

pull/110/head
Alexis Drai 2 years ago
parent cfb495cd68
commit 901a1128e9

@ -31,23 +31,26 @@ namespace App
gameRunner = new(new PlayerManager(), new DieManager(), null);
}
// DB stuff when the app opens
using (DiceAppDbContext db = new())
{
// Later, we'll use the DiceAppDbContext to get a GameDbRunner
// get all the players from the DB
IEnumerable<PlayerEntity> entities = db.Players;
foreach (PlayerEntity entity in entities)
{
try // to persist them
{ // as models !
gameRunner.GlobalPlayerManager.Add(entity.ToModel());
try
{
// DB stuff when the app opens
using (DiceAppDbContext db = new())
{
// Later, we'll use the DiceAppDbContext to get a GameDbRunner
// get all the players from the DB
IEnumerable<PlayerEntity> entities = db.Players;
Debug.WriteLine("Loading players");
foreach (PlayerEntity entity in entities)
{
// persist them as models !
gameRunner.GlobalPlayerManager.Add(entity.ToModel());
}
catch (Exception ex) { Debug.WriteLine($"{ex.Message}\n... Did you make sure that the DATABASE exists?"); }
}
}
}
catch (Exception ex) { Debug.WriteLine($"{ex.Message}\n... Couldn't use the database"); }
string menuChoice = "nothing";
@ -146,26 +149,30 @@ namespace App
}
}
// DB stuff when the app closes
using (DiceAppDbContext db = new())
{
// get all the players from the app's memory
IEnumerable<Player> 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());
}
// 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 (Exception ex) { Debug.WriteLine($"{ex.Message}\n... Did you make sure that the DATABASE exists?"); }
}
}
try
{
// DB stuff when the app closes
using (DiceAppDbContext db = new())
{
// get all the players from the app's memory
IEnumerable<Player> models = gameRunner.GlobalPlayerManager.GetAll();
// create a PlayerDbManager (and inject it with the DB)
PlayerDbManager playerDbManager = new(db);
Debug.WriteLine("Saving players");
foreach (Player model in models)
{
try // to persist them
{ // as entities !
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"); }
}
}
} catch (Exception ex) { Debug.WriteLine($"{ex.Message}\n... Couldn't use the database"); }
}
private static void Play(GameRunner gameRunner, string name)

Loading…
Cancel
Save