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.
132 lines
5.1 KiB
132 lines
5.1 KiB
using Dto;
|
|
using Asp.Versioning;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Shared;
|
|
using Model.OrderCriteria;
|
|
|
|
namespace API.Controllers
|
|
{
|
|
[Route("api/v{version:apiVersion}/[controller]")]
|
|
[Authorize]
|
|
[ApiVersion("1.0")]
|
|
[ApiController]
|
|
public class UsersController(ILogger<UsersController> logger, IdataService<UserDTO, LessonDTO, InquiryDTO, ParagraphDTO, SuccessDTO> dataService) : ControllerBase
|
|
{
|
|
[HttpGet("users/{page}/{number}")]
|
|
[ProducesResponseType(typeof(UserDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 204)]
|
|
public async Task<IActionResult> GetUsers(int page, int number, UserOrderCriteria orderCriteria)
|
|
{
|
|
var users = (await dataService.userService.GetItems(page, number, orderCriteria)).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 async Task<IActionResult> GetUserById(int id)
|
|
{
|
|
try
|
|
{
|
|
logger.LogInformation("[INFORMATION] Utilisateur avec l'id {id} a été trouvé.", id);
|
|
return Ok( (await dataService.userService.GetItems(1, 1, null, UserOrderCriteria.ById.ToString().Substring(2), 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(dataService.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 = dataService.userService.DeleteUser(id);
|
|
if (success)
|
|
{
|
|
logger.LogInformation("[INFORMATION] L'utilisateur avec l'id {id} a été supprimé.", id);
|
|
return Ok(dataService.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),
|
|
dataService.userService.CreateUser(dto.Username, dto.Password, dto.Email, dto.IsAdmin));
|
|
}
|
|
|
|
[HttpPut("user/{id}")]
|
|
[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(dataService.userService.UpdateUser(id, userDto));
|
|
}
|
|
|
|
logger.LogError("[ERREUR] Aucun utilisateur trouvé avec l'id {id}.", id);
|
|
return NotFound();
|
|
}
|
|
}
|
|
} |