diff --git a/Src/Giants.Application/Src/MatchRepositoryMock.cs b/Src/Giants.Application/Src/MatchRepositoryMock.cs index 461a1a1..e71a5d9 100644 --- a/Src/Giants.Application/Src/MatchRepositoryMock.cs +++ b/Src/Giants.Application/Src/MatchRepositoryMock.cs @@ -10,6 +10,9 @@ namespace Giants.Application; public class MatchRepositoryMock : IMatchRepository { private readonly IHexagonalGrid _gridLayout; + private static int _lastId = 1; + + private Dictionary _matchs = new Dictionary(); /// /// constructeur @@ -22,10 +25,31 @@ public class MatchRepositoryMock : IMatchRepository /// public Match? GetMatch(int matchId) { - Match result = new Match(_gridLayout); + if (_matchs.TryGetValue(matchId, out Match? found)) + { + return found; + } + return null; + } + + /// + public Match CreateMatch() + { + Match result = new Match(_gridLayout) + { + Id = _lastId++ + }; + + AddStaticData(result); + + _matchs.Add(result.Id, result); - // Ajout des données statiques return result; } + private void AddStaticData(Match match) + { + + } + } \ No newline at end of file diff --git a/Src/Giants.Core/Src/Commands/BaseCommandResult.cs b/Src/Giants.Core/Src/Commands/BaseCommandResult.cs new file mode 100644 index 0000000..10ecb46 --- /dev/null +++ b/Src/Giants.Core/Src/Commands/BaseCommandResult.cs @@ -0,0 +1,11 @@ +namespace Giants.Core.Commands; + +/// +/// Class de base des resultats de commande. Chaque commande doit retourner un resultat quand elle +/// s'execute et les données de ce resultat est propre a la commande +/// +public abstract class BaseCommandResult +{ + /// indique si le resultat est un success (true) ou un echec(false) + public bool Success { get; init; } = false; +} \ No newline at end of file diff --git a/Src/Giants.Core/Src/Commands/CommandBase.cs b/Src/Giants.Core/Src/Commands/CommandBase.cs new file mode 100644 index 0000000..38c62e9 --- /dev/null +++ b/Src/Giants.Core/Src/Commands/CommandBase.cs @@ -0,0 +1,17 @@ +namespace Giants.Core.Commands; + +/// +/// Base des commande permettant d'agir sur le modele +/// +/// Le type de resultat que retourne cette commande +public abstract class BaseCommand where T : BaseCommandResult +{ + protected abstract T LocalExecute(); + + /// Executer la commande, si nécessaire il faut avoir rempli les parametres en amont + public T Execute() + { + // C'est ici que sera validé l'exécution + return LocalExecute(); + } +} \ No newline at end of file diff --git a/Src/Giants.Core/Src/Commands/NewMatchCommand.cs b/Src/Giants.Core/Src/Commands/NewMatchCommand.cs new file mode 100644 index 0000000..d643f48 --- /dev/null +++ b/Src/Giants.Core/Src/Commands/NewMatchCommand.cs @@ -0,0 +1,38 @@ +using Giants.Core.Interfaces; + +namespace Giants.Core.Commands; + +/// +/// Commande permettant de créer un nouveau match +/// +public class NewMatchCommand : BaseCommand +{ + private readonly IMatchRepository _repo; + + /// + /// constructeur + /// + /// le repository de match + public NewMatchCommand(IMatchRepository repo) + { + _repo = repo; + } + + /// + protected override sealed NewMatchResult LocalExecute() + { + var match = _repo.CreateMatch(); + + return new NewMatchResult() + { + Match = match, + Success = match != null + }; + } +} + +public class NewMatchResult : BaseCommandResult +{ + /// Le match créé si success + public Match? Match { get; init; } +} \ No newline at end of file diff --git a/Src/Giants.Core/Src/Entities/Match.cs b/Src/Giants.Core/Src/Entities/Match.cs index e95bd53..2976233 100644 --- a/Src/Giants.Core/Src/Entities/Match.cs +++ b/Src/Giants.Core/Src/Entities/Match.cs @@ -11,6 +11,7 @@ namespace Giants.Core; public class Match { #region données statiques + public int Id { get; init; } IHexagonalGrid _grid; #endregion diff --git a/Src/Giants.Core/Src/Interfaces/IMatchRepository.cs b/Src/Giants.Core/Src/Interfaces/IMatchRepository.cs index 0f548e2..4879a2b 100644 --- a/Src/Giants.Core/Src/Interfaces/IMatchRepository.cs +++ b/Src/Giants.Core/Src/Interfaces/IMatchRepository.cs @@ -11,4 +11,10 @@ public interface IMatchRepository /// L'id unique du match a récupérer /// le match s'il existe ou null dans le cas contraire Match? GetMatch(int matchId); + + /// + /// Créer un match avec un ID unique + /// + /// le nouveau match a son état initial + Match? CreateMatch(); } \ No newline at end of file diff --git a/Tests/Giants.Core.Tests/Commands/NewMatchCommandTest.cs b/Tests/Giants.Core.Tests/Commands/NewMatchCommandTest.cs new file mode 100644 index 0000000..b0d28cd --- /dev/null +++ b/Tests/Giants.Core.Tests/Commands/NewMatchCommandTest.cs @@ -0,0 +1,34 @@ +namespace Giants.Core.Tests; + +using Giants.Application; +using Giants.Core.Commands; +using Giants.Core.Interfaces; + +public class NewMatchCommandTest +{ + private readonly IMatchRepository _repo = new MatchRepositoryMock(); + + [Fact] + public void CreateBasicMatch() + { + NewMatchCommand command = new NewMatchCommand(_repo); + NewMatchResult result = command.Execute(); + Assert.True(result.Success); + } + + [Fact] + public void TwoNewMatchShouldHaveDifferentID() + { + NewMatchCommand command = new NewMatchCommand(_repo); + NewMatchResult result = command.Execute(); + Assert.True(result.Success); + Assert.NotNull(result.Match); + + NewMatchCommand command2 = new NewMatchCommand(_repo); + NewMatchResult result2 = command2.Execute(); + Assert.True(result2.Success); + Assert.NotNull(result2.Match); + + Assert.NotEqual(result.Match.Id, result2.Match.Id); + } +}