You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
296 lines
9.2 KiB
296 lines
9.2 KiB
using DTO;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Shared;
|
|
using System.Net;
|
|
namespace WfApi.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("api/v1/users")] //Version API
|
|
|
|
public class UsersController : ControllerBase
|
|
{
|
|
private readonly IUserService<UserDTO> _user;
|
|
|
|
private readonly ILogger<UsersController> _logger;
|
|
public UsersController(IUserService<UserDTO> userService, ILogger<UsersController> logger)
|
|
{
|
|
_user = userService;
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
//===================================== ROUTE GET =====================================
|
|
|
|
[HttpGet("id/{id}")] // Indiquer que l'id est dans l'URL
|
|
public async Task<IActionResult> Get(int id)
|
|
{
|
|
try
|
|
{
|
|
var result = _user.GetUserById(id);
|
|
|
|
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("all")] // Indiquer que l'id est dans l'URL
|
|
public async Task<IActionResult> GetAllUsers(int index = 0, int count = 5)
|
|
{
|
|
try
|
|
{
|
|
var result = _user.GetSomeUser(index, count);
|
|
|
|
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("hashpassword/{username}")] // Indiquer que l'id est dans l'URL
|
|
public async Task<IActionResult> GetHashPassword(string username)
|
|
{
|
|
try
|
|
{
|
|
var result = _user.GetHashPassword(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("username/{username}")] // Indiquer que l'id est dans l'URL
|
|
public async Task<IActionResult> GetUserByUsername(string username)
|
|
{
|
|
try
|
|
{
|
|
var result = _user.GetUserByUsername(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("email/{email}")] // Indiquer que l'id est dans l'URL
|
|
public async Task<IActionResult> GetUserByEmail(string email)
|
|
{
|
|
try
|
|
{
|
|
var result = _user.GetUserByEmail(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" });
|
|
}
|
|
}
|
|
|
|
|
|
[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." });
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|