|
|
|
@ -1,118 +1,121 @@
|
|
|
|
|
using DbContextLib;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Model.Business;
|
|
|
|
|
using Model.DTO;
|
|
|
|
|
using Services;
|
|
|
|
|
|
|
|
|
|
namespace API.Controllers
|
|
|
|
|
{
|
|
|
|
|
[Route("api/[controller]")]
|
|
|
|
|
[Authorize]
|
|
|
|
|
[ApiController]
|
|
|
|
|
public class UserController : Controller
|
|
|
|
|
{
|
|
|
|
|
private IDataService _userDataService;
|
|
|
|
|
using DbContextLib;
|
|
|
|
|
using Entities.SQLudeoDB;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Model;
|
|
|
|
|
using Model.Business;
|
|
|
|
|
using Model.DTO;
|
|
|
|
|
using Model.Mappers;
|
|
|
|
|
using Services;
|
|
|
|
|
|
|
|
|
|
private readonly ILogger<UserController> _logger;
|
|
|
|
|
|
|
|
|
|
public UserController(IDataService userDataService, ILogger<UserController> logger)
|
|
|
|
|
{
|
|
|
|
|
_userDataService = userDataService;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet("users/{page}/{number}")]
|
|
|
|
|
public IActionResult GetUsers(int page, int number)
|
|
|
|
|
{
|
|
|
|
|
var nbUser = _userDataService.GetUsers(page, number).Count();
|
|
|
|
|
if(nbUser == 0)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError("[ERREUR] Aucun utilisateur trouvé.");
|
|
|
|
|
return StatusCode(204);
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("[INFORMATION] {nb} Utilisateur(s) trouvé(s)", nbUser);
|
|
|
|
|
return Ok(_userDataService.GetUsers(page, number));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet("user/id/{id}")]
|
|
|
|
|
public IActionResult GetUserById(int id)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("[INFORMATION] Utilisateur avec l'id {id} a été trouvé.", id);
|
|
|
|
|
return Ok(_userDataService.GetUserById(id));
|
|
|
|
|
} catch (ArgumentException)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError("[ERREUR] Aucun utilisateur trouvé avec l'id {id}.", id);
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet("user/username/{username}")]
|
|
|
|
|
public IActionResult GetUserByUsername(string username)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("[INFORMATION] Utilisateur avec l'username {username} a été trouvé.", username);
|
|
|
|
|
return Ok(_userDataService.GetUserByUsername(username));
|
|
|
|
|
}catch (ArgumentException)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError("[ERREUR] Aucun utilisateur trouvé avec l'username {username}.", username);
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpDelete]
|
|
|
|
|
public IActionResult DeleteUser(int id)
|
|
|
|
|
{
|
|
|
|
|
var success = _userDataService.DeleteUser(id);
|
|
|
|
|
if(success)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("[INFORMATION] L'utilisateur avec l'id {id} a été supprimé.", id);
|
|
|
|
|
return Ok(_userDataService.DeleteUser(id));
|
|
|
|
|
} else
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError("[ERREUR] Aucun utilisateur trouvé avec l'id {id}.", id);
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public IActionResult CreateUser([FromBody]UserDTO dto)
|
|
|
|
|
{
|
|
|
|
|
namespace API.Controllers
|
|
|
|
|
{
|
|
|
|
|
[Route("api/[controller]")]
|
|
|
|
|
[Authorize]
|
|
|
|
|
[ApiController]
|
|
|
|
|
public class UserController : Controller
|
|
|
|
|
{
|
|
|
|
|
private IDataService<UserDTO?> _dataService;
|
|
|
|
|
|
|
|
|
|
private readonly ILogger<UserController> _logger;
|
|
|
|
|
|
|
|
|
|
public UserController(IDataService<UserDTO?> dataService, ILogger<UserController> logger)
|
|
|
|
|
{
|
|
|
|
|
_dataService = dataService;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet("users/{page}/{number}")]
|
|
|
|
|
public async Task<IActionResult> GetUsers(int page, int number)
|
|
|
|
|
{
|
|
|
|
|
var nbUser = (await _dataService.UserService.GetUsers(page, number)).ToList().Count();
|
|
|
|
|
if(nbUser == 0)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError("[ERREUR] Aucun utilisateur trouvé.");
|
|
|
|
|
return StatusCode(204);
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("[INFORMATION] {nb} Utilisateur(s) trouvé(s)", nbUser);
|
|
|
|
|
return Ok(_dataService.UserService.GetItems<UserEntity>(page, number));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet("user/id/{id}")]
|
|
|
|
|
public async Task<IActionResult> GetUserById(int id)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("[INFORMATION] Utilisateur avec l'id {id} a été trouvé.", id);
|
|
|
|
|
return Ok(_dataService.UserService.GetItems<UserEntity>(1, 1, UserProperty.Id.ToString(),id));
|
|
|
|
|
} catch (ArgumentException)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError("[ERREUR] Aucun utilisateur trouvé avec l'id {id}.", id);
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet("user/username/{username}")]
|
|
|
|
|
public async Task<IActionResult> GetUserByUsername(string username)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("[INFORMATION] Utilisateur avec l'username {username} a été trouvé.", username);
|
|
|
|
|
return Ok(_dataService.UserService.GetItems<UserEntity>(1, 1,UserProperty.Username.ToString(), username));
|
|
|
|
|
}catch (ArgumentException)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError("[ERREUR] Aucun utilisateur trouvé avec l'username {username}.", username);
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpDelete]
|
|
|
|
|
public async Task<IActionResult> DeleteUser(int id)
|
|
|
|
|
{
|
|
|
|
|
var success = await _dataService.UserService.DeleteItem<UserEntity>(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]
|
|
|
|
|
public async Task<IActionResult> CreateUser([FromBody]UserDTO dto)
|
|
|
|
|
{
|
|
|
|
|
if (dto.Username == null || dto.Password == null || dto.Email == null)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest();
|
|
|
|
|
}
|
|
|
|
|
// return Ok(_userDataService.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), _userDataService.CreateUser(dto.Username, dto.Password, dto.Email, dto.IsAdmin));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPut]
|
|
|
|
|
public IActionResult UpdateUser(int id, [FromBody] UserDTO userDTO)
|
|
|
|
|
{
|
|
|
|
|
if(id != userDTO.Id)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_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.AddItem(dto.FromDTOToModel().FromModelToEntity()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPut]
|
|
|
|
|
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.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(_userDataService.UpdateUser(id, userDTO));
|
|
|
|
|
}
|
|
|
|
|
_logger.LogError("[ERREUR] Aucun utilisateur trouvé avec l'id {id}.", id);
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("[INFORMATION] La mise à jour de l'utilisateur avec l'id {id} a été effectuée", id);
|
|
|
|
|
return Ok(_dataService.UserService.UpdateItem<UserEntity, UserDTO>(id, userDTO));
|
|
|
|
|
}
|
|
|
|
|
_logger.LogError("[ERREUR] Aucun utilisateur trouvé avec l'id {id}.", id);
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|