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.
131 lines
4.8 KiB
131 lines
4.8 KiB
using Dto;
|
|
using Asp.Versioning;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Shared;
|
|
|
|
namespace API.Controllers
|
|
{
|
|
[Route("api/v{version:apiVersion}/[controller]")]
|
|
[Authorize]
|
|
[ApiVersion("1.0")]
|
|
[ApiController]
|
|
public class UserController(ILogger<UserController> logger, IUserService<UserDTO> userService) : ControllerBase
|
|
{
|
|
[HttpGet("users/{page}/{number}")]
|
|
[ProducesResponseType(typeof(UserDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 204)]
|
|
public IActionResult GetUsers(int page, int number)
|
|
{
|
|
var users = userService.GetUsers(page, number).ToList();
|
|
if (users.Count == 0)
|
|
{
|
|
logger.LogError("[ERREUR] Aucun utilisateur trouvé.");
|
|
return StatusCode(204);
|
|
}
|
|
|
|
logger.LogInformation("[INFORMATION] {nb} Utilisateur(s) trouvé(s)", users.Count);
|
|
return Ok(users);
|
|
}
|
|
|
|
[HttpGet("user/id/{id}")]
|
|
[ProducesResponseType(typeof(UserDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
public IActionResult GetUserById(int id)
|
|
{
|
|
try
|
|
{
|
|
logger.LogInformation("[INFORMATION] Utilisateur avec l'id {id} a été trouvé.", id);
|
|
return Ok(userService.GetUserById(id));
|
|
}
|
|
catch (ArgumentException)
|
|
{
|
|
logger.LogError("[ERREUR] Aucun utilisateur trouvé avec l'id {id}.", id);
|
|
return NotFound();
|
|
}
|
|
}
|
|
|
|
[HttpGet("user/username/{username}")]
|
|
[ProducesResponseType(typeof(UserDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
public IActionResult GetUserByUsername(string username)
|
|
{
|
|
try
|
|
{
|
|
logger.LogInformation("[INFORMATION] Utilisateur avec l'username {username} a été trouvé.", username);
|
|
return Ok(userService.GetUserByUsername(username));
|
|
}
|
|
catch (ArgumentException)
|
|
{
|
|
logger.LogError("[ERREUR] Aucun utilisateur trouvé avec l'username {username}.", username);
|
|
return NotFound();
|
|
}
|
|
}
|
|
|
|
[HttpDelete]
|
|
[ProducesResponseType(typeof(UserDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
public IActionResult DeleteUser(int id)
|
|
{
|
|
var success = userService.DeleteUser(id);
|
|
if (success)
|
|
{
|
|
logger.LogInformation("[INFORMATION] L'utilisateur avec l'id {id} a été supprimé.", id);
|
|
return Ok(userService.DeleteUser(id));
|
|
}
|
|
else
|
|
{
|
|
logger.LogError("[ERREUR] Aucun utilisateur trouvé avec l'id {id}.", id);
|
|
return NotFound();
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
[ProducesResponseType(typeof(UserDTO), 201)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
public IActionResult CreateUser([FromBody] UserDTO dto)
|
|
{
|
|
if (dto.Username == null || dto.Password == null || dto.Email == null)
|
|
{
|
|
return BadRequest();
|
|
}
|
|
|
|
// return Ok(userService.CreateUser(username, password, email, isAdmin));
|
|
logger.LogInformation(
|
|
"[INFORMATION] Un utilisateur a été créé : username - {username}, password - {password}, email - {email}, isAdmin - {isAdmin}",
|
|
dto.Username, dto.Password, dto.Email, dto.IsAdmin);
|
|
return Created(nameof(GetUsers),
|
|
userService.CreateUser(dto.Username, dto.Password, dto.Email, dto.IsAdmin));
|
|
}
|
|
|
|
[HttpPut]
|
|
[ProducesResponseType(typeof(UserDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
public IActionResult UpdateUser(int id, [FromBody] UserDTO userDto)
|
|
{
|
|
if (id != userDto.Id)
|
|
{
|
|
logger.LogError("[ERREUR] Problème ID - La mise à jour de l'utilisateur avec l'id {id} a échouée.", id);
|
|
return BadRequest();
|
|
}
|
|
|
|
if (!ModelState.IsValid)
|
|
{
|
|
logger.LogError(
|
|
"[ERREUR] Problème controlleur - La mise à jour de l'utilisateur avec l'id {id} a échouée.", id);
|
|
return BadRequest();
|
|
}
|
|
|
|
if (userDto != null)
|
|
{
|
|
logger.LogInformation("[INFORMATION] La mise à jour de l'utilsiateur avec l'id {id} a été effectuée",
|
|
id);
|
|
return Ok(userService.UpdateUser(id, userDto));
|
|
}
|
|
|
|
logger.LogError("[ERREUR] Aucun utilisateur trouvé avec l'id {id}.", id);
|
|
return NotFound();
|
|
}
|
|
}
|
|
} |