🐛 réglage du bug sur les tests
continuous-integration/drone/push Build is passing Details

Api
Noan07 2 years ago
parent e1c488e252
commit afa148c563

@ -84,6 +84,13 @@ namespace ApiLeapHit.Controllers
return NotFound(new ApiResponse<object>("Le message n'a pas été trouvé.")); return NotFound(new ApiResponse<object>("Le message n'a pas été trouvé."));
} }
var response = new ApiResponse<DTOMessage>("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."); _logger.LogInformation($"Le message avec l'identifiant {id} a été reçu avec succès.");
return Ok(new ApiResponse<DTOMessage>("Message reçu avec succès.", message.ToDto())); return Ok(new ApiResponse<DTOMessage>("Message reçu avec succès.", message.ToDto()));
} }

@ -23,7 +23,7 @@ namespace ApiLeapHit.Controllers
} }
[HttpGet("{id}")] [HttpGet("{id}")]
public async Task<ActionResult<DTOPlayer>> GetPlayer(int id) public async Task<ActionResult<ApiResponse<DTOPlayer>>> GetPlayer(int id)
{ {
try try
{ {
@ -66,24 +66,25 @@ namespace ApiLeapHit.Controllers
{ {
try try
{ {
var player = dtoPlayer.ToPlayer(); var player = dtoPlayer.ToPlayer();
await _dataManager.AddPlayer(player); await _dataManager.AddPlayer(player);
// Ajout des liens HATEOAS // Ajout des liens HATEOAS
var response = new ApiResponse<object>("Joueur ajouté avec succès."); var response = new ApiResponse<DTOPlayer>("Joueur ajouté avec succès.");
response.Links.Add(new ApiLink( response.Links.Add(new ApiLink(
Url.Action("GetPlayer", "Player", new { id = player.playerId }), Url.Action("GetPlayer", "Player", new { id = player.playerId }),
"self", "self",
"GET" "GET"
)); ));
var response2 = new ApiResponse<object>("Joueur ajouté avec succès.");
return Ok(response); return Ok(response);
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Une erreur est survenue lors de l'ajout du joueur."); _logger.LogError(ex, "Une erreur est survenue lors de l'ajout du joueur.");
return StatusCode((int)HttpStatusCode.InternalServerError, new ApiResponse<object>("Une erreur est survenue lors de l'ajout du joueur.")); return StatusCode((int)HttpStatusCode.InternalServerError, new ApiResponse<object>($"Une erreur est survenue lors de l'ajout du joueur. {ex.Message}"));
} }
} }
@ -103,35 +104,35 @@ namespace ApiLeapHit.Controllers
var response = new ApiResponse<IEnumerable<DTOPlayer>>($"La récupération des players a réussi. Nombre de players : {dtoPlayers.Count}", dtoPlayers); var response = new ApiResponse<IEnumerable<DTOPlayer>>($"La récupération des players a réussi. Nombre de players : {dtoPlayers.Count}", dtoPlayers);
// Ajout des liens HATEOAS // Ajout des liens HATEOAS
response.Links.Add(new ApiLink( //response.Links.Add(new ApiLink(
Url.Action("GetPlayers", "Player"), // Url.Action("GetPlayers", "Player"),
"self", // "self",
"GET" // "GET"
)); //));
response.Links.Add(new ApiLink( //response.Links.Add(new ApiLink(
Url.Action("AddPlayer", "Player"), // Url.Action("AddPlayer", "Player"),
"create", // "create",
"POST" // "POST"
)); //));
foreach (var player in dtoPlayers) //foreach (var player in dtoPlayers)
{ //{
response.Links.Add(new ApiLink( // response.Links.Add(new ApiLink(
Url.Action("GetPlayer", "Player", new { id = player.playerId }), // Url.Action("GetPlayer", "Player", new { id = player.playerId }),
"get_player", // "get_player",
"GET" // "GET"
)); // ));
response.Links.Add(new ApiLink( // response.Links.Add(new ApiLink(
Url.Action("RemovePlayer", "Player", new { id = player.playerId }), // Url.Action("RemovePlayer", "Player", new { id = player.playerId }),
"delete_player", // "delete_player",
"DELETE" // "DELETE"
)); // ));
response.Links.Add(new ApiLink( // response.Links.Add(new ApiLink(
Url.Action("Put", "Player", new { id = player.playerId }), // Url.Action("Put", "Player", new { id = player.playerId }),
"update_player", // "update_player",
"PUT" // "PUT"
)); // ));
} //}
return Ok(response); return Ok(response);
} }

@ -13,7 +13,9 @@ namespace ApiLeapHit.Mapper
durationGame = game.durationGame, durationGame = game.durationGame,
nbMaxEchanges = game.nbMaxEchanges, nbMaxEchanges = game.nbMaxEchanges,
playerWinner = game.winner, playerWinner = game.winner,
playerLoser = game.loser playerLoser = game.loser,
scoreLoser = game.loser,
scoreWinner = game.winner
}; };
return dtoGame; return dtoGame;
} }
@ -25,7 +27,9 @@ namespace ApiLeapHit.Mapper
durationGame = dtoGame.durationGame, durationGame = dtoGame.durationGame,
nbMaxEchanges = dtoGame.nbMaxEchanges, nbMaxEchanges = dtoGame.nbMaxEchanges,
winner = dtoGame.playerWinner, winner = dtoGame.playerWinner,
loser = dtoGame.playerLoser loser = dtoGame.playerLoser,
scoreLoser = dtoGame.scoreLoser,
scoreWinner = dtoGame.scoreWinner
}; };
} }
} }

@ -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
};
}
}
}

@ -13,5 +13,7 @@ namespace DTO
public int nbMaxEchanges { get; set; } public int nbMaxEchanges { get; set; }
public int playerWinner { get; set; } public int playerWinner { get; set; }
public int playerLoser { get; set; } public int playerLoser { get; set; }
public int scoreWinner { get; set; }
public int scoreLoser { get; set; }
} }
} }

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -10,7 +11,8 @@ namespace DTO.Factory
{ {
public string Message { get; set; } public string Message { get; set; }
public T Data { get; set; } public T Data { get; set; }
public List<ApiLink> Links { get; set; } = new List<ApiLink>(); public List<ApiLink> Links { get; set; }
private List<ApiLink> links = new();
public ApiResponse(string message, T data = default) public ApiResponse(string message, T data = default)

@ -19,7 +19,7 @@ namespace DataBase.Context
if (!optionsBuilder.IsConfigured) 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");
} }
} }
} }

@ -17,7 +17,7 @@ namespace DataBase.DataManager
using (var context = new PongDbContext()) using (var context = new PongDbContext())
{ {
await context.Players.AddAsync(player); await context.Players.AddAsync(player);
await context.SaveChangesAsync(); context.SaveChangesAsync();
} }
} }

@ -11,8 +11,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace DataBase.Migrations namespace DataBase.Migrations
{ {
[DbContext(typeof(PongDbContextWithStub))] [DbContext(typeof(PongDbContextWithStub))]
[Migration("20230222115848_mymigration")] [Migration("20230228121953_Migrations")]
partial class mymigration partial class Migrations
{ {
/// <inheritdoc /> /// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder) protected override void BuildTargetModel(ModelBuilder modelBuilder)
@ -64,6 +64,12 @@ namespace DataBase.Migrations
b.Property<int>("nbMaxEchanges") b.Property<int>("nbMaxEchanges")
.HasColumnType("INTEGER"); .HasColumnType("INTEGER");
b.Property<int>("scoreLoser")
.HasColumnType("INTEGER");
b.Property<int>("scoreWinner")
.HasColumnType("INTEGER");
b.Property<int>("winner") b.Property<int>("winner")
.HasColumnType("INTEGER"); .HasColumnType("INTEGER");
@ -82,6 +88,8 @@ namespace DataBase.Migrations
durationGame = 65, durationGame = 65,
loser = 2, loser = 2,
nbMaxEchanges = 5, nbMaxEchanges = 5,
scoreLoser = 2,
scoreWinner = 6,
winner = 1 winner = 1
}); });
}); });

@ -8,7 +8,7 @@ using Microsoft.EntityFrameworkCore.Migrations;
namespace DataBase.Migrations namespace DataBase.Migrations
{ {
/// <inheritdoc /> /// <inheritdoc />
public partial class mymigration : Migration public partial class Migrations : Migration
{ {
/// <inheritdoc /> /// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder) protected override void Up(MigrationBuilder migrationBuilder)
@ -63,7 +63,9 @@ namespace DataBase.Migrations
durationGame = table.Column<int>(type: "INTEGER", nullable: false), durationGame = table.Column<int>(type: "INTEGER", nullable: false),
nbMaxEchanges = table.Column<int>(type: "INTEGER", nullable: false), nbMaxEchanges = table.Column<int>(type: "INTEGER", nullable: false),
winner = table.Column<int>(type: "INTEGER", nullable: false), winner = table.Column<int>(type: "INTEGER", nullable: false),
loser = table.Column<int>(type: "INTEGER", nullable: false) loser = table.Column<int>(type: "INTEGER", nullable: false),
scoreWinner = table.Column<int>(type: "INTEGER", nullable: false),
scoreLoser = table.Column<int>(type: "INTEGER", nullable: false)
}, },
constraints: table => constraints: table =>
{ {
@ -126,8 +128,8 @@ namespace DataBase.Migrations
migrationBuilder.InsertData( migrationBuilder.InsertData(
table: "Games", table: "Games",
columns: new[] { "gameId", "durationGame", "loser", "nbMaxEchanges", "winner" }, columns: new[] { "gameId", "durationGame", "loser", "nbMaxEchanges", "scoreLoser", "scoreWinner", "winner" },
values: new object[] { 1, 65, 2, 5, 1 }); values: new object[] { 1, 65, 2, 5, 2, 6, 1 });
migrationBuilder.InsertData( migrationBuilder.InsertData(
table: "Messages", table: "Messages",

@ -61,6 +61,12 @@ namespace DataBase.Migrations
b.Property<int>("nbMaxEchanges") b.Property<int>("nbMaxEchanges")
.HasColumnType("INTEGER"); .HasColumnType("INTEGER");
b.Property<int>("scoreLoser")
.HasColumnType("INTEGER");
b.Property<int>("scoreWinner")
.HasColumnType("INTEGER");
b.Property<int>("winner") b.Property<int>("winner")
.HasColumnType("INTEGER"); .HasColumnType("INTEGER");
@ -79,6 +85,8 @@ namespace DataBase.Migrations
durationGame = 65, durationGame = 65,
loser = 2, loser = 2,
nbMaxEchanges = 5, nbMaxEchanges = 5,
scoreLoser = 2,
scoreWinner = 6,
winner = 1 winner = 1
}); });
}); });

Binary file not shown.

@ -1,5 +1,6 @@
using ApiLeapHit.Controllers; using ApiLeapHit.Controllers;
using ApiLeapHit.Mapper; using ApiLeapHit.Mapper;
using DataBase.Context;
using DataBase.DataManager; using DataBase.DataManager;
using DataBase.Entity; using DataBase.Entity;
using DTO; using DTO;
@ -20,22 +21,26 @@ namespace TestControleurs
public async Task TestGetPlayer_ValidId() public async Task TestGetPlayer_ValidId()
{ {
// Arrange // Arrange
int id = 1; int id = 8;
DbDataManager dataManager = new DbDataManager(); DbDataManager dataManager = new DbDataManager();
ILoggerFactory loggerFactory = LoggerFactory.Create(builder => builder.AddConsole()); ILoggerFactory loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
var player = new Player { playerId = id, name = "Test Player", nbBallTouchTotal = 0, timePlayed = 3 }; var player = new Player { playerId = id, name = "Test Player", nbBallTouchTotal = 0, timePlayed = 3 };
var controller = new PlayerController(dataManager, loggerFactory.CreateLogger<PlayerController>()); var controller = new PlayerController(dataManager, loggerFactory.CreateLogger<PlayerController>());
await controller.AddPlayer(player.ToDto());
var rep = await controller.AddPlayer(player.ToDto());
// Act // Act
var result = await controller.GetPlayer(id); var result = await controller.GetPlayer(id);
var objectResult = (ObjectResult)result.Result; var objectResult = (ObjectResult)result.Result;
var apiResponse = JsonSerializer.Deserialize<ApiResponse<DTOPlayer>>(objectResult.Value); var apiResponse = (ApiResponse<DTOPlayer>)objectResult.Value;
// Assert // Assert
Assert.IsNotNull(apiResponse); Assert.IsNotNull(objectResult);
Assert.AreEqual((int)HttpStatusCode.OK, objectResult.StatusCode); Assert.AreEqual((int)HttpStatusCode.OK, objectResult.StatusCode);
Assert.AreEqual(apiResponse.Data.playerId, id); Assert.AreEqual(apiResponse.Data.playerId, id);
} }
} }
} }
Loading…
Cancel
Save