ajout de la commande de creation de match
This commit is contained in:
@@ -10,6 +10,9 @@ namespace Giants.Application;
|
|||||||
public class MatchRepositoryMock : IMatchRepository
|
public class MatchRepositoryMock : IMatchRepository
|
||||||
{
|
{
|
||||||
private readonly IHexagonalGrid _gridLayout;
|
private readonly IHexagonalGrid _gridLayout;
|
||||||
|
private static int _lastId = 1;
|
||||||
|
|
||||||
|
private Dictionary<int, Match> _matchs = new Dictionary<int, Match>();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// constructeur
|
/// constructeur
|
||||||
@@ -22,10 +25,31 @@ public class MatchRepositoryMock : IMatchRepository
|
|||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public Match? GetMatch(int matchId)
|
public Match? GetMatch(int matchId)
|
||||||
{
|
{
|
||||||
Match result = new Match(_gridLayout);
|
if (_matchs.TryGetValue(matchId, out Match? found))
|
||||||
|
{
|
||||||
|
return found;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public Match CreateMatch()
|
||||||
|
{
|
||||||
|
Match result = new Match(_gridLayout)
|
||||||
|
{
|
||||||
|
Id = _lastId++
|
||||||
|
};
|
||||||
|
|
||||||
|
AddStaticData(result);
|
||||||
|
|
||||||
|
_matchs.Add(result.Id, result);
|
||||||
|
|
||||||
// Ajout des données statiques
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void AddStaticData(Match match)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
11
Src/Giants.Core/Src/Commands/BaseCommandResult.cs
Normal file
11
Src/Giants.Core/Src/Commands/BaseCommandResult.cs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
namespace Giants.Core.Commands;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 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
|
||||||
|
/// </summary>
|
||||||
|
public abstract class BaseCommandResult
|
||||||
|
{
|
||||||
|
/// <summary> indique si le resultat est un success (true) ou un echec(false) </summary>
|
||||||
|
public bool Success { get; init; } = false;
|
||||||
|
}
|
||||||
17
Src/Giants.Core/Src/Commands/CommandBase.cs
Normal file
17
Src/Giants.Core/Src/Commands/CommandBase.cs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
namespace Giants.Core.Commands;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Base des commande permettant d'agir sur le modele
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">Le type de resultat que retourne cette commande </typeparam>
|
||||||
|
public abstract class BaseCommand<T> where T : BaseCommandResult
|
||||||
|
{
|
||||||
|
protected abstract T LocalExecute();
|
||||||
|
|
||||||
|
/// <summary> Executer la commande, si nécessaire il faut avoir rempli les parametres en amont </summary>
|
||||||
|
public T Execute()
|
||||||
|
{
|
||||||
|
// C'est ici que sera validé l'exécution
|
||||||
|
return LocalExecute();
|
||||||
|
}
|
||||||
|
}
|
||||||
38
Src/Giants.Core/Src/Commands/NewMatchCommand.cs
Normal file
38
Src/Giants.Core/Src/Commands/NewMatchCommand.cs
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
using Giants.Core.Interfaces;
|
||||||
|
|
||||||
|
namespace Giants.Core.Commands;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Commande permettant de créer un nouveau match
|
||||||
|
/// </summary>
|
||||||
|
public class NewMatchCommand : BaseCommand<NewMatchResult>
|
||||||
|
{
|
||||||
|
private readonly IMatchRepository _repo;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// constructeur
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="repo">le repository de match</param>
|
||||||
|
public NewMatchCommand(IMatchRepository repo)
|
||||||
|
{
|
||||||
|
_repo = repo;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
protected override sealed NewMatchResult LocalExecute()
|
||||||
|
{
|
||||||
|
var match = _repo.CreateMatch();
|
||||||
|
|
||||||
|
return new NewMatchResult()
|
||||||
|
{
|
||||||
|
Match = match,
|
||||||
|
Success = match != null
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class NewMatchResult : BaseCommandResult
|
||||||
|
{
|
||||||
|
/// <summary> Le match créé si success </summary>
|
||||||
|
public Match? Match { get; init; }
|
||||||
|
}
|
||||||
@@ -11,6 +11,7 @@ namespace Giants.Core;
|
|||||||
public class Match
|
public class Match
|
||||||
{
|
{
|
||||||
#region données statiques
|
#region données statiques
|
||||||
|
public int Id { get; init; }
|
||||||
IHexagonalGrid _grid;
|
IHexagonalGrid _grid;
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|||||||
@@ -11,4 +11,10 @@ public interface IMatchRepository
|
|||||||
/// <param name="matchId">L'id unique du match a récupérer</param>
|
/// <param name="matchId">L'id unique du match a récupérer</param>
|
||||||
/// <returns>le match s'il existe ou null dans le cas contraire</returns>
|
/// <returns>le match s'il existe ou null dans le cas contraire</returns>
|
||||||
Match? GetMatch(int matchId);
|
Match? GetMatch(int matchId);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Créer un match avec un ID unique
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>le nouveau match a son état initial</returns>
|
||||||
|
Match? CreateMatch();
|
||||||
}
|
}
|
||||||
34
Tests/Giants.Core.Tests/Commands/NewMatchCommandTest.cs
Normal file
34
Tests/Giants.Core.Tests/Commands/NewMatchCommandTest.cs
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user