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.
140 lines
5.1 KiB
140 lines
5.1 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 LessonsController : Controller
|
|
{
|
|
private readonly ILessonService<LessonDTO> _lessonDataService;
|
|
|
|
private readonly ILogger<LessonsController> _logger;
|
|
|
|
public LessonsController(ILessonService<LessonDTO> lessonDataService, ILogger<LessonsController> logger)
|
|
{
|
|
_lessonDataService = lessonDataService;
|
|
_logger = logger;
|
|
}
|
|
|
|
[HttpGet("lessons/{page:int}/{number:int}/{orderCriteria}")]
|
|
[ProducesResponseType(typeof(LessonDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 204)]
|
|
public IActionResult GetLessons(int page, int number, LessonOrderCriteria orderCriteria)
|
|
{
|
|
var nbLesson = _lessonDataService.GetLessons(page, number, orderCriteria).Count();
|
|
if (nbLesson == 0)
|
|
{
|
|
_logger.LogError("[ERREUR] Aucune leçon trouvée.");
|
|
return StatusCode(204);
|
|
}
|
|
|
|
_logger.LogInformation("[INFORMATION] {nb} Leçon(s) trouvée(s)", nbLesson);
|
|
return Ok(_lessonDataService.GetLessons(page, number, orderCriteria));
|
|
}
|
|
|
|
[HttpGet("lesson/{id:int}")]
|
|
[ProducesResponseType(typeof(LessonDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
public IActionResult GetLessonById(int id)
|
|
{
|
|
try
|
|
{
|
|
_logger.LogInformation("[INFORMATION] La leçon avec l'id {id} a été trouvé.", id);
|
|
return Ok(_lessonDataService.GetLessonById(id));
|
|
}
|
|
catch (ArgumentException)
|
|
{
|
|
_logger.LogError("[ERREUR] Aucune leçon trouvée avec l'id {id}.", id);
|
|
return NotFound();
|
|
}
|
|
}
|
|
|
|
[HttpGet("lesson/{title:alpha}")]
|
|
[ProducesResponseType(typeof(LessonDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
public IActionResult GetLessonByTitle(string title)
|
|
{
|
|
try
|
|
{
|
|
_logger.LogInformation("[INFORMATION] La leçon avec le titre {title} a été trouvé.", title);
|
|
return Ok(_lessonDataService.GetLessonByTitle(title));
|
|
}
|
|
catch (ArgumentException)
|
|
{
|
|
_logger.LogError("[ERREUR] Aucune leçon trouvée avec le titre {title}.", title);
|
|
return NotFound();
|
|
}
|
|
}
|
|
|
|
[HttpDelete("lesson/{id:int}")]
|
|
[ProducesResponseType(typeof(LessonDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
public IActionResult DeleteLesson(int id)
|
|
{
|
|
var success = _lessonDataService.DeleteLesson(id);
|
|
if (success)
|
|
{
|
|
_logger.LogInformation("[INFORMATION] La leçon avec l'id {id} a été supprimé.", id);
|
|
return Ok(_lessonDataService.DeleteLesson(id));
|
|
}
|
|
else
|
|
{
|
|
_logger.LogError("[ERREUR] Aucune leçon trouvée avec l'id {id}.", id);
|
|
return NotFound();
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
[ProducesResponseType(typeof(LessonDTO), 201)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
public IActionResult CreateLesson([FromBody] LessonDTO dto)
|
|
{
|
|
if (dto.Title == null || dto.LastPublisher == null)
|
|
{
|
|
return BadRequest();
|
|
}
|
|
|
|
_logger.LogInformation(
|
|
"[INFORMATION] Une leçon a été créé : title - {title}, lastPublisher - {publisher}, lastEdit - {lastEdit}",
|
|
dto.Title, dto.LastPublisher, dto.LastEdit);
|
|
return Created(nameof(GetLessons),
|
|
_lessonDataService.CreateLesson(dto.Title, dto.LastPublisher, dto.LastEdit));
|
|
}
|
|
|
|
[HttpPut("lesson/{id:int}")]
|
|
[ProducesResponseType(typeof(LessonDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
public IActionResult UpdateLesson(int id, [FromBody] LessonDTO lessonDTO)
|
|
{
|
|
if (id != lessonDTO.Id)
|
|
{
|
|
_logger.LogError("[ERREUR] Problème ID - La mise à jour de la leçon avec l'id {id} a échouée.", id);
|
|
return BadRequest();
|
|
}
|
|
|
|
if (!ModelState.IsValid)
|
|
{
|
|
_logger.LogError("[ERREUR] Problème controlleur - La mise à jour de la leçon avec l'id {id} a échouée.",
|
|
id);
|
|
return BadRequest();
|
|
}
|
|
|
|
if (lessonDTO != null)
|
|
{
|
|
_logger.LogInformation("[INFORMATION] La mise à jour de la leçon avec l'id {id} a été effectuée", id);
|
|
return Ok(_lessonDataService.UpdateLesson(id, lessonDTO));
|
|
}
|
|
|
|
_logger.LogError("[ERREUR] Aucune leçon trouvée avec l'id {id}.", id);
|
|
return NotFound();
|
|
}
|
|
}
|
|
} |