diff --git a/Sources/Model/Case.cs b/Sources/Model/Case.cs index fd76ae2..f9ba7f3 100644 --- a/Sources/Model/Case.cs +++ b/Sources/Model/Case.cs @@ -26,7 +26,7 @@ namespace Model public Case() { - + Valeur = 0; } public Case(int valeur) diff --git a/Sources/Model/Game.cs b/Sources/Model/Game.cs index 494dfe2..3a69f74 100644 --- a/Sources/Model/Game.cs +++ b/Sources/Model/Game.cs @@ -45,15 +45,26 @@ namespace Model public Game(DateTime date, Player ?owner, GameMode gameMode, int id = 0) { Date = date; + + // Creation list de player + ajout d'une player owner dans la liste players = new(); - turns = new(); Players = new ReadOnlyCollection(players); - Grilles = new ReadOnlyDictionary(new Dictionary()); - Scores = new ReadOnlyDictionary(new Dictionary()); - Turns = new ReadOnlyCollection(turns); - grilles.Add(owner, new Grille()); - scores.Add(owner, 0); players.Add(owner); + + turns = new(); + Turns = new ReadOnlyCollection(turns); + + if(grilles != null) + { + Grilles = new ReadOnlyDictionary(new Dictionary()); + grilles.Add(owner, new Grille()); + } + + if(scores != null) + { + Scores = new ReadOnlyDictionary(new Dictionary()); + scores.Add(owner, 0); + } GameMode = gameMode; Id = id; } diff --git a/Sources/Stub/Stub/StubData.Games.cs b/Sources/Stub/Stub/StubData.Games.cs index c0bdd3e..c5e4a85 100644 --- a/Sources/Stub/Stub/StubData.Games.cs +++ b/Sources/Stub/Stub/StubData.Games.cs @@ -13,10 +13,10 @@ namespace Stub { private List games = new() { - new Game(new DateTime(), new Player("Aurélien",0),new GameMode()), - new Game(new DateTime(), new Player("Maxence",1),new GameMode()), - new Game(new DateTime(), new Player("Théo",2),new GameMode()), - new Game(new DateTime(), new Player("Zakariya",3),new GameMode()), + new Game(new DateTime(), new Player("Aurélien",0),new GameMode(),1), + new Game(new DateTime(), new Player("Maxence",1),new GameMode(),2), + new Game(new DateTime(), new Player("Théo",2),new GameMode(),3), + new Game(new DateTime(), new Player("Zakariya",3),new GameMode(),4), }; public class GamesManager : IGamesManager diff --git a/Sources/Trek12_API/Controllers/GameController.cs b/Sources/Trek12_API/Controllers/GameController.cs index c7d0de5..4aeec20 100644 --- a/Sources/Trek12_API/Controllers/GameController.cs +++ b/Sources/Trek12_API/Controllers/GameController.cs @@ -11,7 +11,7 @@ namespace Trek12_API.Controllers public class GameController: ControllerBase { private readonly ILogger _logger; - private StubData.GamesManager gamesManager { get; set; } = new StubData.GamesManager(new StubData()); + private GamesManager gamesManager { get; set; } = new StubData.GamesManager(new StubData()); public GameController(ILogger logger) diff --git a/Sources/Trek12_API/Converter/PlayerConverter.cs b/Sources/Trek12_API/Converter/PlayerConverter.cs index d4099bb..6cca230 100644 --- a/Sources/Trek12_API/Converter/PlayerConverter.cs +++ b/Sources/Trek12_API/Converter/PlayerConverter.cs @@ -1,4 +1,5 @@ -using Model; +using System.Text.Json.Serialization; +using Model; using Trek12_API.DTO; namespace Trek12_API.Converter diff --git a/Sources/Trek12_API/JsonConverter/ReadOnlyDictionnaryConverter.cs b/Sources/Trek12_API/JsonConverter/ReadOnlyDictionnaryConverter.cs new file mode 100644 index 0000000..5a7b3ff --- /dev/null +++ b/Sources/Trek12_API/JsonConverter/ReadOnlyDictionnaryConverter.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.ObjectModel; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Trek12_API.JsonConverter +{ + public class ReadOnlyDictionnaryConverter: JsonConverter> + { + public override ReadOnlyDictionary? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + Dictionary dictionary = JsonSerializer.Deserialize>(ref reader, options); + return new ReadOnlyDictionary(dictionary); + } + + public override void Write(Utf8JsonWriter writer, ReadOnlyDictionary value, JsonSerializerOptions options) + { + JsonSerializer.Serialize(writer, value.ToDictionary(kv => kv.Key, kv => kv.Value),options); + } + + } +} + diff --git a/Sources/Trek12_API/Program.cs b/Sources/Trek12_API/Program.cs index 62b4262..3ec7642 100644 --- a/Sources/Trek12_API/Program.cs +++ b/Sources/Trek12_API/Program.cs @@ -1,12 +1,20 @@ -var builder = WebApplication.CreateBuilder(args); +using Model; +using Trek12_API.JsonConverter; + +var builder = WebApplication.CreateBuilder(args); // Add services to the container. -builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); +builder.Services.AddControllers().AddJsonOptions(options => +{ + options.JsonSerializerOptions.Converters.Add(new ReadOnlyDictionnaryConverter()); + options.JsonSerializerOptions.Converters.Add(new ReadOnlyDictionnaryConverter()); +}); + var app = builder.Build(); // Configure the HTTP request pipeline. diff --git a/Sources/Trek12_API/Trek12_API.csproj b/Sources/Trek12_API/Trek12_API.csproj index 009f215..27ce4c2 100644 --- a/Sources/Trek12_API/Trek12_API.csproj +++ b/Sources/Trek12_API/Trek12_API.csproj @@ -16,4 +16,10 @@ + + + + + + diff --git a/Sources/Trek12_Lib/EntityFrameWorkLib.csproj b/Sources/Trek12_Lib/EntityFrameWorkLib.csproj index b8e4722..473649d 100644 --- a/Sources/Trek12_Lib/EntityFrameWorkLib.csproj +++ b/Sources/Trek12_Lib/EntityFrameWorkLib.csproj @@ -13,9 +13,9 @@ - - - + + + runtime; build; native; contentfiles; analyzers; buildtransitive all