terminaison (enfin) du contrôleur administrateur ⚠️ ne compile pas car le frontController instancit également les autres contrôleur qui n'ont pas de constructeur
continuous-integration/drone/push Build is passing Details

API
Damien NORTIER 1 year ago
parent 328506e10e
commit 0e3032a9eb

@ -17,6 +17,11 @@ namespace DataManagers
return await Task.FromResult<Administrator>((await manager.addAdmin(admin.ToEntity())).ToModel());
}
public async Task<Administrator?> getAdministrator(int id)
{
return (await manager.getAdministrator(id))?.ToModel();
}
public async Task<Administrator?> getAdministratorByUsername(string username)
{
return await Task.FromResult<Administrator?>((await manager.getAdministratorByUsername(username))?.ToModel());
@ -56,5 +61,10 @@ namespace DataManagers
{
return await manager.setPassword(username, newHashedPassword);
}
public async Task<Administrator?> updateAdministrator(int id, Administrator admin)
{
return (await manager.updateAdministrator(id, admin.ToEntity()))?.ToModel();
}
}
}

@ -19,7 +19,7 @@ namespace EntityManagers
public async Task<AdministratorEntity> addAdmin(AdministratorEntity admin)
{
var tmp = await dbContext.Administrators.Where(a => a.Equals(admin)).FirstOrDefaultAsync();
var tmp = await dbContext.Administrators.Where(a => a.Username == admin.Username && a.HashedPassword == admin.HashedPassword).FirstOrDefaultAsync();
if (tmp != null) // <=> he already exist
{
return tmp!;
@ -29,6 +29,11 @@ namespace EntityManagers
return await dbContext.Administrators.Where(a => a.Equals(admin)).FirstAsync(); // to get
}
public async Task<AdministratorEntity?> getAdministrator(int id)
{
return await dbContext.Administrators.SingleOrDefaultAsync(a => a.Id == id);
}
public async Task<AdministratorEntity?> getAdministratorByUsername(string username)
{
return await dbContext.Administrators.Where(a => a.Username == username).FirstOrDefaultAsync();
@ -58,7 +63,7 @@ namespace EntityManagers
public async Task<AdministratorEntity?> removeAdmin(AdministratorEntity admin)
{
var tmp = await dbContext.Administrators.Where(a => a.Equals(admin)).FirstOrDefaultAsync();
var tmp = await dbContext.Administrators.Where(a => a.Username == admin.Username && a.HashedPassword == admin.HashedPassword).FirstOrDefaultAsync();
if(tmp == null) return await Task.FromResult<AdministratorEntity?>(tmp);
dbContext.Administrators.Remove(tmp);
dbContext.SaveChanges();
@ -67,7 +72,7 @@ namespace EntityManagers
public async Task<AdministratorEntity?> removeAdmin(int id)
{
var tmp = dbContext.Administrators.Where(a => a.Id == id).FirstOrDefaultAsync().Result;
var tmp = await getAdministrator(id);
if (tmp == null) return await Task.FromResult<AdministratorEntity?>(tmp);
dbContext.Administrators.Remove(tmp);
dbContext.SaveChanges();
@ -82,5 +87,15 @@ namespace EntityManagers
await dbContext.SaveChangesAsync();
return await Task.FromResult<bool>(true);
}
public async Task<AdministratorEntity?> updateAdministrator(int id, AdministratorEntity admin)
{
var tmp = await getAdministrator(id);
if (tmp == null) return null;
tmp.Username = admin.Username;
tmp.HashedPassword = admin.HashedPassword;
await dbContext.SaveChangesAsync();
return tmp;
}
}
}

@ -45,7 +45,7 @@ namespace ManagerInterfaces
/// <summary>
/// get a part of all administrators
/// </summary>
/// <param name="nb">the actual page</param>
/// <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>
@ -55,7 +55,13 @@ namespace ManagerInterfaces
/// does not exist (<=> (nb-1)*count outside
/// boundaries (0, getNbElement()-1)))
/// </returns>
public Task<(int nbPages, IEnumerable<T>? administrators)> getAdministrators(int nb, int count, AdministratorOrderCriteria orderCriteria = AdministratorOrderCriteria.ById);
public Task<(int nbPages, IEnumerable<T>? administrators)> getAdministrators(int page, int count, AdministratorOrderCriteria orderCriteria = AdministratorOrderCriteria.ById);
/// <summary>
/// get an administrator by his identifier
/// </summary>
/// <param name="id">the identifier of the administrator</param>
/// <returns>the administrator that corresponde or null if there isn't any</returns>
public Task<T?> getAdministrator(int id);
/// <summary>
/// get an administrator by his username
/// </summary>
@ -72,5 +78,12 @@ namespace ManagerInterfaces
/// false otherwise (no administrator with this username
/// </returns>
public Task<bool> setPassword(string username, string newHashedPassword);
/// <summary>
/// upadte an administrator
/// </summary>
/// <param name="id">the id of the administrator to update</param>
/// <param name="admin">the admin that contains all properties to modify</param>
/// <returns>the administrator updated or null if he don't exist</returns>
public Task<T?> updateAdministrator(int id, T admin);
}
}

@ -23,6 +23,11 @@ namespace ServiceManagers
return await Task.FromResult<AdministratorDto>((await manager.addAdmin(admin.ToModel())).ToDto());
}
public async Task<AdministratorDto?> getAdministrator(int id)
{
return (await manager.getAdministrator(id))?.ToDto();
}
public async Task<AdministratorDto?> getAdministratorByUsername(string username)
{
return await Task.FromResult<AdministratorDto?>((await manager.getAdministratorByUsername(username))?.ToDto());
@ -62,5 +67,10 @@ namespace ServiceManagers
{
return await manager.setPassword(username, newHashedPassword);
}
public async Task<AdministratorDto?> updateAdministrator(int id, AdministratorDto admin)
{
return (await manager.updateAdministrator(id, admin.ToModel()))?.ToDto();
}
}
}

@ -40,12 +40,7 @@ namespace WebApi.Controllers
/// 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)
public async Task<IActionResult> PostAdministrator(AdministratorDto administrator)
{
int count = mgr.getNbElements(); // count : number of elements before opperation
var tmp = await mgr.addAdmin(administrator); // tmp : administrator recieve by the addAdmin method
@ -54,6 +49,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()}");
return StatusCode(208, tmp);
}
else
@ -70,7 +66,7 @@ namespace WebApi.Controllers
// added
if (tmp.Username == administrator.Username)
{
logger.LogInformation(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);
}
@ -79,8 +75,8 @@ namespace WebApi.Controllers
// 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
logger.LogError(message: $"administrator added but not recieved");
return Ok(); // not 202 to make a difference between 2 cases
}
else
@ -95,28 +91,67 @@ namespace WebApi.Controllers
return StatusCode(500);
}
[HttpGet("administrators")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
/// <summary>
/// get a part of all administrators
/// </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 T element 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> GetSomeAdministrators(int page, int count = 10, AdministratorOrderCriteria orderCriteria = AdministratorOrderCriteria.ById)
{
var tmp = await mgr.getAdministrators(page, count, orderCriteria);
if (tmp.administrators == null)
{
return BadRequest();
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 : {mgr.getNbElements()}, 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
{
AdministratorOrderCriteria.ById => "byId",
AdministratorOrderCriteria.ByUserName => "byUsername",
_ => "none"
}}");
return Ok(tmp);
}
}
[HttpDelete()]
/// <summary>
/// delete an administrator
/// </summary>
/// <param name="id">the id of the administrator to delete</param>
/// <returns>
/// <returns>
/// status code :
/// 200 if the administrator is removed
/// 204 if the administrator 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> DeleteAdministrator(int id)
{
if (id < 0)
@ -126,7 +161,35 @@ namespace WebApi.Controllers
}
int count = mgr.getNbElements(); // count : number of elements before opperation
var tmp = await mgr.removeAdmin(id);
if (tmp == null) return StatusCode(403);
if (tmp == null) // we don't recieve the administrator
{
if(mgr.getNbElements() == count) // he is not deleted
{
if (mgr.getAdministrator(id) != null)
{
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");
return BadRequest();
}
}
else // he may be deleted
{
if (mgr.getAdministrator(id) == null) // he must be deleted
{
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");
return StatusCode(500);
}
}
}
if(mgr.getNbElements() == count)
{
// <=> we have recieved an administrator which should be deleted
@ -135,8 +198,96 @@ namespace WebApi.Controllers
logger.LogCritical(message: $"administrator \"{tmp.ToString()}\"should be delete but it isn't");
return StatusCode(500);
}
logger.LogInformation(message: $"administrator removed {tmp.ToString()}");
logger.LogTrace(message: $"administrator removed {tmp.ToString()}");
return Ok(tmp);
}
/// <summary>
/// update an administrator
/// </summary>
/// <param name="id">id of the administrator to update</param>
/// <param name="administrator">an administrator who contains all properties to change</param>
/// <returns>
/// status code :
/// 200 if the administrator is modified
/// 204 if we wanted to modify an administrator who don't exist
/// 208 if the new username of the administrator is already used
/// 500 if there was an internal error that doesn't allow to continue
///
/// return content :
/// the administrator modified when status code = 200
/// nothing when status code = 204
/// the administrator that already have the username when status code = 208
/// nothing when status code = 500
/// </returns>
public async Task<IActionResult> PutAdministrator(int id, AdministratorDto administrator)
{
var tmp = await mgr.getAdministratorByUsername(administrator.Username);
if (tmp != null)
{
logger.LogTrace(message: "Want to modify into an administrator who already exist");
return StatusCode(StatusCodes.Status208AlreadyReported, tmp);
}
tmp = await mgr.updateAdministrator(id, administrator);
if(tmp == null)
{
tmp = await mgr.getAdministrator(id);
if (tmp == null) // Ok, it 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");
return Ok(tmp);
}
else
{
logger.LogCritical(message: "administrator haven't changed and we recieved nothing");
return StatusCode(500);
}
}
}
else if(tmp.Id == id) // we recieve him
{
if(tmp.HashedPassword == administrator.HashedPassword
&& tmp.Username == administrator.Username) // he is changed
{
logger.LogTrace(message: $"administrator with id {id} modified in {administrator.ToString()}");
return Ok(tmp);
}
else // he haven't changed
{
var tmp2 = (await mgr.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");
else // nothing have changed
logger.LogCritical(message: "administrator should have changed but he haven't and we recieved him");
return StatusCode(500);
}
}
else // we recieve another one
{
var tmp2 = await mgr.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");
return NoContent();
}
else if (tmp2.Username == administrator.Username && tmp2.HashedPassword == administrator.HashedPassword)
{
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");
return StatusCode(500);
}
}
}
}
}

@ -1,4 +1,9 @@
namespace WebApi.Controllers
using DbConnectionLibrairie;
using DTOs;
using Microsoft.AspNetCore.Mvc;
using OrderCriterias;
namespace WebApi.Controllers
{
public class FrontController
{
@ -10,6 +15,126 @@
private PlayerController playerController;
private QuestionController questionController;
public FrontController(
MyDbContext dbContext,
Logger<AdministratorController> logAdmin,
Logger<AnswerController> logAnswer,
Logger<ChapterController> logChapter,
Logger<LobbyController> logLobby,
Logger<PlayerController> logPlayer,
Logger<QuestionController> logQuestion
)
{
administratorController = new AdministratorController(dbContext, logAdmin);
answerController = new AnswerController(dbContext, logAnswer);
chapterController = new ChapterController(dbContext, logChapter);
lobbyController = new LobbyController(dbContext, logLobby);
playerController = new PlayerController(dbContext, logPlayer);
questionController = new QuestionController(dbContext, logQuestion);
}
/// <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)
=> await administratorController.PostAdministrator(administrator);
/// <summary>
/// get a part of all administrators
/// </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 T element 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("administrators")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> GetSomeAdministrators([FromQuery] int page, int count = 10,
AdministratorOrderCriteria orderCriteria = AdministratorOrderCriteria.ById)
=> await administratorController.GetSomeAdministrators(page, count, orderCriteria);
/// <summary>
/// delete an administrator
/// </summary>
/// <param name="id">the id of the administrator to delete</param>
/// <returns>
/// <returns>
/// status code :
/// 200 if the administrator is removed
/// 204 if the administrator 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/administrator")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> DeleteAdministrator([FromQuery] int id)
=> await administratorController.DeleteAdministrator(id);
/// <summary>
/// update an administrator
/// </summary>
/// <param name="id">id of the administrator to update</param>
/// <param name="administrator">an administrator who contains all properties to change</param>
/// <returns>
/// status code :
/// 200 if the administrator is modified
/// 204 if we wanted to modify an administrator who don't exist
/// 208 if the new username of the administrator is already used
/// 500 if there was an internal error that doesn't allow to continue
///
/// return content :
/// the administrator modified when status code = 200
/// nothing when status code = 204
/// the administrator that already have the username when status code = 208
/// nothing when status code = 500
/// </returns>
[HttpPut("update/administrator")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status208AlreadyReported)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> PutAdministrator([FromQuery] int id, [FromBody] AdministratorDto administrator)
=> await administratorController.PutAdministrator(id, administrator);
}
}

Loading…
Cancel
Save