From 55d1f95fd1a09c0510b321da552519783ca5df96 Mon Sep 17 00:00:00 2001 From: Alexis DRAI Date: Thu, 29 Sep 2022 21:30:14 +0200 Subject: [PATCH] :memo: Add comments and validation to DieManager --- Sources/Model/Dice/DieManager.cs | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/Sources/Model/Dice/DieManager.cs b/Sources/Model/Dice/DieManager.cs index 92da094..06e3a52 100644 --- a/Sources/Model/Dice/DieManager.cs +++ b/Sources/Model/Dice/DieManager.cs @@ -1,4 +1,5 @@ using Model.Dice.Faces; +using System; using System.Collections.Generic; using System.Linq; @@ -10,7 +11,8 @@ namespace Model.Dice public KeyValuePair>> Add(KeyValuePair>> toAdd) { - diceGroups.Add(toAdd.Key, toAdd.Value); + // on trim la clé d'abord + diceGroups.Add(toAdd.Key.Trim(), toAdd.Value); return toAdd; } @@ -21,6 +23,8 @@ namespace Model.Dice public KeyValuePair>> GetOneByName(string name) { + // les groupes de dés nommés : + // ils sont case-sensistive, mais "mon jeu" == "mon jeu " == " mon jeu" return new KeyValuePair>>(name, diceGroups[name]); } @@ -31,11 +35,19 @@ namespace Model.Dice public KeyValuePair>> Update(KeyValuePair>> before, KeyValuePair>> after) { - // check if key 1 exists - // check if both keys same - diceGroups.Remove(before.Key); - diceGroups.Add(after.Key, after.Value); - return 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; } } }