|
|
|
@ -28,8 +28,8 @@ namespace WfApi.Controllers
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var result = _user.GetUserById(id);
|
|
|
|
|
|
|
|
|
|
if(result.IsCompletedSuccessfully)
|
|
|
|
|
|
|
|
|
|
if (result.IsCompletedSuccessfully)
|
|
|
|
|
{
|
|
|
|
|
return await Task.FromResult<IActionResult>(Ok(result));
|
|
|
|
|
}
|
|
|
|
@ -46,7 +46,7 @@ namespace WfApi.Controllers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet("all")] // Indiquer que l'id est dans l'URL
|
|
|
|
|
public async Task<IActionResult> GetAllUsers(int index =0, int count = 5)
|
|
|
|
|
public async Task<IActionResult> GetAllUsers(int index = 0, int count = 5)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
@ -135,6 +135,161 @@ namespace WfApi.Controllers
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet("countuser")] // Indiquer que l'id est dans l'URL
|
|
|
|
|
public async Task<IActionResult> GetCountUser()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var result = _user.CountUser();
|
|
|
|
|
|
|
|
|
|
if (result.IsCompletedSuccessfully)
|
|
|
|
|
{
|
|
|
|
|
return await Task.FromResult<IActionResult>(Ok(result));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet("existusername/{username}")] // Indiquer que l'id est dans l'URL
|
|
|
|
|
public async Task<IActionResult> GetExistUsername(string username)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var result = _user.ExistUsername(username);
|
|
|
|
|
|
|
|
|
|
if (result.IsCompletedSuccessfully)
|
|
|
|
|
{
|
|
|
|
|
return await Task.FromResult<IActionResult>(Ok(result));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet("existemail/{email}")] // Indiquer que l'id est dans l'URL
|
|
|
|
|
public async Task<IActionResult> GetExistEmail(string email)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var result = _user.ExistEmail(email);
|
|
|
|
|
|
|
|
|
|
if (result.IsCompletedSuccessfully)
|
|
|
|
|
{
|
|
|
|
|
return await Task.FromResult<IActionResult>(Ok(result));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//===================================== ROUTE PUT =====================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPut()]
|
|
|
|
|
public async Task<IActionResult> UpdateUser([FromQuery] int id, [FromBody] UserDTO updateduser)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (updateduser == null)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest(new { message = "Player data is required." });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var existingUser = _user.GetUserById(id).Result;
|
|
|
|
|
if (existingUser == null)
|
|
|
|
|
{
|
|
|
|
|
return NotFound(new { message = "Player not found." });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = _user.UpdateUser(id,updateduser,existingUser);
|
|
|
|
|
|
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal server error." });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//===================================== ROUTE POST =====================================
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public async Task<IActionResult> CreatePlayer([FromBody] UserDTO newUser)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (newUser == null)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest(new { message = "Les données du joueur sont requises." });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var existingPlayer = _user.GetUserById(newUser.Id).Result;
|
|
|
|
|
if (existingPlayer != null)
|
|
|
|
|
{
|
|
|
|
|
return Conflict(new { message = "Un utilisateur avec cet ID existe déjà." });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_user.AddUser(newUser);
|
|
|
|
|
|
|
|
|
|
return CreatedAtAction(nameof(GetAllUsers), new { id = newUser.Id }, newUser);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Erreur interne du serveur." });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//===================================== ROUTE DELETE =====================================
|
|
|
|
|
|
|
|
|
|
[HttpDelete] // /api/v1/players?id=51
|
|
|
|
|
public async Task<IActionResult> DeletePlayer([FromQuery] int id)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
var existingPlayer = _user.GetUserById(id).Result;
|
|
|
|
|
if (existingPlayer == null)
|
|
|
|
|
{
|
|
|
|
|
return NotFound(new { message = "Player not found." });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_user.RemoveUser(existingPlayer);
|
|
|
|
|
|
|
|
|
|
return Ok(new { message = $"Player {id} deleted successfully." });
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal server error." });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|