modification du controller des administrators et ajout du FrontController

API
Damien NORTIER 1 year ago
parent c0b5d1e4c5
commit 52375d4e0f

@ -11,8 +11,8 @@ using Microsoft.AspNetCore.Http.HttpResults;
namespace WebApi.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class AdministratorController
[Route("api/v[version]/")]
public class AdministratorController : ControllerBase
{
private IAdministratorManager<AdministratorDto> mgr;
@ -24,28 +24,96 @@ namespace WebApi.Controllers
this.logger = logger;
}
/// <summary>
/// add an administrator
/// </summary>
/// <param name="administrator">the administrator to add</param>
/// <returns>
/// status code :
/// 200 if the administrator is added
/// 202 if the administrator is added
/// 208 if the administrator 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
/// administrator added when status code = 202
/// administrator 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/administrator/")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status202Accepted)]
[ProducesResponseType(StatusCodes.Status208AlreadyReported)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> PostAdministrator([FromBody] AdministratorDto administrator)
{
int count = mgr.getNbElements();
var tmp = await mgr.addAdmin(administrator);
int count = mgr.getNbElements(); // count : number of elements before opperation
var tmp = await mgr.addAdmin(administrator); // tmp : administrator recieve by the addAdmin method
if (mgr.getNbElements() == count) // <=> not added
{
return StatusCode(208) ;
if(tmp.Username == administrator.Username)
{
// it was already in the database
return StatusCode(208, tmp);
}
else
{
// 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" +
$" 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);
}
}
logger.LogInformation(message: $"administrator added : {administrator.ToString()}");
return StatusCode(202);
// added
if (tmp.Username == administrator.Username)
{
logger.LogInformation(message: $"administrator added : {tmp.ToString()}");
// the administrator has been added and we recieved him
return StatusCode(202, tmp);
}
else
{
// the administrator may be added but we do not recieved him
if (mgr.getAdministratorByUsername(administrator.Username) != null)
{
logger.LogError(message: $"administrator added but not recieved : {tmp.ToString()}");
// he is added
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");
if (mgr.getAdministratorByUsername(administrator.Username) == null) // <=> not added
return StatusCode(500);
}
}
logger.LogError(message: "this case should not append");
return StatusCode(500);
}
[HttpGet("administrators")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> GetSomeAdministrators(int page, int count = 10, AdministratorOrderCriteria orderCriteria = AdministratorOrderCriteria.ById)
{
var tmp = await mgr.getAdministrators(page, count, orderCriteria);
if (tmp.administrators == null)
{
return BadRequest(tmp);
return BadRequest();
}
else if (tmp.administrators.Count() == 0)
{
return NoContent();
}
else
{
return Ok(tmp);
}
else return Ok(tmp);
}
[HttpDelete()]
@ -53,11 +121,21 @@ namespace WebApi.Controllers
{
if (id < 0)
{
logger.LogError("want to delete an administrator with an id less than 0");
return BadRequest();
}
int count = mgr.getNbElements(); // count : number of elements before opperation
var tmp = await mgr.removeAdmin(id);
if (tmp == null) return StatusCode(403);
logger.LogInformation(message: $"administrator removed : ${tmp}");
if(mgr.getNbElements() == count)
{
// <=> 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");
return StatusCode(500);
}
logger.LogInformation(message: $"administrator removed {tmp.ToString()}");
return Ok(tmp);
}
}

@ -0,0 +1,15 @@
namespace WebApi.Controllers
{
public class FrontController
{
// all secondary controllers
private AdministratorController administratorController;
private AnswerController answerController;
private ChapterController chapterController;
private LobbyController lobbyController;
private PlayerController playerController;
private QuestionController questionController;
}
}
Loading…
Cancel
Save