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 4d4ba40..5f094b9 100644 Binary files a/code/server/DataBase/PongDB.db and b/code/server/DataBase/PongDB.db differ diff --git a/code/server/DataBase/PongDB.db-shm b/code/server/DataBase/PongDB.db-shm deleted file mode 100644 index fe9ac28..0000000 Binary files a/code/server/DataBase/PongDB.db-shm and /dev/null differ diff --git a/code/server/DataBase/PongDB.db-wal b/code/server/DataBase/PongDB.db-wal deleted file mode 100644 index e69de29..0000000 diff --git a/code/server/TestControleurs/UnitTestGames.cs b/code/server/TestControleurs/UnitTestGames.cs index e7c226f..d024571 100644 --- a/code/server/TestControleurs/UnitTestGames.cs +++ b/code/server/TestControleurs/UnitTestGames.cs @@ -1,5 +1,6 @@ using ApiLeapHit.Controllers; using ApiLeapHit.Mapper; +using DataBase.Context; using DataBase.DataManager; using DataBase.Entity; using DTO; @@ -20,22 +21,26 @@ namespace TestControleurs public async Task TestGetPlayer_ValidId() { // Arrange - int id = 1; + int id = 8; DbDataManager dataManager = new DbDataManager(); ILoggerFactory loggerFactory = LoggerFactory.Create(builder => 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