diff --git a/WebApi/WebApi/Controllers/FrontController.cs b/WebApi/WebApi/Controllers/FrontController.cs index 3d0a7f6..f264a5d 100644 --- a/WebApi/WebApi/Controllers/FrontController.cs +++ b/WebApi/WebApi/Controllers/FrontController.cs @@ -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 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); } /// @@ -253,8 +263,99 @@ namespace WebApi.Controllers [HttpGet("chapters/name")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status204NoContent)] - public async Task GetChapter(string name) + public async Task GetChapter([FromQuery] string name) => await chapterController.GetChapter(name); + /// + /// add a lobby + /// + /// the lobby to add + /// + /// 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 + /// + [HttpPost("add/lobby")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status202Accepted)] + [ProducesResponseType(StatusCodes.Status208AlreadyReported)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public async Task PostLobby([FromBody] LobbyDto lobby) + => await lobbyController.PostLobby(lobby); + + /// + /// get a part of all lobbies + /// + /// the actual page + /// number of T element in a page + /// the order criteria + /// + /// 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 + /// + [HttpGet("lobbies")] + public async Task GetSomeLobbies([FromQuery] int page, int count = 10, LobbyOrderCriteria orderCriteria = LobbyOrderCriteria.ById) + => await lobbyController.GetSomeLobbies(page, count, orderCriteria); + + /// + /// delete a lobby + /// + /// the id of the lobby to delete + /// + /// + /// 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 + /// + [HttpDelete("delete/lobby")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status204NoContent)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public async Task DeleteLobby([FromQuery] int id) + => await lobbyController.DeleteLobby(id); + + /// + /// get a lobby + /// + /// the id of the lobby + /// + /// 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 + /// + [HttpGet("lobbies/name")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status204NoContent)] + public async Task GetLobby(int id) + => await lobbyController.GetLobby(id); + } } diff --git a/WebApi/WebApi/Controllers/LobbyController.cs b/WebApi/WebApi/Controllers/LobbyController.cs index 5058257..a3bb4d8 100644 --- a/WebApi/WebApi/Controllers/LobbyController.cs +++ b/WebApi/WebApi/Controllers/LobbyController.cs @@ -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 logger; + + public LobbyController(Unit unit, Logger logger) + { + this.unity = unit; + this.logger = logger; + } + + /// + /// add a lobby + /// + /// the lobby to add + /// + /// 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 + /// + public async Task 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); + } + + /// + /// get a part of all lobbies + /// + /// the actual page + /// number of T element in a page + /// the order criteria + /// + /// 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 + /// + public async Task 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); + } + } + + /// + /// delete a lobby + /// + /// the id of the lobby to delete + /// + /// + /// 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 + /// + public async Task 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); + } + + /// + /// get a lobby + /// + /// the id of the lobby + /// + /// 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 + /// + public async Task GetLobby(int id) + { + var tmp = await unity.getLobby(id); + if (tmp == null) return NoContent(); + return Ok(tmp); + } } } diff --git a/WebApi/WebApi/Unit.cs b/WebApi/WebApi/Unit.cs index a60ffe1..a97d88e 100644 --- a/WebApi/WebApi/Unit.cs +++ b/WebApi/WebApi/Unit.cs @@ -20,14 +20,21 @@ namespace WebApi public IPlayerManager PlayerManager { get; private set; } public IQuestionManager 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 administratorManager, + IAnswerManager answerManager, + IChapterManager chapterManager, + ILobbyManager lobbyManager, + IPlayerManager playerManager, + IQuestionManager 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 getLobby(string name, int? idCreator) + { + return LobbyManager.getLobby(name, idCreator); + } + public int getNbPlayers() { return PlayerManager.getNbPlayers();