🐛 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é."));
}
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.");
return Ok(new ApiResponse<DTOMessage>("Message reçu avec succès.", message.ToDto()));
}

@ -23,7 +23,7 @@ namespace ApiLeapHit.Controllers
}
[HttpGet("{id}")]
public async Task<ActionResult<DTOPlayer>> GetPlayer(int id)
public async Task<ActionResult<ApiResponse<DTOPlayer>>> 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<object>("Joueur ajouté avec succès.");
var response = new ApiResponse<DTOPlayer>("Joueur ajouté avec succès.");
response.Links.Add(new ApiLink(
Url.Action("GetPlayer", "Player", new { id = player.playerId }),
"self",
"GET"
));
var response2 = new ApiResponse<object>("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<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);
// 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);
}

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

@ -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 playerWinner { 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.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<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)

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

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

@ -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
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
@ -64,6 +64,12 @@ namespace DataBase.Migrations
b.Property<int>("nbMaxEchanges")
.HasColumnType("INTEGER");
b.Property<int>("scoreLoser")
.HasColumnType("INTEGER");
b.Property<int>("scoreWinner")
.HasColumnType("INTEGER");
b.Property<int>("winner")
.HasColumnType("INTEGER");
@ -82,6 +88,8 @@ namespace DataBase.Migrations
durationGame = 65,
loser = 2,
nbMaxEchanges = 5,
scoreLoser = 2,
scoreWinner = 6,
winner = 1
});
});

@ -8,7 +8,7 @@ using Microsoft.EntityFrameworkCore.Migrations;
namespace DataBase.Migrations
{
/// <inheritdoc />
public partial class mymigration : Migration
public partial class Migrations : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
@ -63,7 +63,9 @@ namespace DataBase.Migrations
durationGame = table.Column<int>(type: "INTEGER", nullable: false),
nbMaxEchanges = 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 =>
{
@ -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",

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

Binary file not shown.

@ -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<PlayerController>());
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<ApiResponse<DTOPlayer>>(objectResult.Value);
var apiResponse = (ApiResponse<DTOPlayer>)objectResult.Value;
// Assert
Assert.IsNotNull(apiResponse);
Assert.IsNotNull(objectResult);
Assert.AreEqual((int)HttpStatusCode.OK, objectResult.StatusCode);
Assert.AreEqual(apiResponse.Data.playerId, id);
}
}
}
Loading…
Cancel
Save