using DTO; using Entity; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Shared; using System.Net; namespace WfApi.Controllers { [ApiController] [Route("api/v1/quote")] //Version API public class QuotesController : ControllerBase { private readonly IQuoteService _quote; private readonly ILogger _logger; public QuotesController(IQuoteService quoteService, ILogger logger) { _quote = quoteService; _logger = logger; } //===================================== ROUTE GET ===================================== [HttpGet("{id}")] // Indiquer que l'id est dans l'URL [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task GetQuote(int id) { try { var result = await _quote.GetQuoteById(id); if (result!=null) { return Ok(result); } else { return NoContent(); } } catch (Exception) { return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" }); } } [HttpGet("all")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task GetAllQuote(int index = 0, int count = 10) { try { var result = await _quote.GetSomeQuote(index, count); if (result != null) { return await Task.FromResult(Ok(result)); } else { return NoContent(); } } catch (Exception) { return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" }); } } [HttpGet("allbylang")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task GetAllQuoteByLang(TypeLangageDTO lang,int index = 0, int count = 10) { try { var result = await _quote.GetAllQuoteLang(index, count,(int)lang); if (result != null) { return await Task.FromResult(Ok(result)); //return Ok(result)); } else { return NoContent(); } } catch (Exception) { return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" }); } } [HttpGet("dailyquote")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task GetDailyQuote([FromQuery] int year, [FromQuery] int month, [FromQuery] int day, TypeLangageDTO lang) { try { DateOnly date = new DateOnly(year, month, day); var result = await _quote.GetDailyQuote(date, (int)lang); if (result != null) { return await Task.FromResult(Ok(result)); } else { return NoContent(); } } catch (Exception) { return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" }); } } [HttpGet("invalid")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task GetInvalidQuote(TypeLangageDTO lang, int index = 0, int count = 10) { try { var result =await _quote.GetInvalidQuote(index, count, (int)lang); if (result != null) { return await Task.FromResult(Ok(result)); } else { return NoContent(); } } catch (Exception) { return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" }); } } [HttpGet("suggest")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task GetSuggestQuote(TypeLangageDTO lang, int index = 0, int count = 10) { try { var result = await _quote.GetSuggestions(index, count, (int)lang); if (result != null) { return await Task.FromResult(Ok(result)); } else { return NoContent(); } } catch (Exception) { return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" }); } } [HttpGet("searchByCharacter")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task GetQuoteSearchByCharacter(TypeLangageDTO lang, string character, int index = 0, int count = 10) { try { var result = await _quote.SearchByCharacter(character, index, count, (int)lang); if (result != null) { return await Task.FromResult(Ok(result)); } else { return NoContent(); } } catch (Exception) { return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" }); } } [HttpGet("searchBySource")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task GetQuoteSearchBySource(TypeLangageDTO lang, string source, int index = 0, int count = 10) { try { var result = await _quote.SearchBySource(source, index, count, (int)lang); if (result != null) { return await Task.FromResult(Ok(result)); } else { return NoContent(); } } catch (Exception) { return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" }); } } [HttpGet("searchByContent")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task GetQuoteSearchByContent(TypeLangageDTO lang, string content, int index = 0, int count = 10) { try { var result =await _quote.SearchByContent(content, index, count, (int)lang); if (result != null) { return await Task.FromResult(Ok(result)); } else { return NoContent(); } } catch (Exception) { return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" }); } } //===================================== ROUTE POST ===================================== [HttpPost] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task CreateQuote([FromBody] QuoteDTO newQuote) { try { try { if (newQuote == null) { return BadRequest(new { message = "Les données de la quote sont requises." }); } if (await _quote.GetQuoteById(newQuote.Id) != null) { return Conflict(new { message = "Une quote avec cet ID existe déjà." }); } newQuote.IsValide=false; var quote=await _quote.AddQuote(newQuote); return CreatedAtAction(nameof(CreateQuote), new { id = newQuote.Id }, quote); } catch (KeyNotFoundException e) { return StatusCode((int)HttpStatusCode.NotFound, e); } } catch (Exception) { return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Erreur interne du serveur." }); } } //===================================== ROUTE PUT ===================================== [HttpPut()] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task UpdateQuote([FromQuery] int id, [FromBody] QuoteDTO updatedquote) { try { if (updatedquote == null) { return BadRequest(new { message = "Quote data is required." }); } var result = _quote.UpdateQuote(id, updatedquote); return Ok(result); } catch (Exception) { return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal server error." }); } } [HttpPut("valide")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task ValideQuote([FromQuery] int id) { try { var updatedquote = _quote.GetQuoteById(id).Result; if (updatedquote == null) { return BadRequest(new { message = "Id Quote is underfined." }); } updatedquote.IsValide = true; var result = _quote.UpdateQuote(id, updatedquote); return Ok(result); } catch (Exception) { return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal server error." }); } } //===================================== ROUTE DELETE ===================================== [HttpDelete("delete")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task DeleteQuote([FromQuery] int idQuote) { try { try { _quote.RemoveQuote(idQuote).Wait(); return await Task.FromResult(Ok()); } catch (KeyNotFoundException e) { return StatusCode((int)HttpStatusCode.NotFound, e); } } catch (Exception) { return StatusCode((int) HttpStatusCode.InternalServerError, new { message = "Erreur interne du serveur." }); } } } }