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

Loading…
Cancel
Save