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.
143 lines
5.6 KiB
143 lines
5.6 KiB
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Dto;
|
|
using Model.OrderCriteria;
|
|
using Shared;
|
|
using Asp.Versioning;
|
|
|
|
namespace API.Controllers
|
|
{
|
|
[Route("api/v{version:apiVersion}/[controller]")]
|
|
[Authorize]
|
|
[ApiVersion("1.0")]
|
|
[ApiController]
|
|
public class SuccessesController : Controller
|
|
{
|
|
private readonly ISuccessService<SuccessDTO> _successDataService;
|
|
|
|
private readonly ILogger<SuccessesController> _logger;
|
|
|
|
public SuccessesController(ISuccessService<SuccessDTO> successDataService, ILogger<SuccessesController> logger)
|
|
{
|
|
_successDataService = successDataService;
|
|
_logger = logger;
|
|
}
|
|
|
|
[HttpGet("successes/{page:int}/{number:int}/{orderCriteria}")]
|
|
[ProducesResponseType(typeof(SuccessDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 204)]
|
|
public IActionResult GetSuccesses(int page, int number, SuccessOrderCriteria orderCriteria)
|
|
{
|
|
var nbUser = _successDataService.GetSuccesses(page, number, orderCriteria).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(_successDataService.GetSuccesses(page, number, orderCriteria));
|
|
}
|
|
|
|
[HttpGet("success/user/{id:int}")]
|
|
[ProducesResponseType(typeof(SuccessDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
public IActionResult GetSuccessByUserId(int id)
|
|
{
|
|
try
|
|
{
|
|
_logger.LogInformation("[INFORMATION] Le succès avec l'id de l'utilisateur {id} a été trouvé.", id);
|
|
return Ok(_successDataService.GetSuccessByUserId(id));
|
|
}
|
|
catch (ArgumentException)
|
|
{
|
|
_logger.LogError("[ERREUR] Aucun utilisateur trouvé avec l'id de l'utilisateur {id}.", id);
|
|
return NotFound();
|
|
}
|
|
}
|
|
|
|
[HttpGet("success/inquiry/{id:int}")]
|
|
[ProducesResponseType(typeof(SuccessDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
public IActionResult GetSuccessByInquiryId(int id)
|
|
{
|
|
try
|
|
{
|
|
_logger.LogInformation("[INFORMATION] Utilisateur avec l'id de l'enquête {inquiryId} a été trouvé.",
|
|
id);
|
|
return Ok(_successDataService.GetSuccessByInquiryId(id));
|
|
}
|
|
catch (ArgumentException)
|
|
{
|
|
_logger.LogError("[ERREUR] Aucun utilisateur trouvé avec l'id de l'enquête {inquiryId}.", id);
|
|
return NotFound();
|
|
}
|
|
}
|
|
|
|
[HttpDelete("success/{id:int}")]
|
|
[ProducesResponseType(typeof(SuccessDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
public IActionResult DeleteSuccess(int id)
|
|
{
|
|
var success = _successDataService.DeleteSuccess(id);
|
|
if (success)
|
|
{
|
|
_logger.LogInformation("[INFORMATION] Le succès avec l'id {id} a été supprimé.", id);
|
|
return Ok(_successDataService.DeleteSuccess(id));
|
|
}
|
|
else
|
|
{
|
|
_logger.LogError("[ERREUR] Aucun succès trouvé avec l'id {id}.", id);
|
|
return NotFound();
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
[ProducesResponseType(typeof(SuccessDTO), 201)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
public IActionResult CreateSuccess([FromBody] SuccessDTO dto)
|
|
{
|
|
/*if (dto.UserId == null || dto.InquiryId == null)
|
|
{
|
|
return BadRequest();
|
|
}*/
|
|
_logger.LogInformation(
|
|
"[INFORMATION] Un succès a été créé : userId - {userId}, inquiryId - {inquiryId}, isFinished - {isFinished}",
|
|
dto.UserId, dto.InquiryId, dto.IsFinished);
|
|
return Created(nameof(GetSuccesses),
|
|
_successDataService.CreateSuccess(dto.UserId, dto.InquiryId, dto.IsFinished));
|
|
}
|
|
|
|
[HttpPut("success/{id:int}")]
|
|
[ProducesResponseType(typeof(SuccessDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
public IActionResult UpdateSuccess(int id, [FromBody] SuccessDTO successDTO)
|
|
{
|
|
if (id != successDTO.UserId)
|
|
{
|
|
_logger.LogError(
|
|
"[ERREUR] Problème ID - La mise à jour du succès avec l'id de l'utilisateur {id} a échouée.", id);
|
|
return BadRequest();
|
|
}
|
|
|
|
if (!ModelState.IsValid)
|
|
{
|
|
_logger.LogError(
|
|
"[ERREUR] Problème controlleur - La mise à jour du succès avec l'id de l'utilisateur {id} a échouée.",
|
|
id);
|
|
return BadRequest();
|
|
}
|
|
|
|
if (successDTO != null)
|
|
{
|
|
_logger.LogInformation(
|
|
"[INFORMATION] La mise à jour du succès avec l'id de l'utilisateur {id} a été effectuée", id);
|
|
return Ok(_successDataService.UpdateSuccess(id, successDTO));
|
|
}
|
|
|
|
_logger.LogError("[ERREUR] Aucun succès trouvé avec l'id de l'utilisateur {id}.", id);
|
|
return NotFound();
|
|
}
|
|
}
|
|
} |