🔨 add jsonconverter for dictionary, requetes for games OK
continuous-integration/drone/push Build is failing Details

master
Maxence LANONE 2 years ago
parent 7b5206b161
commit 8f8536ab1d

@ -26,7 +26,7 @@ namespace Model
public Case() public Case()
{ {
Valeur = 0;
} }
public Case(int valeur) public Case(int valeur)

@ -45,15 +45,26 @@ namespace Model
public Game(DateTime date, Player ?owner, GameMode gameMode, int id = 0) public Game(DateTime date, Player ?owner, GameMode gameMode, int id = 0)
{ {
Date = date; Date = date;
// Creation list de player + ajout d'une player owner dans la liste
players = new(); players = new();
turns = new();
Players = new ReadOnlyCollection<Player>(players); Players = new ReadOnlyCollection<Player>(players);
Grilles = new ReadOnlyDictionary<Player, Grille>(new Dictionary<Player, Grille>());
Scores = new ReadOnlyDictionary<Player, int>(new Dictionary<Player, int>());
Turns = new ReadOnlyCollection<Turn>(turns);
grilles.Add(owner, new Grille());
scores.Add(owner, 0);
players.Add(owner); players.Add(owner);
turns = new();
Turns = new ReadOnlyCollection<Turn>(turns);
if(grilles != null)
{
Grilles = new ReadOnlyDictionary<Player, Grille>(new Dictionary<Player, Grille>());
grilles.Add(owner, new Grille());
}
if(scores != null)
{
Scores = new ReadOnlyDictionary<Player, int>(new Dictionary<Player, int>());
scores.Add(owner, 0);
}
GameMode = gameMode; GameMode = gameMode;
Id = id; Id = id;
} }

@ -13,10 +13,10 @@ namespace Stub
{ {
private List<Game> games = new() private List<Game> games = new()
{ {
new Game(new DateTime(), new Player("Aurélien",0),new GameMode()), new Game(new DateTime(), new Player("Aurélien",0),new GameMode(),1),
new Game(new DateTime(), new Player("Maxence",1),new GameMode()), new Game(new DateTime(), new Player("Maxence",1),new GameMode(),2),
new Game(new DateTime(), new Player("Théo",2),new GameMode()), new Game(new DateTime(), new Player("Théo",2),new GameMode(),3),
new Game(new DateTime(), new Player("Zakariya",3),new GameMode()), new Game(new DateTime(), new Player("Zakariya",3),new GameMode(),4),
}; };
public class GamesManager : IGamesManager public class GamesManager : IGamesManager

@ -11,7 +11,7 @@ namespace Trek12_API.Controllers
public class GameController: ControllerBase public class GameController: ControllerBase
{ {
private readonly ILogger<GameController> _logger; private readonly ILogger<GameController> _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<GameController> logger) public GameController(ILogger<GameController> logger)

@ -1,4 +1,5 @@
using Model; using System.Text.Json.Serialization;
using Model;
using Trek12_API.DTO; using Trek12_API.DTO;
namespace Trek12_API.Converter namespace Trek12_API.Converter

@ -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<TKey,TValue>: JsonConverter<ReadOnlyDictionary<TKey,TValue>>
{
public override ReadOnlyDictionary<TKey, TValue>? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
Dictionary<TKey, TValue> dictionary = JsonSerializer.Deserialize<Dictionary<TKey, TValue>>(ref reader, options);
return new ReadOnlyDictionary<TKey, TValue>(dictionary);
}
public override void Write(Utf8JsonWriter writer, ReadOnlyDictionary<TKey, TValue> value, JsonSerializerOptions options)
{
JsonSerializer.Serialize(writer, value.ToDictionary(kv => kv.Key, kv => kv.Value),options);
}
}
}

@ -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. // Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen();
builder.Services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new ReadOnlyDictionnaryConverter<Player, Grille>());
options.JsonSerializerOptions.Converters.Add(new ReadOnlyDictionnaryConverter<int, Player>());
});
var app = builder.Build(); var app = builder.Build();
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.

@ -16,4 +16,10 @@
<ProjectReference Include="..\Stub\Stub\Stub.csproj" /> <ProjectReference Include="..\Stub\Stub\Stub.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Remove="JsonConverter\" />
</ItemGroup>
<ItemGroup>
<Folder Include="JsonConverter\" />
</ItemGroup>
</Project> </Project>

@ -13,9 +13,9 @@
<None Remove="Microsoft.EntityFrameworkCore.Tools" /> <None Remove="Microsoft.EntityFrameworkCore.Tools" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.2" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.2" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.2"> <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
</PackageReference> </PackageReference>

Loading…
Cancel
Save