From 2fae748af987be5e98e149c58c990a8a664d0183 Mon Sep 17 00:00:00 2001 From: "alexis.drai" Date: Thu, 6 Oct 2022 11:56:37 +0200 Subject: [PATCH] :necktie: Implement PlayerDbManager --- Sources/Data/EF/Players/PlayerDBManager.cs | 47 ++++++++++++++++++---- Sources/Data/EF/Players/PlayerDbManager.cs | 47 ++++++++++++++++++---- 2 files changed, 78 insertions(+), 16 deletions(-) diff --git a/Sources/Data/EF/Players/PlayerDBManager.cs b/Sources/Data/EF/Players/PlayerDBManager.cs index 18eb812..b12ee87 100644 --- a/Sources/Data/EF/Players/PlayerDBManager.cs +++ b/Sources/Data/EF/Players/PlayerDBManager.cs @@ -6,6 +6,7 @@ namespace Data.EF.Players public sealed class PlayerDbManager : IManager { private readonly DiceAppDbContext db; + public PlayerDbManager(DiceAppDbContext db) { this.db = db; @@ -16,36 +17,66 @@ namespace Data.EF.Players if (db.Players!.Where(entity => entity.Name == toAdd.Name).Any()) { throw new ArgumentException("this username is already taken", nameof(toAdd)); + } + if (toAdd is null) + { + throw new ArgumentNullException(nameof(toAdd), "param should not be null"); + } + if (string.IsNullOrWhiteSpace(toAdd.Name)) + { + throw new ArgumentException("Name property should not be null or whitespace", nameof(toAdd)); } EntityEntry ee = db.Players!.Add(toAdd); db.SaveChanges(); return (PlayerEntity)ee.Entity; } - public IEnumerable GetAll() { return db.Players!.AsEnumerable(); } + /// + /// Calls First(), which will throw an exception if no player with such name exists + /// + /// + /// public PlayerEntity GetOneByName(string name) { - throw new NotImplementedException(); + if(string.IsNullOrWhiteSpace(name)) + { + throw new ArgumentException("Name property should not be null or whitespace", nameof(name)); + } + return db.Players!.First(p => p.Name == name); } public void Remove(PlayerEntity toRemove) - { - throw new NotImplementedException(); + { + if (toRemove is null) + { + throw new ArgumentNullException(nameof(toRemove), "param should not be null"); + } + db.Players!.Remove(toRemove); + db.SaveChanges(); } public PlayerEntity Update(PlayerEntity before, PlayerEntity after) - { - throw new NotImplementedException(); + { + EntityEntry ee = db.Players!.Update(before); + before.Name = after.Name; + db.SaveChanges(); + return (PlayerEntity)ee.Entity; + } + /// + /// Calls First(), which will throw an exception if no player with such ID exists + /// + /// + /// public PlayerEntity GetOneByID(Guid ID) - { - throw new NotImplementedException(); + { + return db.Players!.First(p => p.ID == ID); } } } diff --git a/Sources/Data/EF/Players/PlayerDbManager.cs b/Sources/Data/EF/Players/PlayerDbManager.cs index 18eb812..b12ee87 100644 --- a/Sources/Data/EF/Players/PlayerDbManager.cs +++ b/Sources/Data/EF/Players/PlayerDbManager.cs @@ -6,6 +6,7 @@ namespace Data.EF.Players public sealed class PlayerDbManager : IManager { private readonly DiceAppDbContext db; + public PlayerDbManager(DiceAppDbContext db) { this.db = db; @@ -16,36 +17,66 @@ namespace Data.EF.Players if (db.Players!.Where(entity => entity.Name == toAdd.Name).Any()) { throw new ArgumentException("this username is already taken", nameof(toAdd)); + } + if (toAdd is null) + { + throw new ArgumentNullException(nameof(toAdd), "param should not be null"); + } + if (string.IsNullOrWhiteSpace(toAdd.Name)) + { + throw new ArgumentException("Name property should not be null or whitespace", nameof(toAdd)); } EntityEntry ee = db.Players!.Add(toAdd); db.SaveChanges(); return (PlayerEntity)ee.Entity; } - public IEnumerable GetAll() { return db.Players!.AsEnumerable(); } + /// + /// Calls First(), which will throw an exception if no player with such name exists + /// + /// + /// public PlayerEntity GetOneByName(string name) { - throw new NotImplementedException(); + if(string.IsNullOrWhiteSpace(name)) + { + throw new ArgumentException("Name property should not be null or whitespace", nameof(name)); + } + return db.Players!.First(p => p.Name == name); } public void Remove(PlayerEntity toRemove) - { - throw new NotImplementedException(); + { + if (toRemove is null) + { + throw new ArgumentNullException(nameof(toRemove), "param should not be null"); + } + db.Players!.Remove(toRemove); + db.SaveChanges(); } public PlayerEntity Update(PlayerEntity before, PlayerEntity after) - { - throw new NotImplementedException(); + { + EntityEntry ee = db.Players!.Update(before); + before.Name = after.Name; + db.SaveChanges(); + return (PlayerEntity)ee.Entity; + } + /// + /// Calls First(), which will throw an exception if no player with such ID exists + /// + /// + /// public PlayerEntity GetOneByID(Guid ID) - { - throw new NotImplementedException(); + { + return db.Players!.First(p => p.ID == ID); } } }