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 public async Task GetQuote(int id) { try { var result = _quote.GetQuoteById(id); if (result.IsCompletedSuccessfully) { return await Task.FromResult(Ok(result)); } else { return NoContent(); } } catch (Exception) { return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" }); } } [HttpGet("all")] public async Task GetAllQuote(int index = 0, int count = 10) { try { var result = _quote.GetSomeQuote(index, count); if (result.IsCompletedSuccessfully) { return await Task.FromResult(Ok(result)); } else { return NoContent(); } } catch (Exception) { return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" }); } } [HttpGet("allbylang")] public async Task GetAllQuoteByLang(TypeLangageDTO lang,int index = 0, int count = 10) { try { var result = _quote.GetAllQuoteLang(index, count,(int)lang); if (result.IsCompletedSuccessfully) { return await Task.FromResult(Ok(result)); } else { return NoContent(); } } catch (Exception) { return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" }); } } [HttpGet("dailyquote")] 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 = _quote.GetDailyQuote(date, (int)lang); if (result.IsCompletedSuccessfully) { return await Task.FromResult(Ok(result)); } else { return NoContent(); } } catch (Exception) { return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" }); } } [HttpGet("invalid")] public async Task GetInvalidQuote(TypeLangageDTO lang, int index = 0, int count = 10) { try { var result = _quote.GetInvalidQuote(index, count, (int)lang); if (result.IsCompletedSuccessfully) { return await Task.FromResult(Ok(result)); } else { return NoContent(); } } catch (Exception) { return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" }); } } [HttpGet("suggest")] public async Task GetSuggestQuote(TypeLangageDTO lang, int index = 0, int count = 10) { try { var result = _quote.GetSuggestions(index, count, (int)lang); if (result.IsCompletedSuccessfully) { return await Task.FromResult(Ok(result)); } else { return NoContent(); } } catch (Exception) { return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" }); } } [HttpGet("searchByCharacter")] public async Task GetQuoteSearchByCharacter(TypeLangageDTO lang, string character, int index = 0, int count = 10) { try { var result = _quote.SearchByCharacter(character, index, count, (int)lang); if (result.IsCompletedSuccessfully) { return await Task.FromResult(Ok(result)); } else { return NoContent(); } } catch (Exception) { return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" }); } } [HttpGet("searchByCharac")] public async Task GetQuoteSearchBySource(TypeLangageDTO lang, string source, int index = 0, int count = 10) { try { var result = _quote.SearchBySource(source, index, count, (int)lang); if (result.IsCompletedSuccessfully) { return await Task.FromResult(Ok(result)); } else { return NoContent(); } } catch (Exception) { return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" }); } } [HttpGet("searchByContent")] public async Task GetQuoteSearchByContent(TypeLangageDTO lang, string content, int index = 0, int count = 10) { try { var result = _quote.SearchByContent(content, index, count, (int)lang); if (result.IsCompletedSuccessfully) { return await Task.FromResult(Ok(result)); } else { return NoContent(); } } catch (Exception) { return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" }); } } //===================================== ROUTE POST ===================================== [HttpPost] public async Task CreateQuote([FromBody] QuoteDTO newQuote) { try { if (newQuote == null) { return BadRequest(new { message = "Les données de la quote sont requises." }); } var existingPlayer = _quote.GetQuoteById(newQuote.Id).Result; if (existingPlayer != null) { return Conflict(new { message = "Une quote avec cet ID existe déjà." }); } _quote.AddQuote(newQuote); return CreatedAtAction(nameof(GetAllQuote), new { id = newQuote.Id }, newQuote); } catch (Exception) { return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Erreur interne du serveur." }); } } //===================================== ROUTE PUT ===================================== [HttpPut()] 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")] 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")] public async Task DeleteQuote([FromQuery] int idQuote) { try { var result = _quote.RemoveQuote(idQuote); if (result.IsCompletedSuccessfully) { return await Task.FromResult(Ok(result)); } else { return NotFound(); } } catch (Exception) { return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" }); } } } }