using System; using System.Collections.Generic; using System.Linq; namespace Model.Games { public class GameManager : IManager { /// /// the games managed by this instance /// private readonly List games; public GameManager() { games = new(); } /// /// gets an unmodifiable collection of the games /// /// unmodifiable collection of the games public IEnumerable GetAll() => games.AsEnumerable(); /// /// 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 Game GetOneByName(string name) { if (!string.IsNullOrWhiteSpace(name)) { Game result = games.FirstOrDefault(g => g.Name == name); return result; // may return null } throw new ArgumentException("param should not be null or blank", nameof(name)); } /// /// not implemented in the model /// /// /// /// public Game 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 Game 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 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 Game 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); } } }