using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; using Model; using Model.Players; using System.Runtime.Intrinsics.Arm; namespace Data.EF.Players { public sealed class PlayerDbManager : IManager { private readonly DiceAppDbContext db; public PlayerDbManager(DiceAppDbContext db) { if (db is null) { throw new ArgumentNullException(nameof(db), "param should not be null"); } this.db = db; } /// /// side effect: entity's name is trimmed. /// /// /// /// private static void CleanPlayerEntity(PlayerEntity entity) { if (entity is null) { throw new ArgumentNullException(nameof(entity), "param should not be null"); } if (string.IsNullOrWhiteSpace(entity.Name)) { throw new ArgumentException("Name property should not be null or whitespace", nameof(entity)); } entity.Name = entity.Name.Trim(); } /// /// adds a non-null PlayerEntity with a valid name to this mgr's context /// /// the entity to add /// /// /// public PlayerEntity Add(PlayerEntity toAdd) { CleanPlayerEntity(toAdd); if (db.Players.Where(entity => entity.Name == toAdd.Name).Any()) { throw new ArgumentException("this username is already taken", nameof(toAdd)); } EntityEntry ee = db.Players.Add(toAdd); db.SaveChanges(); return (PlayerEntity)ee.Entity; } public IEnumerable GetAll() { return db.Players.AsEnumerable(); } /// /// This will throw an exception if no player with such name exists. /// If you want to know whether any player with that name exists, call IsPresentByName() /// /// /// /// /// public PlayerEntity GetOneByName(string name) { if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentException("Name property should not be null or whitespace", nameof(name)); } name = name.Trim(); return db.Players.Where(p => p.Name == name).First(); } public bool IsPresentByName(string name) { if (string.IsNullOrWhiteSpace(name)) { return false; } name = name.Trim(); return db.Players.Where(p => p.Name == name).Any(); } /// /// removes a non-null PlayerEntity with a valid name from this mgr's context /// /// the entity to remove /// /// public void Remove(PlayerEntity toRemove) { CleanPlayerEntity(toRemove); if (IsPresentByID(toRemove.ID)) { db.Players.Remove(toRemove); db.SaveChanges(); } } /// /// updates a non-null PlayerEntity with a valid name in this mgr's context. This cannot update an ID /// /// the entity to update /// the entity to replace 'before' /// the updated entity /// /// public PlayerEntity Update(PlayerEntity before, PlayerEntity after) { PlayerEntity[] args = { before, after }; foreach (PlayerEntity entity in args) { CleanPlayerEntity(entity); } if (before.ID != after.ID) { throw new ArgumentException("ID cannot be updated", nameof(after)); } string beforeName = before.Name; before.Name = after.Name; EntityEntry ee = db.Players.Update(before); db.SaveChanges(); before.Name = beforeName; return (PlayerEntity)ee.Entity; } /// /// This will throw an exception if no player with such ID exists. /// If you want to know whether any player with that ID exists, call IsPresentByID() /// /// the ID to look for /// PlayerEntity with that ID /// public PlayerEntity GetOneByID(Guid ID) { return db.Players.First(p => p.ID == ID); } public bool IsPresentByID(Guid ID) { return db.Players.Where(p => p.ID == ID).Any(); } } }