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.
136 lines
5.3 KiB
136 lines
5.3 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 IdataService<UserDTO, LessonDTO, InquiryDTO, ParagraphDTO, SuccessDTO> _dataService;
|
|
|
|
private readonly ILogger<LessonsController> _logger;
|
|
|
|
public LessonsController(IdataService<UserDTO, LessonDTO, InquiryDTO, ParagraphDTO, SuccessDTO> dataService, ILogger<LessonsController> logger)
|
|
{
|
|
_dataService = dataService;
|
|
_logger = logger;
|
|
}
|
|
|
|
[HttpGet("lessons/{page:int}/{number:int}/{orderCriteria}")]
|
|
[ProducesResponseType(typeof(LessonDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 204)]
|
|
public async Task<IActionResult> GetLessons(int page, int number, LessonOrderCriteria orderCriteria)
|
|
{
|
|
var nbLesson = (await _dataService.lessonService.GetItems(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(_dataService.lessonService.GetLessons(page, number, orderCriteria));
|
|
}
|
|
|
|
[HttpGet("lesson/{id:int}")]
|
|
[ProducesResponseType(typeof(LessonDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
public async Task<IActionResult> GetLessonById(int id)
|
|
{
|
|
try
|
|
{
|
|
_logger.LogInformation("[INFORMATION] La leçon avec l'id {id} a été trouvé.", id);
|
|
return Ok( await _dataService.lessonService.GetItems(1, 1, null, LessonOrderCriteria.ById.ToString().Substring(2), 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 async Task<IActionResult> GetLessonByTitle(string title)
|
|
{
|
|
try
|
|
{
|
|
_logger.LogInformation("[INFORMATION] La leçon avec le titre {title} a été trouvé.", title);
|
|
return Ok(await _dataService.lessonService.GetItems(1, 1, null, LessonOrderCriteria.ByTitle.ToString().Substring(2), 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 async Task<IActionResult> DeleteLesson(int id)
|
|
{
|
|
var success = await _dataService.lessonService.DeleteItem(id);
|
|
if (success)
|
|
{
|
|
_logger.LogInformation("[INFORMATION] La leçon avec l'id {id} a été supprimé.", id);
|
|
return Ok();
|
|
}
|
|
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 async Task<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), await _dataService.lessonService.AddItem(dto));
|
|
}
|
|
|
|
[HttpPut("lesson/{id:int}")]
|
|
[ProducesResponseType(typeof(LessonDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
public async Task<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(await _dataService.lessonService.UpdateItem<LessonDTO>(id, lessonDTO));
|
|
}
|
|
|
|
_logger.LogError("[ERREUR] Aucune leçon trouvée avec l'id {id}.", id);
|
|
return NotFound();
|
|
}
|
|
}
|
|
} |