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/ParagraphsController.cs

118 lines
4.7 KiB

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 ParagraphsController : Controller
{
private IDataService _paragraphDataService;
private readonly ILogger<ParagraphsController> _logger;
public ParagraphsController(IDataService paragraphDataService, ILogger<ParagraphsController> logger)
{
_paragraphDataService = paragraphDataService;
_logger = logger;
}
[HttpGet("paragraphs/{page}/{number}")]
public async Task<IActionResult> GetParagraphs(int page, int number)
{
var nbParagraph = (await _paragraphDataService.GetParagraphs(page, number)).ToList().Count();
if(nbParagraph == 0)
{
_logger.LogError("[ERREUR] Aucun paragraphe trouvé.");
return StatusCode(204);
}
_logger.LogInformation("[INFORMATION] {nb} Paragraphe(s) trouvé(s)", nbParagraph);
return Ok(_paragraphDataService.GetParagraphs(page, number));
}
[HttpGet("paragraph/id/{id}")]
public async Task<IActionResult> GetParagraphById(int id)
{
try
{
_logger.LogInformation("[INFORMATION] Le paragraphe avec l'id {id} a été trouvé.", id);
return Ok(_paragraphDataService.GetParagraphById(id));
} catch (ArgumentException)
{
_logger.LogError("[ERREUR] Aucun paragraphe trouvé avec l'id {id}.", id);
return NotFound();
}
}
[HttpGet("paragraph/title/{title}")]
public async Task<IActionResult> GetParagraphByTitle(string title)
{
try
{
_logger.LogInformation("[INFORMATION] Le paragraphe avec le titre {title} a été trouvé.", title);
return Ok(_paragraphDataService.GetUserByUsername(title));
}catch (ArgumentException)
{
_logger.LogError("[ERREUR] Aucun paragraphe trouvé avec le titre {title}.", title);
return NotFound();
}
}
[HttpDelete]
public async Task<IActionResult> DeleteParagraph(int id)
{
var success = await _paragraphDataService.DeleteParagraph(id);
if(success)
{
_logger.LogInformation("[INFORMATION] Le paragraphe avec l'id {id} a été supprimé.", id);
return Ok(_paragraphDataService.DeleteParagraph(id));
} else
{
_logger.LogError("[ERREUR] Aucun paragraphe trouvé avec l'id {id}.", id);
return NotFound();
}
}
[HttpPost]
public async Task<IActionResult> CreateParagraph([FromBody] ParagraphDTO dto)
{
if (dto.Title == null || dto.Content == null || dto.Info == null || dto.Query == null || dto.Comment == null)
{
return BadRequest();
}
_logger.LogInformation("[INFORMATION] Un paragraphe a été créé : title - {title}, content - {content}, info - {info}, query - {query}, comment - {comment}", dto.Title, dto.Content, dto.Info, dto.Query, dto.Comment);
return Created(nameof(GetParagraphs), _paragraphDataService.CreateParagraph(dto.Title, dto.Content, dto.Info, dto.Query, dto.Comment));
}
[HttpPut]
public async Task<IActionResult> UpdateParagraph(int id, [FromBody] ParagraphDTO paragraphDTO)
{
if(id != paragraphDTO.Id)
{
_logger.LogError("[ERREUR] Problème ID - La mise à jour du paragraphe avec l'id {id} a échouée.", id);
return BadRequest();
}
if(!ModelState.IsValid)
{
_logger.LogError("[ERREUR] Problème controlleur - La mise à jour du paragraphe avec l'id {id} a échouée.", id);
return BadRequest();
}
if(paragraphDTO != null)
{
_logger.LogInformation("[INFORMATION] La mise à jour du paragraphe avec l'id {id} a été effectuée", id);
return Ok(_paragraphDataService.UpdateParagraph(id, paragraphDTO));
}
_logger.LogError("[ERREUR] Aucun paragraphe trouvé avec l'id {id}.", id);
return NotFound();
}
}
}