👔 Implement PlayerDbManager
continuous-integration/drone/push Build is passing Details

pull/110/head
Alexis Drai 2 years ago
parent 621d6c3bc2
commit 2fae748af9

@ -6,6 +6,7 @@ namespace Data.EF.Players
public sealed class PlayerDbManager : IManager<PlayerEntity> public sealed class PlayerDbManager : IManager<PlayerEntity>
{ {
private readonly DiceAppDbContext db; private readonly DiceAppDbContext db;
public PlayerDbManager(DiceAppDbContext db) public PlayerDbManager(DiceAppDbContext db)
{ {
this.db = db; this.db = db;
@ -17,35 +18,65 @@ namespace Data.EF.Players
{ {
throw new ArgumentException("this username is already taken", nameof(toAdd)); 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); EntityEntry ee = db.Players!.Add(toAdd);
db.SaveChanges(); db.SaveChanges();
return (PlayerEntity)ee.Entity; return (PlayerEntity)ee.Entity;
} }
public IEnumerable<PlayerEntity> GetAll() public IEnumerable<PlayerEntity> GetAll()
{ {
return db.Players!.AsEnumerable(); return db.Players!.AsEnumerable();
} }
/// <summary>
/// Calls First(), which will throw an exception if no player with such name exists
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public PlayerEntity GetOneByName(string name) 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) 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) 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;
} }
/// <summary>
/// Calls First(), which will throw an exception if no player with such ID exists
/// </summary>
/// <param name="ID"></param>
/// <returns></returns>
public PlayerEntity GetOneByID(Guid ID) public PlayerEntity GetOneByID(Guid ID)
{ {
throw new NotImplementedException(); return db.Players!.First(p => p.ID == ID);
} }
} }
} }

@ -6,6 +6,7 @@ namespace Data.EF.Players
public sealed class PlayerDbManager : IManager<PlayerEntity> public sealed class PlayerDbManager : IManager<PlayerEntity>
{ {
private readonly DiceAppDbContext db; private readonly DiceAppDbContext db;
public PlayerDbManager(DiceAppDbContext db) public PlayerDbManager(DiceAppDbContext db)
{ {
this.db = db; this.db = db;
@ -17,35 +18,65 @@ namespace Data.EF.Players
{ {
throw new ArgumentException("this username is already taken", nameof(toAdd)); 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); EntityEntry ee = db.Players!.Add(toAdd);
db.SaveChanges(); db.SaveChanges();
return (PlayerEntity)ee.Entity; return (PlayerEntity)ee.Entity;
} }
public IEnumerable<PlayerEntity> GetAll() public IEnumerable<PlayerEntity> GetAll()
{ {
return db.Players!.AsEnumerable(); return db.Players!.AsEnumerable();
} }
/// <summary>
/// Calls First(), which will throw an exception if no player with such name exists
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public PlayerEntity GetOneByName(string name) 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) 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) 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;
} }
/// <summary>
/// Calls First(), which will throw an exception if no player with such ID exists
/// </summary>
/// <param name="ID"></param>
/// <returns></returns>
public PlayerEntity GetOneByID(Guid ID) public PlayerEntity GetOneByID(Guid ID)
{ {
throw new NotImplementedException(); return db.Players!.First(p => p.ID == ID);
} }
} }
} }

Loading…
Cancel
Save