ajout du contrôleur des lobbies
continuous-integration/drone/push Build is passing Details

API
Damien NORTIER 1 year ago
parent 5b7f683bff
commit b8bf972765

@ -1,8 +1,11 @@
using DbConnectionLibrairie;
using DataManagers;
using DbConnectionLibrairie;
using DTOs;
using EntityManagers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using OrderCriterias;
using ServiceManagers;
namespace WebApi.Controllers
{
@ -24,12 +27,19 @@ namespace WebApi.Controllers
Logger<QuestionController> logQuestion
)
{
var unity = new Unit(dbContext);
var unity = new Unit(
new AdministratorServiceManager(new AdministratorDataManager(new AdministratorEntityManager(dbContext))),
new AnswerServiceManager(new AnswerDataManager(new AnswerEntityManager(dbContext))),
new ChapterServiceManager(new ChapterDataManager(new ChapterEntityManager(dbContext))),
new LobbyServiceManager(new LobbyDataManager(new LobbyEntityManager(dbContext))),
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);
//playerController = new PlayerController(unity, logPlayer);
//questionController = new QuestionController(unity, logQuestion);
}
/// <summary>
@ -253,8 +263,99 @@ namespace WebApi.Controllers
[HttpGet("chapters/name")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task<IActionResult> GetChapter(string name)
public async Task<IActionResult> GetChapter([FromQuery] string name)
=> await chapterController.GetChapter(name);
/// <summary>
/// add a lobby
/// </summary>
/// <param name="lobby">the lobby to add</param>
/// <returns>
/// status code :
/// 200 if the lobby is added
/// 202 if the lobby is added
/// 208 if the lobby was already in the database
/// 500 if there was an internal error that doesn't allow to continue
/// return content :
/// no content when status code = 200
/// lobby added when status code = 202
/// lobby already in the database that equals the one we wanted to add when status code = 208
/// no content when status code = 500
/// </returns>
[HttpPost("add/lobby")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status202Accepted)]
[ProducesResponseType(StatusCodes.Status208AlreadyReported)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> PostLobby([FromBody] LobbyDto lobby)
=> await lobbyController.PostLobby(lobby);
/// <summary>
/// get a part of all lobbies
/// </summary>
/// <param name="page">the actual page</param>
/// <param name="count">number of T element in a page</param>
/// <param name="orderCriteria">the order criteria</param>
/// <returns>
/// status code :
/// 200 if we got a set
/// 204 if we got an empty set
/// 400 if we got nothing
///
/// return content :
/// a tuple of the number of page and
/// all lobbies in the database for
/// the page nb if status code = 200
/// the number of page for count element if status code = 204
/// nothing if status code = 400
/// </returns>
[HttpGet("lobbies")]
public async Task<IActionResult> GetSomeLobbies([FromQuery] int page, int count = 10, LobbyOrderCriteria orderCriteria = LobbyOrderCriteria.ById)
=> await lobbyController.GetSomeLobbies(page, count, orderCriteria);
/// <summary>
/// delete a lobby
/// </summary>
/// <param name="id">the id of the lobby to delete</param>
/// <returns>
/// <returns>
/// status code :
/// 200 if the lobby is removed
/// 204 if the lobby is removed
/// 400 if the id is incorrect
/// 500 if there was an internal error that doesn't allow to continue
/// return content :
/// aministrator deleted when status code = 200
/// no content when status code = 204
/// no content when status code = 400
/// no content when status code = 500
/// </returns>
[HttpDelete("delete/lobby")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> DeleteLobby([FromQuery] int id)
=> await lobbyController.DeleteLobby(id);
/// <summary>
/// get a lobby
/// </summary>
/// <param name="id">the id of the lobby</param>
/// <returns>
/// status code :
/// 200 when we got a lobby
/// 204 when we got null
///
/// return content :
/// the lobby that correspond to the id when status code = 200
/// nothing when status code 204
/// </returns>
[HttpGet("lobbies/name")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task<IActionResult> GetLobby(int id)
=> await lobbyController.GetLobby(id);
}
}

@ -1,6 +1,224 @@
namespace WebApi.Controllers
using DTOs;
using Microsoft.AspNetCore.Mvc;
using OrderCriterias;
namespace WebApi.Controllers
{
public class LobbyController
public class LobbyController : ControllerBase
{
private Unit unity;
private readonly Logger<LobbyController> logger;
public LobbyController(Unit unit, Logger<LobbyController> logger)
{
this.unity = unit;
this.logger = logger;
}
/// <summary>
/// add a lobby
/// </summary>
/// <param name="lobby">the lobby to add</param>
/// <returns>
/// status code :
/// 200 if the lobby is added
/// 202 if the lobby is added
/// 208 if the lobby was already in the database
/// 500 if there was an internal error that doesn't allow to continue
/// return content :
/// no content when status code = 200
/// lobby added when status code = 202
/// lobby already in the database that equals the one we wanted to add when status code = 208
/// no content when status code = 500
/// </returns>
public async Task<IActionResult> PostLobby(LobbyDto lobby)
{
int count = unity.getNbLobbies(); // count : number of elements before opperation
var tmp = await unity.addLobby(lobby); // tmp : lobby recieve by the addLobby method
if (unity.getNbLobbies() == count) // <=> not added
{
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()}");
return StatusCode(208, tmp);
}
else
{
// 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" +
$" 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);
}
}
// added
if (tmp.Name == lobby.Name)
{
logger.LogTrace(message: $"lobby added : {tmp.ToString()}");
// the lobby has been added and we recieved him
return StatusCode(202, tmp);
}
else
{
// the lobby may be added but we do not recieved him
try
{
if (unity.getLobby(lobby.Name, lobby.IdCreator) != null)
{
// he is added
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");
if (unity.getLobby(lobby.Name, lobby.IdCreator) == null) // <=> not added
return StatusCode(500);
}
}
catch (Exception)
{
return StatusCode(500);
}
}
logger.LogError(message: "this case should not append");
return StatusCode(500);
}
/// <summary>
/// get a part of all lobbies
/// </summary>
/// <param name="page">the actual page</param>
/// <param name="count">number of T element in a page</param>
/// <param name="orderCriteria">the order criteria</param>
/// <returns>
/// status code :
/// 200 if we got a set
/// 204 if we got an empty set
/// 400 if we got nothing
///
/// return content :
/// a tuple of the number of page and
/// all lobbies in the database for
/// the page nb if status code = 200
/// the number of page for count element if status code = 204
/// nothing if status code = 400
/// </returns>
public async Task<IActionResult> GetSomeLobbies(int page, int count = 10, LobbyOrderCriteria orderCriteria = LobbyOrderCriteria.ById)
{
var tmp = await unity.getLobbies(page, count, orderCriteria);
if (tmp.lobbies == null)
{
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}");
return NoContent();
}
else
{
logger.LogTrace(message: $"get lobbies : page = {page}, count = {count}, order criteria = {orderCriteria switch
{
LobbyOrderCriteria.ById => "byId",
LobbyOrderCriteria.ByName => "byName",
_ => "none"
}}");
return Ok(tmp);
}
}
/// <summary>
/// delete a lobby
/// </summary>
/// <param name="id">the id of the lobby to delete</param>
/// <returns>
/// <returns>
/// status code :
/// 200 if the lobby is removed
/// 204 if the lobby is removed
/// 400 if the id is incorrect
/// 500 if there was an internal error that doesn't allow to continue
/// return content :
/// aministrator deleted when status code = 200
/// no content when status code = 204
/// no content when status code = 400
/// no content when status code = 500
/// </returns>
public async Task<IActionResult> DeleteLobby(int id)
{
if (id < 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
var tmp = await unity.removeLobby(id);
if (tmp == null) // we don't recieve the lobby
{
if (unity.getNbLobbies() == count) // he is not deleted
{
if (unity.getLobby(id) != null)
{
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");
return BadRequest();
}
}
else // he may be deleted
{
if (unity.getLobby(id) == null) // he must be deleted
{
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");
return StatusCode(500);
}
}
}
if (unity.getNbLobbies() == count)
{
// <=> 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");
return StatusCode(500);
}
logger.LogTrace(message: $"lobby removed {tmp.ToString()}");
return Ok(tmp);
}
/// <summary>
/// get a lobby
/// </summary>
/// <param name="id">the id of the lobby</param>
/// <returns>
/// status code :
/// 200 when we got a lobby
/// 204 when we got null
///
/// return content :
/// the lobby that correspond to the id when status code = 200
/// nothing when status code 204
/// </returns>
public async Task<IActionResult> GetLobby(int id)
{
var tmp = await unity.getLobby(id);
if (tmp == null) return NoContent();
return Ok(tmp);
}
}
}

@ -20,14 +20,21 @@ namespace WebApi
public IPlayerManager<PlayerDto> PlayerManager { get; private set; }
public IQuestionManager<QuestionDto> QuestionManager { get; private set; }
public Unit(MyDbContext dbContext)
{
AdministratorManager = new AdministratorServiceManager(dbContext);
AnswerManager = new AnswerServiceManager(dbContext);
ChapterManager = new ChapterServiceManager(dbContext);
LobbyManager = new LobbyServiceManager(dbContext);
PlayerManager = new PlayerServiceManager(dbContext);
QuestionManager = new QuestionServiceManager(dbContext);
public Unit(
IAdministratorManager<AdministratorDto> administratorManager,
IAnswerManager<AnswerDto> answerManager,
IChapterManager<ChapterDto> chapterManager,
ILobbyManager<LobbyDto> lobbyManager,
IPlayerManager<PlayerDto> playerManager,
IQuestionManager<QuestionDto> questionManager
)
{
AdministratorManager = administratorManager;
AnswerManager = answerManager;
ChapterManager = chapterManager;
LobbyManager = lobbyManager;
PlayerManager = playerManager;
QuestionManager = questionManager;
}
public int getNbAdmins()
@ -190,6 +197,11 @@ namespace WebApi
return LobbyManager.getLobby(id);
}
public Task<LobbyDto?> getLobby(string name, int? idCreator)
{
return LobbyManager.getLobby(name, idCreator);
}
public int getNbPlayers()
{
return PlayerManager.getNbPlayers();

Loading…
Cancel
Save