From afa148c5634595d84de22f795c5cdcd668ead918 Mon Sep 17 00:00:00 2001 From: Noan07 <84435602+Noan07@users.noreply.github.com> Date: Tue, 28 Feb 2023 18:03:37 +0100 Subject: [PATCH] =?UTF-8?q?:bug:=20r=C3=A9glage=20du=20bug=20sur=20les=20t?= =?UTF-8?q?ests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/MessageController.cs | 7 ++ .../Controllers/PlayerController.cs | 69 +++++++++--------- code/server/ApiLeapHit/Mapper/GameMapper.cs | 8 +- .../Mapper/GameWithIdPlayerMapper.cs | 19 ----- code/server/DTO/DTOGame.cs | 2 + code/server/DTO/Factory/ApiResponse.cs | 4 +- code/server/DataBase/Context/PongDbContext.cs | 2 +- .../DataManager/DbDataManager.Player.cs | 2 +- ... => 20230228121953_Migrations.Designer.cs} | 12 ++- ...ration.cs => 20230228121953_Migrations.cs} | 10 ++- .../PongDbContextWithStubModelSnapshot.cs | 8 ++ code/server/DataBase/PongDB.db | Bin 57344 -> 57344 bytes code/server/DataBase/PongDB.db-shm | Bin 32768 -> 0 bytes code/server/DataBase/PongDB.db-wal | 0 code/server/TestControleurs/UnitTestGames.cs | 13 +++- 15 files changed, 88 insertions(+), 68 deletions(-) delete mode 100644 code/server/ApiLeapHit/Mapper/GameWithIdPlayerMapper.cs rename code/server/DataBase/Migrations/{20230222115848_mymigration.Designer.cs => 20230228121953_Migrations.Designer.cs} (95%) rename code/server/DataBase/Migrations/{20230222115848_mymigration.cs => 20230228121953_Migrations.cs} (95%) delete mode 100644 code/server/DataBase/PongDB.db-shm delete mode 100644 code/server/DataBase/PongDB.db-wal diff --git a/code/server/ApiLeapHit/Controllers/MessageController.cs b/code/server/ApiLeapHit/Controllers/MessageController.cs index 097fe5b..48125af 100644 --- a/code/server/ApiLeapHit/Controllers/MessageController.cs +++ b/code/server/ApiLeapHit/Controllers/MessageController.cs @@ -84,6 +84,13 @@ namespace ApiLeapHit.Controllers return NotFound(new ApiResponse("Le message n'a pas été trouvé.")); } + var response = new ApiResponse("Joueur ajouté avec succès."); + response.Links.Add(new ApiLink( + Url.Action("GetPlayer", "Player", new { id = player.playerId }), + "self", + "GET" + )); + _logger.LogInformation($"Le message avec l'identifiant {id} a été reçu avec succès."); return Ok(new ApiResponse("Message reçu avec succès.", message.ToDto())); } diff --git a/code/server/ApiLeapHit/Controllers/PlayerController.cs b/code/server/ApiLeapHit/Controllers/PlayerController.cs index 346902b..f6f16c2 100644 --- a/code/server/ApiLeapHit/Controllers/PlayerController.cs +++ b/code/server/ApiLeapHit/Controllers/PlayerController.cs @@ -23,7 +23,7 @@ namespace ApiLeapHit.Controllers } [HttpGet("{id}")] - public async Task> GetPlayer(int id) + public async Task>> GetPlayer(int id) { try { @@ -66,24 +66,25 @@ namespace ApiLeapHit.Controllers { try { - var player = dtoPlayer.ToPlayer(); + var player = dtoPlayer.ToPlayer(); - await _dataManager.AddPlayer(player); + await _dataManager.AddPlayer(player); // Ajout des liens HATEOAS - var response = new ApiResponse("Joueur ajouté avec succès."); + var response = new ApiResponse("Joueur ajouté avec succès."); response.Links.Add(new ApiLink( Url.Action("GetPlayer", "Player", new { id = player.playerId }), "self", "GET" )); + var response2 = new ApiResponse("Joueur ajouté avec succès."); return Ok(response); } catch (Exception ex) { _logger.LogError(ex, "Une erreur est survenue lors de l'ajout du joueur."); - return StatusCode((int)HttpStatusCode.InternalServerError, new ApiResponse("Une erreur est survenue lors de l'ajout du joueur.")); + return StatusCode((int)HttpStatusCode.InternalServerError, new ApiResponse($"Une erreur est survenue lors de l'ajout du joueur. {ex.Message}")); } } @@ -103,35 +104,35 @@ namespace ApiLeapHit.Controllers var response = new ApiResponse>($"La récupération des players a réussi. Nombre de players : {dtoPlayers.Count}", dtoPlayers); // Ajout des liens HATEOAS - response.Links.Add(new ApiLink( - Url.Action("GetPlayers", "Player"), - "self", - "GET" - )); - response.Links.Add(new ApiLink( - Url.Action("AddPlayer", "Player"), - "create", - "POST" - )); - - foreach (var player in dtoPlayers) - { - response.Links.Add(new ApiLink( - Url.Action("GetPlayer", "Player", new { id = player.playerId }), - "get_player", - "GET" - )); - response.Links.Add(new ApiLink( - Url.Action("RemovePlayer", "Player", new { id = player.playerId }), - "delete_player", - "DELETE" - )); - response.Links.Add(new ApiLink( - Url.Action("Put", "Player", new { id = player.playerId }), - "update_player", - "PUT" - )); - } + //response.Links.Add(new ApiLink( + // Url.Action("GetPlayers", "Player"), + // "self", + // "GET" + //)); + //response.Links.Add(new ApiLink( + // Url.Action("AddPlayer", "Player"), + // "create", + // "POST" + //)); + + //foreach (var player in dtoPlayers) + //{ + // response.Links.Add(new ApiLink( + // Url.Action("GetPlayer", "Player", new { id = player.playerId }), + // "get_player", + // "GET" + // )); + // response.Links.Add(new ApiLink( + // Url.Action("RemovePlayer", "Player", new { id = player.playerId }), + // "delete_player", + // "DELETE" + // )); + // response.Links.Add(new ApiLink( + // Url.Action("Put", "Player", new { id = player.playerId }), + // "update_player", + // "PUT" + // )); + //} return Ok(response); } diff --git a/code/server/ApiLeapHit/Mapper/GameMapper.cs b/code/server/ApiLeapHit/Mapper/GameMapper.cs index fa7d34a..1500e70 100644 --- a/code/server/ApiLeapHit/Mapper/GameMapper.cs +++ b/code/server/ApiLeapHit/Mapper/GameMapper.cs @@ -13,7 +13,9 @@ namespace ApiLeapHit.Mapper durationGame = game.durationGame, nbMaxEchanges = game.nbMaxEchanges, playerWinner = game.winner, - playerLoser = game.loser + playerLoser = game.loser, + scoreLoser = game.loser, + scoreWinner = game.winner }; return dtoGame; } @@ -25,7 +27,9 @@ namespace ApiLeapHit.Mapper durationGame = dtoGame.durationGame, nbMaxEchanges = dtoGame.nbMaxEchanges, winner = dtoGame.playerWinner, - loser = dtoGame.playerLoser + loser = dtoGame.playerLoser, + scoreLoser = dtoGame.scoreLoser, + scoreWinner = dtoGame.scoreWinner }; } } diff --git a/code/server/ApiLeapHit/Mapper/GameWithIdPlayerMapper.cs b/code/server/ApiLeapHit/Mapper/GameWithIdPlayerMapper.cs deleted file mode 100644 index 33fa3d2..0000000 --- a/code/server/ApiLeapHit/Mapper/GameWithIdPlayerMapper.cs +++ /dev/null @@ -1,19 +0,0 @@ -using DataBase.Entity; -using DTO; - -namespace ApiLeapHit.Mapper -{ - public static class GameWithIdPlayerMapper - { - public static Game ToGame(this DTOGameWithIdPlayer dtoGame, Player winner, Player loser) - { - return new Game - { - durationGame = dtoGame.durationGame, - nbMaxEchanges = dtoGame.nbMaxEchanges, - winner = winner.playerId, - loser = loser.playerId - }; - } - } -} diff --git a/code/server/DTO/DTOGame.cs b/code/server/DTO/DTOGame.cs index ff430e8..c2b0fd1 100644 --- a/code/server/DTO/DTOGame.cs +++ b/code/server/DTO/DTOGame.cs @@ -13,5 +13,7 @@ namespace DTO public int nbMaxEchanges { get; set; } public int playerWinner { get; set; } public int playerLoser { get; set; } + public int scoreWinner { get; set; } + public int scoreLoser { get; set; } } } diff --git a/code/server/DTO/Factory/ApiResponse.cs b/code/server/DTO/Factory/ApiResponse.cs index 1cf32a6..76b0e76 100644 --- a/code/server/DTO/Factory/ApiResponse.cs +++ b/code/server/DTO/Factory/ApiResponse.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -10,7 +11,8 @@ namespace DTO.Factory { public string Message { get; set; } public T Data { get; set; } - public List Links { get; set; } = new List(); + public List Links { get; set; } + private List links = new(); public ApiResponse(string message, T data = default) diff --git a/code/server/DataBase/Context/PongDbContext.cs b/code/server/DataBase/Context/PongDbContext.cs index 17bad66..5829fd8 100644 --- a/code/server/DataBase/Context/PongDbContext.cs +++ b/code/server/DataBase/Context/PongDbContext.cs @@ -19,7 +19,7 @@ namespace DataBase.Context if (!optionsBuilder.IsConfigured) { - optionsBuilder.UseSqlite($"Data Source=../DataBase/PongDB.db"); + optionsBuilder.UseSqlite($"Data Source=C:\\Users\\noanr\\source\\repos\\leap-hit-server\\code\\server\\DataBase\\PongDB.db"); } } } diff --git a/code/server/DataBase/DataManager/DbDataManager.Player.cs b/code/server/DataBase/DataManager/DbDataManager.Player.cs index 6ed56a6..ce3f082 100644 --- a/code/server/DataBase/DataManager/DbDataManager.Player.cs +++ b/code/server/DataBase/DataManager/DbDataManager.Player.cs @@ -17,7 +17,7 @@ namespace DataBase.DataManager using (var context = new PongDbContext()) { await context.Players.AddAsync(player); - await context.SaveChangesAsync(); + context.SaveChangesAsync(); } } diff --git a/code/server/DataBase/Migrations/20230222115848_mymigration.Designer.cs b/code/server/DataBase/Migrations/20230228121953_Migrations.Designer.cs similarity index 95% rename from code/server/DataBase/Migrations/20230222115848_mymigration.Designer.cs rename to code/server/DataBase/Migrations/20230228121953_Migrations.Designer.cs index 6865b45..a256488 100644 --- a/code/server/DataBase/Migrations/20230222115848_mymigration.Designer.cs +++ b/code/server/DataBase/Migrations/20230228121953_Migrations.Designer.cs @@ -11,8 +11,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace DataBase.Migrations { [DbContext(typeof(PongDbContextWithStub))] - [Migration("20230222115848_mymigration")] - partial class mymigration + [Migration("20230228121953_Migrations")] + partial class Migrations { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -64,6 +64,12 @@ namespace DataBase.Migrations b.Property("nbMaxEchanges") .HasColumnType("INTEGER"); + b.Property("scoreLoser") + .HasColumnType("INTEGER"); + + b.Property("scoreWinner") + .HasColumnType("INTEGER"); + b.Property("winner") .HasColumnType("INTEGER"); @@ -82,6 +88,8 @@ namespace DataBase.Migrations durationGame = 65, loser = 2, nbMaxEchanges = 5, + scoreLoser = 2, + scoreWinner = 6, winner = 1 }); }); diff --git a/code/server/DataBase/Migrations/20230222115848_mymigration.cs b/code/server/DataBase/Migrations/20230228121953_Migrations.cs similarity index 95% rename from code/server/DataBase/Migrations/20230222115848_mymigration.cs rename to code/server/DataBase/Migrations/20230228121953_Migrations.cs index 0ea0e72..cf2b11d 100644 --- a/code/server/DataBase/Migrations/20230222115848_mymigration.cs +++ b/code/server/DataBase/Migrations/20230228121953_Migrations.cs @@ -8,7 +8,7 @@ using Microsoft.EntityFrameworkCore.Migrations; namespace DataBase.Migrations { /// - public partial class mymigration : Migration + public partial class Migrations : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) @@ -63,7 +63,9 @@ namespace DataBase.Migrations durationGame = table.Column(type: "INTEGER", nullable: false), nbMaxEchanges = table.Column(type: "INTEGER", nullable: false), winner = table.Column(type: "INTEGER", nullable: false), - loser = table.Column(type: "INTEGER", nullable: false) + loser = table.Column(type: "INTEGER", nullable: false), + scoreWinner = table.Column(type: "INTEGER", nullable: false), + scoreLoser = table.Column(type: "INTEGER", nullable: false) }, constraints: table => { @@ -126,8 +128,8 @@ namespace DataBase.Migrations migrationBuilder.InsertData( table: "Games", - columns: new[] { "gameId", "durationGame", "loser", "nbMaxEchanges", "winner" }, - values: new object[] { 1, 65, 2, 5, 1 }); + columns: new[] { "gameId", "durationGame", "loser", "nbMaxEchanges", "scoreLoser", "scoreWinner", "winner" }, + values: new object[] { 1, 65, 2, 5, 2, 6, 1 }); migrationBuilder.InsertData( table: "Messages", diff --git a/code/server/DataBase/Migrations/PongDbContextWithStubModelSnapshot.cs b/code/server/DataBase/Migrations/PongDbContextWithStubModelSnapshot.cs index 5abba79..e144450 100644 --- a/code/server/DataBase/Migrations/PongDbContextWithStubModelSnapshot.cs +++ b/code/server/DataBase/Migrations/PongDbContextWithStubModelSnapshot.cs @@ -61,6 +61,12 @@ namespace DataBase.Migrations b.Property("nbMaxEchanges") .HasColumnType("INTEGER"); + b.Property("scoreLoser") + .HasColumnType("INTEGER"); + + b.Property("scoreWinner") + .HasColumnType("INTEGER"); + b.Property("winner") .HasColumnType("INTEGER"); @@ -79,6 +85,8 @@ namespace DataBase.Migrations durationGame = 65, loser = 2, nbMaxEchanges = 5, + scoreLoser = 2, + scoreWinner = 6, winner = 1 }); }); diff --git a/code/server/DataBase/PongDB.db b/code/server/DataBase/PongDB.db index 4d4ba40eb910847a1642f69fec79694513408cb6..5f094b94e0c3507c543048d29eef44bbfb833ab9 100644 GIT binary patch delta 483 zcmaKny-LGS6vyw$%}sM#(_=u%phDEq4{D02ON)rMLajwAf({ZS)QYyyTS2!Df`W@K zPA)!xgX95J@By?6;!qq!q%I;(YC%(~qn_!U|Hltb>5P`n=-x{EHVKyTSgUBefm?py zJMzVq7v)fyV9#v(WFMG&$x!xhMHHW57_hV8R=EnjU1BL;aOSePoKxsBCzI*%*!YZ@ zoKBm`*+imO^PAo@LJ9ZAZ!x(-u;S{eYwYU;8a1qt6%JX}@PIWiG8kT*%4Q07F`Lhc zq5e>R*iQ%nd?5JXRqn8-oksbe4b2NvEE9agM=ZN~^94q9m62{0(vB#asa1Q^DbOZ~ zkyeo6>zVvK`3OM$f*E@)+pkz=IxQ6?$wS3c1E gQ{h2MId6RbN$~SWR#B$FrT_IB!P+mzt)0r|H|H>Y;Q#;t delta 525 zcmZ{g&1+LZ6vgMxosTztiKhX}Lu#Q2;zmOHQZd^g_=Q4o=|N)FGHi%rod)a znp{Kq3~@b*D{-|F$MMwEh1zuOPJ6!HYIc*^R_DRRa;01y5ke4l1a`90%mi|IlX1UEX}R$7o+jFG%L*X-ODKSIiHbSr7MPChJRTPUpLeV?#` zcm5l%aM>|ObR}swQd>|wK{p#|nlu}!^eN8ez)A18bT^qzIV2vZnfu9nqnmR64#J_p zA@;ITZ-8;@NNQ5TA30Rm5ZK38e8h$idxNvq8W98@Tl2TzKZPSBh#b{*_Ola?N*wr^ zUoht9Xl8cq_mYkTj<7#Or-YHLn4HX!!eEH-4FkW$b)3^oK4_?BsQ;RG1&(omJ?wh( R7NFRX&Bf%x+3{;N`3rcPdJF&n diff --git a/code/server/DataBase/PongDB.db-shm b/code/server/DataBase/PongDB.db-shm deleted file mode 100644 index fe9ac2845eca6fe6da8a63cd096d9cf9e24ece10..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 32768 zcmeIuAr62r3 builder.AddConsole()); var player = new Player { playerId = id, name = "Test Player", nbBallTouchTotal = 0, timePlayed = 3 }; var controller = new PlayerController(dataManager, loggerFactory.CreateLogger()); - await controller.AddPlayer(player.ToDto()); + + + var rep = await controller.AddPlayer(player.ToDto()); // Act var result = await controller.GetPlayer(id); var objectResult = (ObjectResult)result.Result; - var apiResponse = JsonSerializer.Deserialize>(objectResult.Value); + var apiResponse = (ApiResponse)objectResult.Value; // Assert - Assert.IsNotNull(apiResponse); + Assert.IsNotNull(objectResult); Assert.AreEqual((int)HttpStatusCode.OK, objectResult.StatusCode); Assert.AreEqual(apiResponse.Data.playerId, id); + } + } } \ No newline at end of file