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.
API_SQLuedo/API_SQLuedo/API/Controllers/UserController.cs

130 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:int}")]
[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:alpha}")]
[ProducesResponseType(typeof(UserDTO), 200)]
[ProducesResponseType(typeof(string), 404)]
public async Task<IActionResult> GetUserByUsername(string username)
{
try
{
logger.LogInformation("[INFORMATION] Utilisateur avec l'username {username} a été trouvé.", username);
return Ok( await _dataService.userService.GetItems(1, 1, null, UserOrderCriteria.ByUsername.ToString().Substring(2) , username));
}
catch (ArgumentException)
{
logger.LogError("[ERREUR] Aucun utilisateur trouvé avec l'username {username}.", username);
return NotFound();
}
}
[HttpDelete("user/{id}")]
[ProducesResponseType(typeof(UserDTO), 200)]
[ProducesResponseType(typeof(string), 404)]
public async Task<IActionResult> DeleteUser(int id)
{
var success = await _dataService.userService.DeleteItem(id);
if (success)
{
logger.LogInformation("[INFORMATION] L'utilisateur avec l'id {id} a été supprimé.", id);
return Ok();
}
else
{
logger.LogError("[ERREUR] Aucun utilisateur trouvé avec l'id {id}.", id);
return NotFound();
}
}
[HttpPost]
[ProducesResponseType(typeof(UserDTO), 201)]
[ProducesResponseType(typeof(string), 400)]
public async Task<IActionResult> CreateUser([FromBody] UserDTO dto)
{
if (dto.Username == null || dto.Password == null || dto.Email == null)
{
return BadRequest();
}
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),
await _dataService.userService.AddItem(dto));
}
[HttpPut("user/{id}")]
[ProducesResponseType(typeof(UserDTO), 200)]
[ProducesResponseType(typeof(string), 400)]
[ProducesResponseType(typeof(string), 404)]
public async Task<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(await _dataService.userService.UpdateItem<UserDTO>(id, userDto));
}
logger.LogError("[ERREUR] Aucun utilisateur trouvé avec l'id {id}.", id);
return NotFound();
}
}
}