suppression d'un bug
continuous-integration/drone/push Build is passing Details

API
Damien NORTIER 1 year ago
parent fb6eec6c67
commit dc6fea8bf6

@ -7,6 +7,7 @@ using ServiceManagers;
using Microsoft.Extensions.Logging;
using OrderCriterias;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Components;
namespace WebApi.Controllers
{
@ -14,12 +15,12 @@ namespace WebApi.Controllers
{
private Unit unity;
private readonly Logger<AdministratorController> logger;
[Inject]
private Logger<AdministratorController> Logger { get; set; }
public AdministratorController(Unit unit, Logger<AdministratorController> logger)
public AdministratorController(Unit unit)
{
this.unity = unit;
this.logger = logger;
}
/// <summary>
@ -47,7 +48,7 @@ namespace WebApi.Controllers
if(tmp.Username == administrator.Username)
{
// it was already in the database
logger.LogInformation(message: $"want to add an administrator already in the database : {tmp.ToString()}");
Logger.LogInformation(message: $"want to add an administrator already in the database : {tmp.ToString()}");
return StatusCode(208, tmp);
}
else
@ -55,7 +56,7 @@ namespace WebApi.Controllers
// we recieve an administrator already in the database
// that should be equal to the administrator that we
// wanted to add but it isn't
logger.LogCritical(message: "controller's add fail (already in the database) but\nadministrator" +
Logger.LogCritical(message: "controller's add fail (already in the database) but\nadministrator" +
$" recieve isn't the same as the one we wanted to add.\n" +
$"Administrator recieve : {tmp.ToString()}\nadministrator to add : {administrator.ToString()}");
return StatusCode(500);
@ -64,7 +65,7 @@ namespace WebApi.Controllers
// added
if (tmp.Username == administrator.Username)
{
logger.LogTrace(message: $"administrator added : {tmp.ToString()}");
Logger.LogTrace(message: $"administrator added : {tmp.ToString()}");
// the administrator has been added and we recieved him
return StatusCode(202, tmp);
}
@ -74,18 +75,18 @@ namespace WebApi.Controllers
if (unity.getAdministratorByUsername(administrator.Username) != null)
{
// he is added
logger.LogError(message: $"administrator added but not recieved");
Logger.LogError(message: $"administrator added but not recieved");
return Ok(); // not 202 to make a difference between 2 cases
}
else
{
// he is not added
logger.LogCritical(message: "administrator that we wanted to add not added\nand we have added another one");
Logger.LogCritical(message: "administrator that we wanted to add not added\nand we have added another one");
if (unity.getAdministratorByUsername(administrator.Username) == null) // <=> not added
return StatusCode(500);
}
}
logger.LogError(message: "this case should not append");
Logger.LogError(message: "this case should not append");
return StatusCode(500);
}
@ -113,17 +114,17 @@ namespace WebApi.Controllers
var tmp = await unity.getAdministrators(page, count, orderCriteria);
if (tmp.administrators == null)
{
logger.LogInformation(message: "get admin : bad request (page or/and count incorrect)");
Logger.LogInformation(message: "get admin : bad request (page or/and count incorrect)");
return BadRequest(tmp.nbPages);
}
else if (tmp.administrators.Count() == 0)
{
logger.LogWarning(message: $"get admin : no content. number of element : {unity.getNbAdmins()}, page wanted : {page}, number of elements in a page : {count}");
Logger.LogWarning(message: $"get admin : no content. number of element : {unity.getNbAdmins()}, page wanted : {page}, number of elements in a page : {count}");
return NoContent();
}
else
{
logger.LogTrace(message: $"get admins : page = {page}, count = {count}, order criteria = {orderCriteria switch
Logger.LogTrace(message: $"get admins : page = {page}, count = {count}, order criteria = {orderCriteria switch
{
AdministratorOrderCriteria.ById => "byId",
AdministratorOrderCriteria.ByUserName => "byUsername",
@ -154,7 +155,7 @@ namespace WebApi.Controllers
{
if (id < 0)
{
logger.LogError("want to delete an administrator with an id less than 0");
Logger.LogError("want to delete an administrator with an id less than 0");
return BadRequest();
}
int count = unity.getNbAdmins(); // count : number of elements before opperation
@ -165,12 +166,12 @@ namespace WebApi.Controllers
{
if (unity.getAdministrator(id) != null)
{
logger.LogCritical(message: "remove administrator fail : administrator not removed and not recieved");
Logger.LogCritical(message: "remove administrator fail : administrator not removed and not recieved");
return StatusCode(500);
}
else
{
logger.LogInformation(message: "trying to remove an administrator with an id who don't exist");
Logger.LogInformation(message: "trying to remove an administrator with an id who don't exist");
return BadRequest();
}
}
@ -178,12 +179,12 @@ namespace WebApi.Controllers
{
if (unity.getAdministrator(id) == null) // he must be deleted
{
logger.LogError(message: "administrator removed but not returned");
Logger.LogError(message: "administrator removed but not returned");
return NoContent();
}
else // he is not deleted
{
logger.LogCritical(message: "remove administrator fail : administrator to remove not remove\ninstead, anotherone is deleted and we recieved nothing");
Logger.LogCritical(message: "remove administrator fail : administrator to remove not remove\ninstead, anotherone is deleted and we recieved nothing");
return StatusCode(500);
}
}
@ -193,10 +194,10 @@ namespace WebApi.Controllers
// <=> we have recieved an administrator which should be deleted
// but since we have the same number of administrator than
// before deletion, it isn't deleted
logger.LogCritical(message: $"administrator \"{tmp.ToString()}\"should be delete but it isn't");
Logger.LogCritical(message: $"administrator \"{tmp.ToString()}\"should be delete but it isn't");
return StatusCode(500);
}
logger.LogTrace(message: $"administrator removed {tmp.ToString()}");
Logger.LogTrace(message: $"administrator removed {tmp.ToString()}");
return Ok(tmp);
}
@ -223,7 +224,7 @@ namespace WebApi.Controllers
var tmp = await unity.getAdministratorByUsername(administrator.Username);
if (tmp != null)
{
logger.LogTrace(message: "Want to modify into an administrator who already exist");
Logger.LogTrace(message: "Want to modify into an administrator who already exist");
return StatusCode(StatusCodes.Status208AlreadyReported, tmp);
}
tmp = await unity.updateAdministrator(id, administrator);
@ -232,19 +233,19 @@ namespace WebApi.Controllers
tmp = await unity.getAdministrator(id);
if (tmp == null) // Ok, it don't exist
{
logger.LogWarning(message: "Want to modify an administrator who don't exist");
Logger.LogWarning(message: "Want to modify an administrator who don't exist");
return NoContent();
}
else
{
if (tmp.Username == administrator.Username && tmp.HashedPassword == administrator.HashedPassword)
{
logger.LogError(message: "Administrator changed but not recieved");
Logger.LogError(message: "Administrator changed but not recieved");
return Ok(tmp);
}
else
{
logger.LogCritical(message: "administrator haven't changed and we recieved nothing");
Logger.LogCritical(message: "administrator haven't changed and we recieved nothing");
return StatusCode(500);
}
}
@ -254,16 +255,16 @@ namespace WebApi.Controllers
if(tmp.HashedPassword == administrator.HashedPassword
&& tmp.Username == administrator.Username) // he is changed
{
logger.LogTrace(message: $"administrator with id {id} modified in {administrator.ToString()}");
Logger.LogTrace(message: $"administrator with id {id} modified in {administrator.ToString()}");
return Ok(tmp);
}
else // he haven't changed
{
var tmp2 = (await unity.getAdministratorByUsername(administrator.Username));
if(tmp2 != null) // it change another administrator
logger.LogCritical(message: "administrator should have changed but he haven't. Instead, another one changed");
Logger.LogCritical(message: "administrator should have changed but he haven't. Instead, another one changed");
else // nothing have changed
logger.LogCritical(message: "administrator should have changed but he haven't and we recieved him");
Logger.LogCritical(message: "administrator should have changed but he haven't and we recieved him");
return StatusCode(500);
}
}
@ -272,17 +273,17 @@ namespace WebApi.Controllers
var tmp2 = await unity.getAdministrator(id);
if (tmp2 == null)
{ // ok, he d'ont exist.
logger.LogError(message: "Want to modify an administrator who don't exist but recieved a random one");
Logger.LogError(message: "Want to modify an administrator who don't exist but recieved a random one");
return NoContent();
}
else if (tmp2.Username == administrator.Username && tmp2.HashedPassword == administrator.HashedPassword)
{
logger.LogError(message: $"administrator modified but recieved a random one");
Logger.LogError(message: $"administrator modified but recieved a random one");
return Ok(tmp2);
}
else
{
logger.LogCritical(message: "administrator that we wanted to modify not modified");
Logger.LogCritical(message: "administrator that we wanted to modify not modified");
return StatusCode(500);
}
}

@ -1,4 +1,5 @@
using DTOs;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Mvc;
using OrderCriterias;
@ -8,12 +9,13 @@ namespace WebApi.Controllers
{
private Unit unity;
private readonly Logger<ChapterController> logger;
public ChapterController(Unit unit, Logger<ChapterController> logger)
[Inject]
private Logger<ChapterController> Logger { get; set; }
public ChapterController(Unit unit)
{
this.unity = unit;
this.logger = logger;
}
/// <summary>
@ -41,7 +43,7 @@ namespace WebApi.Controllers
if (tmp.Name == chapter.Name)
{
// it was already in the database
logger.LogInformation(message: $"want to add a chapter already in the database : {tmp.ToString()}");
Logger.LogInformation(message: $"want to add a chapter already in the database : {tmp.ToString()}");
return StatusCode(208, tmp);
}
else
@ -49,7 +51,7 @@ namespace WebApi.Controllers
// we recieve a chapter already in the database
// that should be equal to the chapter that we
// wanted to add but it isn't
logger.LogCritical(message: "controller's add fail (already in the database) but\nchapter" +
Logger.LogCritical(message: "controller's add fail (already in the database) but\nchapter" +
$" recieve isn't the same as the one we wanted to add.\n" +
$"Chapter recieve : {tmp.ToString()}\nchapter to add : {chapter.ToString()}");
return StatusCode(500);
@ -58,7 +60,7 @@ namespace WebApi.Controllers
// added
if (tmp.Name == chapter.Name)
{
logger.LogTrace(message: $"chapter added : {tmp.ToString()}");
Logger.LogTrace(message: $"chapter added : {tmp.ToString()}");
// the chapter has been added and we recieved him
return StatusCode(202, tmp);
}
@ -68,18 +70,18 @@ namespace WebApi.Controllers
if (unity.getChapter(chapter.Name) != null)
{
// he is added
logger.LogError(message: $"chapter added but not recieved");
Logger.LogError(message: $"chapter added but not recieved");
return Ok(); // not 202 to make a difference between 2 cases
}
else
{
// he is not added
logger.LogCritical(message: "chapter that we wanted to add not added\nand we have added another one");
Logger.LogCritical(message: "chapter that we wanted to add not added\nand we have added another one");
if (unity.getChapter(chapter.Name) == null) // <=> not added
return StatusCode(500);
}
}
logger.LogError(message: "this case should not append");
Logger.LogError(message: "this case should not append");
return StatusCode(500);
}
@ -107,17 +109,17 @@ namespace WebApi.Controllers
var tmp = await unity.getChapters(page, count, orderCriteria);
if (tmp.chapters == null)
{
logger.LogInformation(message: "get chapter : bad request (page or/and count incorrect)");
Logger.LogInformation(message: "get chapter : bad request (page or/and count incorrect)");
return BadRequest(tmp.nbPages);
}
else if (tmp.chapters.Count() == 0)
{
logger.LogWarning(message: $"get chapter : no content. number of element : {unity.getNbChapters()}, page wanted : {page}, number of elements in a page : {count}");
Logger.LogWarning(message: $"get chapter : no content. number of element : {unity.getNbChapters()}, page wanted : {page}, number of elements in a page : {count}");
return NoContent();
}
else
{
logger.LogTrace(message: $"get chapters : page = {page}, count = {count}, order criteria = {orderCriteria switch
Logger.LogTrace(message: $"get chapters : page = {page}, count = {count}, order criteria = {orderCriteria switch
{
ChapterOrderCriteria.ById => "byId",
ChapterOrderCriteria.ByName => "byName",
@ -148,7 +150,7 @@ namespace WebApi.Controllers
{
if (id < 0)
{
logger.LogError("want to delete a chapter with an id less than 0");
Logger.LogError("want to delete a chapter with an id less than 0");
return BadRequest();
}
int count = unity.getNbChapters(); // count : number of elements before opperation
@ -159,12 +161,12 @@ namespace WebApi.Controllers
{
if (unity.getChapter(id) != null)
{
logger.LogCritical(message: "remove chapter fail : chapter not removed and not recieved");
Logger.LogCritical(message: "remove chapter fail : chapter not removed and not recieved");
return StatusCode(500);
}
else
{
logger.LogInformation(message: "trying to remove a chapter with an id who don't exist");
Logger.LogInformation(message: "trying to remove a chapter with an id who don't exist");
return BadRequest();
}
}
@ -172,12 +174,12 @@ namespace WebApi.Controllers
{
if (unity.getChapter(id) == null) // he must be deleted
{
logger.LogError(message: "chapter removed but not returned");
Logger.LogError(message: "chapter removed but not returned");
return NoContent();
}
else // he is not deleted
{
logger.LogCritical(message: "remove chapter fail : chapter to remove not remove\ninstead, anotherone is deleted and we recieved nothing");
Logger.LogCritical(message: "remove chapter fail : chapter to remove not remove\ninstead, anotherone is deleted and we recieved nothing");
return StatusCode(500);
}
}
@ -187,10 +189,10 @@ namespace WebApi.Controllers
// <=> we have recieved a chapter which should be deleted
// but since we have the same number of chapter than
// before deletion, it isn't deleted
logger.LogCritical(message: $"chapter \"{tmp.ToString()}\"should be delete but it isn't");
Logger.LogCritical(message: $"chapter \"{tmp.ToString()}\"should be delete but it isn't");
return StatusCode(500);
}
logger.LogTrace(message: $"chapter removed {tmp.ToString()}");
Logger.LogTrace(message: $"chapter removed {tmp.ToString()}");
return Ok(tmp);
}
@ -217,7 +219,7 @@ namespace WebApi.Controllers
var tmp = await unity.getChapter(chapter.Name);
if (tmp != null)
{
logger.LogTrace(message: "Want to modify into a chapter who already exist");
Logger.LogTrace(message: "Want to modify into a chapter who already exist");
return StatusCode(StatusCodes.Status208AlreadyReported, tmp);
}
tmp = await unity.updateChapter(id, chapter.Name);
@ -226,19 +228,19 @@ namespace WebApi.Controllers
tmp = await unity.getChapter(id);
if (tmp == null) // Ok, it don't exist
{
logger.LogWarning(message: "Want to modify a chapter who don't exist");
Logger.LogWarning(message: "Want to modify a chapter who don't exist");
return NoContent();
}
else
{
if (tmp.Name == chapter.Name)
{
logger.LogError(message: "Chapter changed but not recieved");
Logger.LogError(message: "Chapter changed but not recieved");
return Ok(tmp);
}
else
{
logger.LogCritical(message: "chapter haven't changed and we recieved nothing");
Logger.LogCritical(message: "chapter haven't changed and we recieved nothing");
return StatusCode(500);
}
}
@ -247,16 +249,16 @@ namespace WebApi.Controllers
{
if (tmp.Name == chapter.Name) // he is changed
{
logger.LogTrace(message: $"chapter with id {id} modified in {chapter.ToString()}");
Logger.LogTrace(message: $"chapter with id {id} modified in {chapter.ToString()}");
return Ok(tmp);
}
else // he haven't changed
{
var tmp2 = (await unity.getChapter(chapter.Name));
if (tmp2 != null) // it change another chapter
logger.LogCritical(message: "chapter should have changed but he haven't. Instead, another one changed");
Logger.LogCritical(message: "chapter should have changed but he haven't. Instead, another one changed");
else // nothing have changed
logger.LogCritical(message: "chapter should have changed but he haven't and we recieved him");
Logger.LogCritical(message: "chapter should have changed but he haven't and we recieved him");
return StatusCode(500);
}
}
@ -265,17 +267,17 @@ namespace WebApi.Controllers
var tmp2 = await unity.getChapter(id);
if (tmp2 == null)
{ // ok, he d'ont exist.
logger.LogError(message: "Want to modify a chapter who don't exist but recieved a random one");
Logger.LogError(message: "Want to modify a chapter who don't exist but recieved a random one");
return NoContent();
}
else if (tmp2.Name == chapter.Name)
{
logger.LogError(message: $"chapter modified but recieved a random one");
Logger.LogError(message: $"chapter modified but recieved a random one");
return Ok(tmp2);
}
else
{
logger.LogCritical(message: "chapter that we wanted to modify not modified");
Logger.LogCritical(message: "chapter that we wanted to modify not modified");
return StatusCode(500);
}
}

@ -11,12 +11,16 @@ using RouteAttribute = Microsoft.AspNetCore.Mvc.RouteAttribute;
namespace WebApi.Controllers
{
/// <summary>
/// a controller just to separate route definition with route "documentation"
/// </summary>
[ApiVersion("1.0")]
[Route("api/v{version:apiversion}")]
[ApiController]
public class FrontController
{
[Inject]
public MyDbContext dbContext { get; set; }
private Unit unity;
// all secondary controllers
private AdministratorController administratorController;
@ -26,14 +30,10 @@ namespace WebApi.Controllers
private QuestionController questionController;
public FrontController(
Logger<AdministratorController> logAdmin,
Logger<ChapterController> logChapter,
Logger<LobbyController> logLobby,
Logger<PlayerController> logPlayer,
Logger<QuestionController> logQuestion
)
MyDbContext dbContext
)
{
var unity = new Unit(
unity = new Unit(
new AdministratorServiceManager(new AdministratorDataManager(new AdministratorEntityManager(dbContext))),
new AnswerServiceManager(new AnswerDataManager(new AnswerEntityManager(dbContext))),
new ChapterServiceManager(new ChapterDataManager(new ChapterEntityManager(dbContext))),
@ -41,11 +41,11 @@ namespace WebApi.Controllers
new PlayerServiceManager(new PlayerDataManager(new PlayerEntityManager(dbContext))),
new QuestionServiceManager(new QuestionDataManager(new QuestionEntityManager(dbContext)))
);
administratorController = new AdministratorController(unity, logAdmin);
chapterController = new ChapterController(unity, logChapter);
lobbyController = new LobbyController(unity, logLobby);
playerController = new PlayerController(unity, logPlayer);
questionController = new QuestionController(unity, logQuestion);
administratorController = new AdministratorController(unity);
chapterController = new ChapterController(unity);
lobbyController = new LobbyController(unity);
playerController = new PlayerController(unity);
questionController = new QuestionController(unity);
}
/// <summary>

@ -1,4 +1,5 @@
using DTOs;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Mvc;
using OrderCriterias;
@ -8,12 +9,12 @@ namespace WebApi.Controllers
{
private Unit unity;
private readonly Logger<LobbyController> logger;
[Inject]
private Logger<LobbyController> Logger { get; set; }
public LobbyController(Unit unit, Logger<LobbyController> logger)
public LobbyController(Unit unit)
{
this.unity = unit;
this.logger = logger;
}
/// <summary>
@ -41,7 +42,7 @@ namespace WebApi.Controllers
if (tmp.Name == lobby.Name)
{
// it was already in the database
logger.LogInformation(message: $"want to add a lobby already in the database : {tmp.ToString()}");
Logger.LogInformation(message: $"want to add a lobby already in the database : {tmp.ToString()}");
return StatusCode(208, tmp);
}
else
@ -49,7 +50,7 @@ namespace WebApi.Controllers
// we recieve a lobby already in the database
// that should be equal to the lobby that we
// wanted to add but it isn't
logger.LogCritical(message: "controller's add fail (already in the database) but\nlobby" +
Logger.LogCritical(message: "controller's add fail (already in the database) but\nlobby" +
$" recieve isn't the same as the one we wanted to add.\n" +
$"Lobby recieve : {tmp.ToString()}\nlobby to add : {lobby.ToString()}");
return StatusCode(500);
@ -58,7 +59,7 @@ namespace WebApi.Controllers
// added
if (tmp.Name == lobby.Name)
{
logger.LogTrace(message: $"lobby added : {tmp.ToString()}");
Logger.LogTrace(message: $"lobby added : {tmp.ToString()}");
// the lobby has been added and we recieved him
return StatusCode(202, tmp);
}
@ -70,13 +71,13 @@ namespace WebApi.Controllers
if (unity.getLobby(lobby.Name, lobby.IdCreator) != null)
{
// he is added
logger.LogError(message: $"lobby added but not recieved");
Logger.LogError(message: $"lobby added but not recieved");
return Ok(); // not 202 to make a difference between 2 cases
}
else
{
// he is not added
logger.LogCritical(message: "lobby that we wanted to add not added\nand we have added another one");
Logger.LogCritical(message: "lobby that we wanted to add not added\nand we have added another one");
if (unity.getLobby(lobby.Name, lobby.IdCreator) == null) // <=> not added
return StatusCode(500);
}
@ -86,7 +87,7 @@ namespace WebApi.Controllers
return StatusCode(500);
}
}
logger.LogError(message: "this case should not append");
Logger.LogError(message: "this case should not append");
return StatusCode(500);
}
@ -114,17 +115,17 @@ namespace WebApi.Controllers
var tmp = await unity.getLobbies(page, count, orderCriteria);
if (tmp.lobbies == null)
{
logger.LogInformation(message: "get lobby : bad request (page or/and count incorrect)");
Logger.LogInformation(message: "get lobby : bad request (page or/and count incorrect)");
return BadRequest(tmp.nbPages);
}
else if (tmp.lobbies.Count() == 0)
{
logger.LogWarning(message: $"get lobby : no content. number of element : {unity.getNbLobbies()}, page wanted : {page}, number of elements in a page : {count}");
Logger.LogWarning(message: $"get lobby : no content. number of element : {unity.getNbLobbies()}, page wanted : {page}, number of elements in a page : {count}");
return NoContent();
}
else
{
logger.LogTrace(message: $"get lobbies : page = {page}, count = {count}, order criteria = {orderCriteria switch
Logger.LogTrace(message: $"get lobbies : page = {page}, count = {count}, order criteria = {orderCriteria switch
{
LobbyOrderCriteria.ById => "byId",
LobbyOrderCriteria.ByName => "byName",
@ -155,7 +156,7 @@ namespace WebApi.Controllers
{
if (id < 0)
{
logger.LogError("want to delete a lobby with an id less than 0");
Logger.LogError("want to delete a lobby with an id less than 0");
return BadRequest();
}
int count = unity.getNbLobbies(); // count : number of elements before opperation
@ -166,12 +167,12 @@ namespace WebApi.Controllers
{
if (unity.getLobby(id) != null)
{
logger.LogCritical(message: "remove lobby fail : lobby not removed and not recieved");
Logger.LogCritical(message: "remove lobby fail : lobby not removed and not recieved");
return StatusCode(500);
}
else
{
logger.LogInformation(message: "trying to remove a lobby with an id who don't exist");
Logger.LogInformation(message: "trying to remove a lobby with an id who don't exist");
return BadRequest();
}
}
@ -179,12 +180,12 @@ namespace WebApi.Controllers
{
if (unity.getLobby(id) == null) // he must be deleted
{
logger.LogError(message: "lobby removed but not returned");
Logger.LogError(message: "lobby removed but not returned");
return NoContent();
}
else // he is not deleted
{
logger.LogCritical(message: "remove lobby fail : lobby to remove not remove\ninstead, anotherone is deleted and we recieved nothing");
Logger.LogCritical(message: "remove lobby fail : lobby to remove not remove\ninstead, anotherone is deleted and we recieved nothing");
return StatusCode(500);
}
}
@ -194,10 +195,10 @@ namespace WebApi.Controllers
// <=> we have recieved a lobby which should be deleted
// but since we have the same number of lobby than
// before deletion, it isn't deleted
logger.LogCritical(message: $"lobby \"{tmp.ToString()}\"should be delete but it isn't");
Logger.LogCritical(message: $"lobby \"{tmp.ToString()}\"should be delete but it isn't");
return StatusCode(500);
}
logger.LogTrace(message: $"lobby removed {tmp.ToString()}");
Logger.LogTrace(message: $"lobby removed {tmp.ToString()}");
return Ok(tmp);
}

@ -1,4 +1,5 @@
using DTOs;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Mvc;
using OrderCriterias;
@ -8,12 +9,12 @@ namespace WebApi.Controllers
{
private Unit unity;
private readonly Logger<PlayerController> logger;
[Inject]
private Logger<PlayerController> Logger { get; set; }
public PlayerController(Unit unit, Logger<PlayerController> logger)
public PlayerController(Unit unit)
{
this.unity = unit;
this.logger = logger;
}
/// <summary>
@ -41,7 +42,7 @@ namespace WebApi.Controllers
if (tmp.Nickname == player.Nickname)
{
// it was already in the database
logger.LogInformation(message: $"want to add a player already in the database : {tmp.ToString()}");
Logger.LogInformation(message: $"want to add a player already in the database : {tmp.ToString()}");
return StatusCode(208, tmp);
}
else
@ -49,7 +50,7 @@ namespace WebApi.Controllers
// we recieve a player already in the database
// that should be equal to the player that we
// wanted to add but it isn't
logger.LogCritical(message: "controller's add fail (already in the database) but\nplayer" +
Logger.LogCritical(message: "controller's add fail (already in the database) but\nplayer" +
$" recieve isn't the same as the one we wanted to add.\n" +
$"Player recieve : {tmp.ToString()}\nplayer to add : {player.ToString()}");
return StatusCode(500);
@ -58,7 +59,7 @@ namespace WebApi.Controllers
// added
if (tmp.Nickname == player.Nickname)
{
logger.LogTrace(message: $"player added : {tmp.ToString()}");
Logger.LogTrace(message: $"player added : {tmp.ToString()}");
// the player has been added and we recieved him
return StatusCode(202, tmp);
}
@ -70,13 +71,13 @@ namespace WebApi.Controllers
if (unity.getPlayer(player.Nickname) != null)
{
// he is added
logger.LogError(message: $"player added but not recieved");
Logger.LogError(message: $"player added but not recieved");
return Ok(); // not 202 to make a difference between 2 cases
}
else
{
// he is not added
logger.LogCritical(message: "player that we wanted to add not added\nand we have added another one");
Logger.LogCritical(message: "player that we wanted to add not added\nand we have added another one");
if (unity.getPlayer(player.Nickname) == null) // <=> not added
return StatusCode(500);
}
@ -86,7 +87,7 @@ namespace WebApi.Controllers
return StatusCode(500);
}
}
logger.LogError(message: "this case should not append");
Logger.LogError(message: "this case should not append");
return StatusCode(500);
}
@ -114,17 +115,17 @@ namespace WebApi.Controllers
var tmp = await unity.getPlayers(page, count, orderCriteria);
if (tmp.players == null)
{
logger.LogInformation(message: "get player : bad request (page or/and count incorrect)");
Logger.LogInformation(message: "get player : bad request (page or/and count incorrect)");
return BadRequest(tmp.nbPages);
}
else if (tmp.players.Count() == 0)
{
logger.LogWarning(message: $"get player : no content. number of element : {unity.getNbPlayers()}, page wanted : {page}, number of elements in a page : {count}");
Logger.LogWarning(message: $"get player : no content. number of element : {unity.getNbPlayers()}, page wanted : {page}, number of elements in a page : {count}");
return NoContent();
}
else
{
logger.LogTrace(message: $"get players : page = {page}, count = {count}, order criteria = {orderCriteria switch
Logger.LogTrace(message: $"get players : page = {page}, count = {count}, order criteria = {orderCriteria switch
{
PlayerOrderCriteria.ById => "byId",
PlayerOrderCriteria.ByNickname => "byNickname",
@ -155,7 +156,7 @@ namespace WebApi.Controllers
{
if (id < 0)
{
logger.LogError("want to delete a player with an id less than 0");
Logger.LogError("want to delete a player with an id less than 0");
return BadRequest();
}
int count = unity.getNbPlayers(); // count : number of elements before opperation
@ -166,12 +167,12 @@ namespace WebApi.Controllers
{
if (unity.getPlayer(id) != null)
{
logger.LogCritical(message: "remove player fail : player not removed and not recieved");
Logger.LogCritical(message: "remove player fail : player not removed and not recieved");
return StatusCode(500);
}
else
{
logger.LogInformation(message: "trying to remove a player with an id who don't exist");
Logger.LogInformation(message: "trying to remove a player with an id who don't exist");
return BadRequest();
}
}
@ -179,12 +180,12 @@ namespace WebApi.Controllers
{
if (unity.getPlayer(id) == null) // he must be deleted
{
logger.LogError(message: "player removed but not returned");
Logger.LogError(message: "player removed but not returned");
return NoContent();
}
else // he is not deleted
{
logger.LogCritical(message: "remove player fail : player to remove not remove\ninstead, anotherone is deleted and we recieved nothing");
Logger.LogCritical(message: "remove player fail : player to remove not remove\ninstead, anotherone is deleted and we recieved nothing");
return StatusCode(500);
}
}
@ -194,10 +195,10 @@ namespace WebApi.Controllers
// <=> we have recieved a player which should be deleted
// but since we have the same number of player than
// before deletion, it isn't deleted
logger.LogCritical(message: $"player \"{tmp.ToString()}\"should be delete but it isn't");
Logger.LogCritical(message: $"player \"{tmp.ToString()}\"should be delete but it isn't");
return StatusCode(500);
}
logger.LogTrace(message: $"player removed {tmp.ToString()}");
Logger.LogTrace(message: $"player removed {tmp.ToString()}");
return Ok(tmp);
}

@ -1,4 +1,5 @@
using DTOs;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Mvc;
using OrderCriterias;
@ -8,12 +9,12 @@ namespace WebApi.Controllers
{
private Unit unity;
private readonly Logger<QuestionController> logger;
[Inject]
private Logger<QuestionController> Logger { get; set; }
public QuestionController(Unit unit, Logger<QuestionController> logger)
public QuestionController(Unit unit)
{
this.unity = unit;
this.logger = logger;
}
/// <summary>
@ -41,7 +42,7 @@ namespace WebApi.Controllers
if (tmp.Content == question.Content)
{
// it was already in the database
logger.LogInformation(message: $"want to add a question already in the database : {tmp.ToString()}");
Logger.LogInformation(message: $"want to add a question already in the database : {tmp.ToString()}");
return StatusCode(208, tmp);
}
else
@ -49,7 +50,7 @@ namespace WebApi.Controllers
// we recieve a question already in the database
// that should be equal to the question that we
// wanted to add but it isn't
logger.LogCritical(message: "controller's add fail (already in the database) but\nquestion" +
Logger.LogCritical(message: "controller's add fail (already in the database) but\nquestion" +
$" recieve isn't the same as the one we wanted to add.\n" +
$"Question recieve : {tmp.ToString()}\nquestion to add : {question.ToString()}");
return StatusCode(500);
@ -58,7 +59,7 @@ namespace WebApi.Controllers
// added
if (tmp.Content == question.Content)
{
logger.LogTrace(message: $"question added : {tmp.ToString()}");
Logger.LogTrace(message: $"question added : {tmp.ToString()}");
// the question has been added and we recieved him
return StatusCode(202, tmp);
}
@ -70,13 +71,13 @@ namespace WebApi.Controllers
if (unity.getQuestion(question.Content) != null)
{
// he is added
logger.LogError(message: $"question added but not recieved");
Logger.LogError(message: $"question added but not recieved");
return Ok(); // not 202 to make a difference between 2 cases
}
else
{
// he is not added
logger.LogCritical(message: "question that we wanted to add not added\nand we have added another one");
Logger.LogCritical(message: "question that we wanted to add not added\nand we have added another one");
if (unity.getQuestion(question.Content) == null) // <=> not added
return StatusCode(500);
}
@ -86,7 +87,7 @@ namespace WebApi.Controllers
return StatusCode(500);
}
}
logger.LogError(message: "this case should not append");
Logger.LogError(message: "this case should not append");
return StatusCode(500);
}
@ -114,17 +115,17 @@ namespace WebApi.Controllers
var tmp = await unity.getQuestions(page, count, orderCriteria);
if (tmp.questions == null)
{
logger.LogInformation(message: "get question : bad request (page or/and count incorrect)");
Logger.LogInformation(message: "get question : bad request (page or/and count incorrect)");
return BadRequest(tmp.nbPages);
}
else if (tmp.questions.Count() == 0)
{
logger.LogWarning(message: $"get question : no content. number of element : {unity.getNbQuestions()}, page wanted : {page}, number of elements in a page : {count}");
Logger.LogWarning(message: $"get question : no content. number of element : {unity.getNbQuestions()}, page wanted : {page}, number of elements in a page : {count}");
return NoContent();
}
else
{
logger.LogTrace(message: $"get questions : page = {page}, count = {count}, order criteria = {orderCriteria switch
Logger.LogTrace(message: $"get questions : page = {page}, count = {count}, order criteria = {orderCriteria switch
{
QuestionOrderCriteria.ById => "byId",
QuestionOrderCriteria.ByContent => "byContent",
@ -157,7 +158,7 @@ namespace WebApi.Controllers
{
if (id < 0)
{
logger.LogError("want to delete a question with an id less than 0");
Logger.LogError("want to delete a question with an id less than 0");
return BadRequest();
}
int count = unity.getNbQuestions(); // count : number of elements before opperation
@ -168,12 +169,12 @@ namespace WebApi.Controllers
{
if (unity.getQuestion(id) != null)
{
logger.LogCritical(message: "remove question fail : question not removed and not recieved");
Logger.LogCritical(message: "remove question fail : question not removed and not recieved");
return StatusCode(500);
}
else
{
logger.LogInformation(message: "trying to remove a question with an id who don't exist");
Logger.LogInformation(message: "trying to remove a question with an id who don't exist");
return BadRequest();
}
}
@ -181,12 +182,12 @@ namespace WebApi.Controllers
{
if (unity.getQuestion(id) == null) // he must be deleted
{
logger.LogError(message: "question removed but not returned");
Logger.LogError(message: "question removed but not returned");
return NoContent();
}
else // he is not deleted
{
logger.LogCritical(message: "remove question fail : question to remove not remove\ninstead, anotherone is deleted and we recieved nothing");
Logger.LogCritical(message: "remove question fail : question to remove not remove\ninstead, anotherone is deleted and we recieved nothing");
return StatusCode(500);
}
}
@ -196,10 +197,10 @@ namespace WebApi.Controllers
// <=> we have recieved a question which should be deleted
// but since we have the same number of question than
// before deletion, it isn't deleted
logger.LogCritical(message: $"question \"{tmp.ToString()}\"should be delete but it isn't");
Logger.LogCritical(message: $"question \"{tmp.ToString()}\"should be delete but it isn't");
return StatusCode(500);
}
logger.LogTrace(message: $"question removed {tmp.ToString()}");
Logger.LogTrace(message: $"question removed {tmp.ToString()}");
return Ok(tmp);
}

@ -1,4 +1,7 @@
using DbConnectionLibrairie;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using WebApi.Controllers;
var builder = WebApplication.CreateBuilder(args);

@ -8,6 +8,10 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

Binary file not shown.
Loading…
Cancel
Save