using Model.Dice.Faces; using System; using System.Collections.Generic; using System.Linq; namespace Model.Dice { public class DieManager : IManager>>> { private readonly Dictionary>> diceGroups = new(); public KeyValuePair>> Add(KeyValuePair>> toAdd) { // on trim la clé d'abord if (string.IsNullOrWhiteSpace(toAdd.Key)) { throw new ArgumentNullException(nameof(toAdd), "param should not be null or empty"); } if (diceGroups.Contains(toAdd)) { throw new ArgumentException("this username is already taken", nameof(toAdd)); } diceGroups.Add(toAdd.Key.Trim(), toAdd.Value); return toAdd; } public IEnumerable>>> GetAll() { return diceGroups.AsEnumerable(); } public KeyValuePair>> GetOneByID(Guid ID) { throw new NotImplementedException(); } public KeyValuePair>> GetOneByName(string name) { // les groupes de dés nommés : // ils sont case-sensistive, mais "mon jeu" == "mon jeu " == " mon jeu" if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentNullException(nameof(name), "param should not be null or empty"); } else { return new KeyValuePair>>(name, diceGroups[name]); } } public void Remove(KeyValuePair>> toRemove) { if (toRemove.Key is null) { throw new ArgumentNullException(nameof(toRemove), "param should not be null"); } else { diceGroups.Remove(toRemove.Key); } } public KeyValuePair>> Update(KeyValuePair>> before, KeyValuePair>> after) { // pas autorisé de changer les dés, juste le nom if (!before.Value.Equals(after.Value)) { if (string.IsNullOrWhiteSpace(before.Key) || string.IsNullOrWhiteSpace(after.Key)) { throw new ArgumentNullException(nameof(before), "dice group name should not be null or empty"); } diceGroups.Remove(before.Key); diceGroups.Add(after.Key, after.Value); return after; } return before; } } }