🩹 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,6 +31,8 @@ namespace App
gameRunner = new(new PlayerManager(), new DieManager(), null); gameRunner = new(new PlayerManager(), new DieManager(), null);
} }
try
{
// DB stuff when the app opens // DB stuff when the app opens
using (DiceAppDbContext db = new()) using (DiceAppDbContext db = new())
{ {
@ -39,15 +41,16 @@ namespace App
// get all the players from the DB // get all the players from the DB
IEnumerable<PlayerEntity> entities = db.Players; IEnumerable<PlayerEntity> entities = db.Players;
Debug.WriteLine("Loading players");
foreach (PlayerEntity entity in entities) foreach (PlayerEntity entity in entities)
{ {
try // to persist them // persist them as models !
{ // 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,6 +149,8 @@ namespace App
} }
} }
try
{
// DB stuff when the app closes // DB stuff when the app closes
using (DiceAppDbContext db = new()) using (DiceAppDbContext db = new())
{ {
@ -155,6 +160,8 @@ namespace App
// 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);
Debug.WriteLine("Saving players");
foreach (Player model in models) foreach (Player model in models)
{ {
try // to persist them try // to persist them
@ -163,9 +170,9 @@ namespace App
} }
// 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"); }
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