From 915942b9b2c57c94048279b40494bc602400f3d3 Mon Sep 17 00:00:00 2001 From: Theo DUPIN Date: Sat, 18 Mar 2023 22:45:42 +0100 Subject: [PATCH 1/2] Ajout des Converter pour les DTO --- Sources/Model/Game.cs | 4 ++- Sources/Trek12_API/Converter/GameConverter.cs | 25 ++++++++++++++++++- .../Trek12_API/Converter/GameModeConverter.cs | 17 +++++++++++++ .../Trek12_API/Converter/GrilleConverter.cs | 20 +++++++++++++++ Sources/Trek12_API/Converter/TurnConverter.cs | 18 +++++++++++++ Sources/Trek12_API/DTO/GameDTO.cs | 5 ++-- Sources/Trek12_API/DTO/TurnDTO.cs | 3 +-- 7 files changed, 85 insertions(+), 7 deletions(-) create mode 100644 Sources/Trek12_API/Converter/GameModeConverter.cs create mode 100644 Sources/Trek12_API/Converter/GrilleConverter.cs create mode 100644 Sources/Trek12_API/Converter/TurnConverter.cs diff --git a/Sources/Model/Game.cs b/Sources/Model/Game.cs index e2f79b3..730c4b4 100644 --- a/Sources/Model/Game.cs +++ b/Sources/Model/Game.cs @@ -27,6 +27,8 @@ namespace Model public GameMode GameMode { get; set; } + public Player Player { get; set; } + //public Game(TimeSpan duration, DateOnly date, Dictionary grilles, Dictionary scores, List turns, GameMode gameMode,int id=0) //{ @@ -50,7 +52,7 @@ namespace Model scores.Add(owner, 0); GameMode = gameMode; Id = id; - + Player = owner; } public bool AddPlayerToGame(Player player) diff --git a/Sources/Trek12_API/Converter/GameConverter.cs b/Sources/Trek12_API/Converter/GameConverter.cs index af8899e..ecc1f8d 100644 --- a/Sources/Trek12_API/Converter/GameConverter.cs +++ b/Sources/Trek12_API/Converter/GameConverter.cs @@ -1,9 +1,32 @@ using Model; +using System.Collections.Generic; using Trek12_API.DTO; namespace Trek12_API.Converter { - public class GameConverter + public static class GameConverter { + public static GameDTO toDTO(this Game game) + { + var gameDTO = new GameDTO(game.Id, game.Date, game.Player.toDTO(), game.GameMode.toDTO()); + gameDTO.Duration = game.Duration; + gameDTO.Date = game.Date; + gameDTO.Turns = new List(); + foreach (var turn in game.Turns) + { + gameDTO.Turns.Add(turn.toDTO()); + } + gameDTO.Grilles = new Dictionary(); + foreach ( var grille in game.Grilles) + { + gameDTO.Grilles.Add(grille.Key.toDTO(), grille.Value.toDTO()); + } + gameDTO.Scores = new Dictionary(); + foreach ( var score in game.Scores) + { + gameDTO.Scores.Add(score.Key.toDTO(), score.Value); + } + return gameDTO; + } } } diff --git a/Sources/Trek12_API/Converter/GameModeConverter.cs b/Sources/Trek12_API/Converter/GameModeConverter.cs new file mode 100644 index 0000000..5974c3e --- /dev/null +++ b/Sources/Trek12_API/Converter/GameModeConverter.cs @@ -0,0 +1,17 @@ +using Model; +using Trek12_API.DTO; + +namespace Trek12_API.Converter +{ + public static class GameModeConverter + { + public static GamemodeDTO toDTO(this GameMode gameMode) + { + return new GamemodeDTO() + { + Id = gameMode.Id, + Name = gameMode.Name, + }; + } + } +} diff --git a/Sources/Trek12_API/Converter/GrilleConverter.cs b/Sources/Trek12_API/Converter/GrilleConverter.cs new file mode 100644 index 0000000..5842c4e --- /dev/null +++ b/Sources/Trek12_API/Converter/GrilleConverter.cs @@ -0,0 +1,20 @@ +using Model; +using Trek12_API.DTO; + +namespace Trek12_API.Converter +{ + public static class GrilleConverter + { + public static GrilleDTO toDTO(this Grille grille) + { + return new GrilleDTO() + { + Id = grille.Id, + NbChaines = grille.NbChaine, + NbZones = grille.NbZone, + MaxChaines = grille.MaxChaine, + MaxZones = grille.MaxZone + }; + } + } +} diff --git a/Sources/Trek12_API/Converter/TurnConverter.cs b/Sources/Trek12_API/Converter/TurnConverter.cs new file mode 100644 index 0000000..8e31a8f --- /dev/null +++ b/Sources/Trek12_API/Converter/TurnConverter.cs @@ -0,0 +1,18 @@ +using Model; +using Trek12_API.DTO; + +namespace Trek12_API.Converter +{ + public static class TurnConverter + { + public static TurnDTO toDTO(this Turn turn) + { + return new TurnDTO() + { + Id = turn.Id, + diceValue1 = turn.DiceValue1, + diceValue2 = turn.DiceValue2, + }; + } + } +} diff --git a/Sources/Trek12_API/DTO/GameDTO.cs b/Sources/Trek12_API/DTO/GameDTO.cs index 82ccfba..10a4997 100644 --- a/Sources/Trek12_API/DTO/GameDTO.cs +++ b/Sources/Trek12_API/DTO/GameDTO.cs @@ -9,12 +9,10 @@ namespace Trek12_API.DTO public TimeSpan Duration { get; set; } public DateOnly Date { get; set; } public List Turns { get; set; } - public Dictionary Grilles { get; set; } - public Dictionary Scores { get; set; } - public GamemodeDTO GameMode { get; set; } + public PlayerDTO Player { get; set; } public GameDTO(int id, DateOnly date, PlayerDTO owner, GamemodeDTO gameMode) { @@ -26,6 +24,7 @@ namespace Trek12_API.DTO Scores.Add(owner, 0); GameMode = gameMode; Id = id; + Player = owner; } } } diff --git a/Sources/Trek12_API/DTO/TurnDTO.cs b/Sources/Trek12_API/DTO/TurnDTO.cs index 3c7c37b..194e10e 100644 --- a/Sources/Trek12_API/DTO/TurnDTO.cs +++ b/Sources/Trek12_API/DTO/TurnDTO.cs @@ -4,7 +4,6 @@ { public int Id { get; set; } public int diceValue1 { get; set; } - public string diceValue2 { get; set; } - + public int diceValue2 { get; set; } } } From d4662f43ac615c537ddec966cccfdaf9962b34df Mon Sep 17 00:00:00 2001 From: Theo DUPIN Date: Sun, 19 Mar 2023 14:17:08 +0100 Subject: [PATCH 2/2] =?UTF-8?q?Impl=C3=A9mentation=20de=20toutes=20les=20m?= =?UTF-8?q?=C3=A9thodes=20dans=20les=20StubData?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/Model/Game.cs | 12 ++-- Sources/Model/IDataManager.cs | 6 +- Sources/Stub/Stub/Extensions.cs | 2 +- Sources/Stub/Stub/StubData.Cases.cs | 23 +++---- Sources/Stub/Stub/StubData.Games.cs | 81 +++++++++++++++++-------- Sources/Stub/Stub/StubData.GamesMode.cs | 23 +++---- Sources/Stub/Stub/StubData.Grilles.cs | 10 +-- Sources/Stub/Stub/StubData.Players.cs | 8 +-- Sources/Stub/Stub/StubData.Stats.cs | 16 ++--- Sources/Stub/Stub/StubData.Turns.cs | 23 +++---- 10 files changed, 103 insertions(+), 101 deletions(-) diff --git a/Sources/Model/Game.cs b/Sources/Model/Game.cs index 730c4b4..a8f0d8e 100644 --- a/Sources/Model/Game.cs +++ b/Sources/Model/Game.cs @@ -12,9 +12,11 @@ namespace Model public int Id { get; set; } public TimeSpan Duration { get; set; } + public DateOnly Date { get; set; } - //public ReadOnlyCollection Players { get; private set; } - //private List players = new(); + + public ReadOnlyCollection Players { get; private set; } + private List players = new(); public ReadOnlyCollection Turns { get; private set; } private List turns = new(); @@ -27,8 +29,6 @@ namespace Model public GameMode GameMode { get; set; } - public Player Player { get; set; } - //public Game(TimeSpan duration, DateOnly date, Dictionary grilles, Dictionary scores, List turns, GameMode gameMode,int id=0) //{ @@ -45,6 +45,7 @@ namespace Model public Game(DateOnly date, Player owner, GameMode gameMode, int id = 0) { Date = date; + Players = new ReadOnlyCollection(players); Grilles = new ReadOnlyDictionary(grilles); Scores = new ReadOnlyDictionary(scores); Turns = new ReadOnlyCollection(turns); @@ -52,7 +53,6 @@ namespace Model scores.Add(owner, 0); GameMode = gameMode; Id = id; - Player = owner; } public bool AddPlayerToGame(Player player) @@ -76,7 +76,7 @@ namespace Model return false; } - public bool AddCaseValue(Player player, int value, int index) + public bool AddCaseValueToPlayer(Player player, int value, int index) { if (grilles.ContainsKey(player) == true && scores.ContainsKey(player) == true) { diff --git a/Sources/Model/IDataManager.cs b/Sources/Model/IDataManager.cs index 0a5f475..304b12b 100644 --- a/Sources/Model/IDataManager.cs +++ b/Sources/Model/IDataManager.cs @@ -24,17 +24,15 @@ namespace Model Task GetNbItemsByPseudo(string charPseudo); //byId ? - } public interface IGamesManager : IGenericDataManager { Task AddPlayer(Player player); - Task AddScoreToPlayer(int id, int score); - Task AddCaseValueToPlayer(int id, int value, int index); + Task AddScoreToPlayer(int idGame, int idPlayer, int score); + Task AddCaseValueToPlayer(int idGame, int idPlayer, int value, int index); Task AddTurn(Turn turn); Task AddTime(TimeSpan time); - } public interface IGamesModeManager : IGenericDataManager diff --git a/Sources/Stub/Stub/Extensions.cs b/Sources/Stub/Stub/Extensions.cs index 5d1b109..748bf51 100644 --- a/Sources/Stub/Stub/Extensions.cs +++ b/Sources/Stub/Stub/Extensions.cs @@ -34,7 +34,7 @@ namespace Stub { if (item == null || collection.Contains(item)) { - return Task.FromResult(default(T)); + return Task.FromResult(default(T)); } collection.Add(item); return Task.FromResult(item); diff --git a/Sources/Stub/Stub/StubData.Cases.cs b/Sources/Stub/Stub/StubData.Cases.cs index bc4d855..305ef47 100644 --- a/Sources/Stub/Stub/StubData.Cases.cs +++ b/Sources/Stub/Stub/StubData.Cases.cs @@ -27,29 +27,22 @@ namespace Stub => this.parent = parent; public Task AddItem(Case? item) - { - throw new NotImplementedException(); - } + => parent.Cases.AddItem(item); public Task DeleteItem(Case? item) - { - throw new NotImplementedException(); - } + => parent.Cases.DeleteItem(item); public Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) - { - throw new NotImplementedException(); - } + => parent.Cases.GetItemsWithFilterAndOrdering( + c => true, + index, count, + orderingPropertyName, descending); public Task GetNbItems() - { - throw new NotImplementedException(); - } + => Task.FromResult(parent.Cases.Count()); public Task UpdateItem(Case? oldItem, Case? newItem) - { - throw new NotImplementedException(); - } + => parent.Cases.UpdateItem(oldItem, newItem); } } } diff --git a/Sources/Stub/Stub/StubData.Games.cs b/Sources/Stub/Stub/StubData.Games.cs index e96bb73..56a2cc2 100644 --- a/Sources/Stub/Stub/StubData.Games.cs +++ b/Sources/Stub/Stub/StubData.Games.cs @@ -23,55 +23,88 @@ namespace Stub public GamesManager(StubData parent) => this.parent = parent; - Task IGamesManager.AddCaseValueToPlayer(int id, int value, int index) + Task IGamesManager.AddCaseValueToPlayer(int idGame, int idPlayer, int value, int index) { - throw new NotImplementedException(); - } - - Task IGenericDataManager.AddItem(Game? item) - { - throw new NotImplementedException(); + var game = parent.games.FirstOrDefault(g => g.Id == idGame); + if(game == null) + { + return Task.FromResult(false); + } + var player = game.Players.FirstOrDefault(p => p.Id == idPlayer); + if(player == null) + { + return Task.FromResult(false); + } + game.AddCaseValueToPlayer(player, value, index); + return Task.FromResult(true); } Task IGamesManager.AddPlayer(Player player) { - throw new NotImplementedException(); + var game = parent.games.FirstOrDefault(); + if(game == null) + { + return Task.FromResult(false) ; + } + game.Players.AddItem(player); + return Task.FromResult(true); } - Task IGamesManager.AddScoreToPlayer(int id, int score) + Task IGamesManager.AddScoreToPlayer(int idGame, int idPlayer, int score) { - throw new NotImplementedException(); + var game = parent.games.FirstOrDefault(g => g.Id == idGame); + if( game == null) + { + return Task.FromResult(false); + } + var player = game.Players.FirstOrDefault(p => p.Id == idPlayer); + if(player == null) + { + return Task.FromResult(false); + } + game.AddScoreToPlayer(player, score); + return Task.FromResult(true); } Task IGamesManager.AddTime(TimeSpan time) { - throw new NotImplementedException(); + var game = parent.games.FirstOrDefault(); + if (game == null) + { + return Task.FromResult(false); + } + game.AddTime(time); + return Task.FromResult(true); } Task IGamesManager.AddTurn(Turn turn) { - throw new NotImplementedException(); + var game = parent.games.FirstOrDefault(); + if( game == null) + { + return Task.FromResult(false); + } + game.AddTurn(turn); + return Task.FromResult(true); } + Task IGenericDataManager.AddItem(Game? item) + => parent.games.AddItem(item); + Task IGenericDataManager.DeleteItem(Game? item) - { - throw new NotImplementedException(); - } + => parent.games.DeleteItem(item); Task> IGenericDataManager.GetItems(int index, int count, string? orderingPropertyName, bool descending) - { - throw new NotImplementedException(); - } + => parent.games.GetItemsWithFilterAndOrdering( + g => true, + index, count, + orderingPropertyName, descending); Task IGenericDataManager.GetNbItems() - { - throw new NotImplementedException(); - } + => Task.FromResult(parent.games.Count()); Task IGenericDataManager.UpdateItem(Game? oldItem, Game? newItem) - { - throw new NotImplementedException(); - } + => parent.games.UpdateItem(oldItem, newItem); } } } diff --git a/Sources/Stub/Stub/StubData.GamesMode.cs b/Sources/Stub/Stub/StubData.GamesMode.cs index 1705ab2..762775c 100644 --- a/Sources/Stub/Stub/StubData.GamesMode.cs +++ b/Sources/Stub/Stub/StubData.GamesMode.cs @@ -23,29 +23,22 @@ namespace Stub => this.parent = parent; public Task AddItem(GameMode? item) - { - throw new NotImplementedException(); - } + => parent.GamesMode.AddItem(item); public Task DeleteItem(GameMode? item) - { - throw new NotImplementedException(); - } + => parent.GamesMode.DeleteItem(item); public Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) - { - throw new NotImplementedException(); - } + => parent.GamesMode.GetItemsWithFilterAndOrdering( + g => true, + index, count, + orderingPropertyName, descending); public Task GetNbItems() - { - throw new NotImplementedException(); - } + => Task.FromResult(parent.GamesMode.Count()); public Task UpdateItem(GameMode? oldItem, GameMode? newItem) - { - throw new NotImplementedException(); - } + => parent.GamesMode.UpdateItem(oldItem, newItem); } } } diff --git a/Sources/Stub/Stub/StubData.Grilles.cs b/Sources/Stub/Stub/StubData.Grilles.cs index 1f1d140..84360a9 100644 --- a/Sources/Stub/Stub/StubData.Grilles.cs +++ b/Sources/Stub/Stub/StubData.Grilles.cs @@ -24,22 +24,22 @@ namespace Stub => this.parent = parent; public Task AddItem(Grille? item) - => parent.grilles.AddItem(item); + => parent.grilles.AddItem(item); public Task DeleteItem(Grille? item) - => parent.grilles.DeleteItem(item); + => parent.grilles.DeleteItem(item); public Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) - => parent.grilles.GetItemsWithFilterAndOrdering( + => parent.grilles.GetItemsWithFilterAndOrdering( c => true, index, count, orderingPropertyName, descending); public Task GetNbItems() - => Task.FromResult(parent.grilles.Count); + => Task.FromResult(parent.grilles.Count); public Task UpdateItem(Grille? oldItem, Grille? newItem) - => parent.grilles.UpdateItem(oldItem,newItem); + => parent.grilles.UpdateItem(oldItem,newItem); } } } diff --git a/Sources/Stub/Stub/StubData.Players.cs b/Sources/Stub/Stub/StubData.Players.cs index da8abe5..792ac5b 100644 --- a/Sources/Stub/Stub/StubData.Players.cs +++ b/Sources/Stub/Stub/StubData.Players.cs @@ -28,10 +28,10 @@ namespace Stub => parent.players.AddItem(item); public Task DeleteItem(Player? item) - => parent.players.DeleteItem(item); + => parent.players.DeleteItem(item); public Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) - => parent.players.GetItemsWithFilterAndOrdering( + => parent.players.GetItemsWithFilterAndOrdering( c => true, index, count, orderingPropertyName, descending); @@ -40,12 +40,12 @@ namespace Stub => Task.FromResult(parent.players.Count); public Task UpdateItem(Player? oldItem, Player? newItem) - => parent.players.UpdateItem(oldItem, newItem); + => parent.players.UpdateItem(oldItem, newItem); private Func filterByPseudo = (player, substring) => player.Pseudo.Contains(substring, StringComparison.InvariantCultureIgnoreCase); public Task> GetItemsByPseudo(string charPseudo, int index, int count, string? orderingPropertyName, bool descending = false) - => parent.players.GetItemsWithFilterAndOrdering(player => filterByPseudo(player, charPseudo), index, count, orderingPropertyName, descending); + => parent.players.GetItemsWithFilterAndOrdering(player => filterByPseudo(player, charPseudo), index, count, orderingPropertyName, descending); public Task GetNbItemsByPseudo(string charPseudo) { diff --git a/Sources/Stub/Stub/StubData.Stats.cs b/Sources/Stub/Stub/StubData.Stats.cs index 0af6076..0112d73 100644 --- a/Sources/Stub/Stub/StubData.Stats.cs +++ b/Sources/Stub/Stub/StubData.Stats.cs @@ -23,14 +23,10 @@ namespace Stub => this.parent = parent; public Task AddItem(Stats? item) - { - throw new NotImplementedException(); - } + => parent.stats.AddItem(item); public Task DeleteItem(Stats? item) - { - throw new NotImplementedException(); - } + => parent.stats.DeleteItem(item); public Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) => parent.stats.GetItemsWithFilterAndOrdering( @@ -39,14 +35,10 @@ namespace Stub orderingPropertyName, descending); public Task GetNbItems() - { - throw new NotImplementedException(); - } + => Task.FromResult(parent.stats.Count()); public Task UpdateItem(Stats? oldItem, Stats? newItem) - { - throw new NotImplementedException(); - } + => parent.stats.UpdateItem(oldItem, newItem); } } } diff --git a/Sources/Stub/Stub/StubData.Turns.cs b/Sources/Stub/Stub/StubData.Turns.cs index 6bd2581..36da2f1 100644 --- a/Sources/Stub/Stub/StubData.Turns.cs +++ b/Sources/Stub/Stub/StubData.Turns.cs @@ -23,29 +23,22 @@ namespace Stub => this.parent = parent; public Task AddItem(Turn? item) - { - throw new NotImplementedException(); - } + => parent.Turns.AddItem(item); public Task DeleteItem(Turn? item) - { - throw new NotImplementedException(); - } + => parent.Turns.DeleteItem(item); public Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) - { - throw new NotImplementedException(); - } + => parent.Turns.GetItemsWithFilterAndOrdering( + t => true, + index, count, + orderingPropertyName, descending); public Task GetNbItems() - { - throw new NotImplementedException(); - } + => Task.FromResult(parent.Turns.Count()); public Task UpdateItem(Turn? oldItem, Turn? newItem) - { - throw new NotImplementedException(); - } + => parent.Turns.UpdateItem(oldItem, newItem); } } }