using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Threading.Tasks; namespace Model.Games { public class GameManager : IManager { /// /// the games managed by this instance /// private readonly List games = new(); /// /// gets an unmodifiable collection of the games /// /// unmodifiable collection of the games public Task> GetAll() => Task.FromResult(new ReadOnlyCollection(games)); /// /// finds the game with that name and returns it ///
/// that copy does not belong to this manager's games, so it should not be modified ///
/// a games's name /// game with said name, or null if no such game was found public Task GetOneByName(string name) { if (!string.IsNullOrWhiteSpace(name)) { return Task.FromResult(games.FirstOrDefault(g => g.Name == name)); // may return null } throw new ArgumentException("param should not be null or blank", nameof(name)); } /// /// not implemented in the model /// /// /// /// public Task GetOneByID(Guid ID) { throw new NotImplementedException(); } /// /// saves a given game -- does not allow copies yet: if a game with the same name exists, it is overwritten /// /// a game to save public Task Add(Game toAdd) { if (toAdd is null) { throw new ArgumentNullException(nameof(toAdd), "param should not be null"); } games.Remove(games.FirstOrDefault(g => g.Name == toAdd.Name)); // will often be an update: if game with that name exists, it is removed, else, nothing happens above games.Add(toAdd); return Task.FromResult(toAdd); } /// /// removes a game. does nothing if the game doesn't exist /// /// game to remove /// public void Remove(Game toRemove) { if (toRemove is null) { throw new ArgumentNullException(nameof(toRemove), "param should not be null"); } games.Remove(toRemove); } /// /// updates a game /// /// original game /// new game /// /// public Task Update(Game before, Game after) { Game[] args = { before, after }; foreach (Game game in args) { if (game is null) { throw new ArgumentNullException(nameof(after), "param should not be null"); // could also be because of before, but one param had to be chosen as an example // and putting "player" there was raising a major code smell } } Remove(before); return (Add(after)); } } }