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.
314 lines
11 KiB
314 lines
11 KiB
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<QuoteDTO> _quote;
|
|
|
|
private readonly ILogger<UsersController> _logger;
|
|
public QuotesController(IQuoteService<QuoteDTO> quoteService, ILogger<UsersController> logger)
|
|
{
|
|
_quote = quoteService;
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===================================== ROUTE GET =====================================
|
|
[HttpGet("{id}")] // Indiquer que l'id est dans l'URL
|
|
public async Task<IActionResult> GetQuote(int id)
|
|
{
|
|
try
|
|
{
|
|
var result = _quote.GetQuoteById(id);
|
|
|
|
if (result.IsCompletedSuccessfully)
|
|
{
|
|
return await Task.FromResult<IActionResult>(Ok(result));
|
|
}
|
|
else
|
|
{
|
|
return NoContent();
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" });
|
|
}
|
|
}
|
|
|
|
|
|
[HttpGet("all")]
|
|
public async Task<IActionResult> GetAllQuote(int index = 0, int count = 10)
|
|
{
|
|
try
|
|
{
|
|
var result = _quote.GetSomeQuote(index, count);
|
|
|
|
if (result.IsCompletedSuccessfully)
|
|
{
|
|
return await Task.FromResult<IActionResult>(Ok(result));
|
|
}
|
|
else
|
|
{
|
|
return NoContent();
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" });
|
|
}
|
|
}
|
|
[HttpGet("allbylang")]
|
|
public async Task<IActionResult> 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<IActionResult>(Ok(result));
|
|
}
|
|
else
|
|
{
|
|
return NoContent();
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" });
|
|
}
|
|
}
|
|
[HttpGet("dailyquote")]
|
|
public async Task<IActionResult> 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<IActionResult>(Ok(result));
|
|
}
|
|
else
|
|
{
|
|
return NoContent();
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" });
|
|
}
|
|
}
|
|
[HttpGet("invalid")]
|
|
public async Task<IActionResult> 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<IActionResult>(Ok(result));
|
|
}
|
|
else
|
|
{
|
|
return NoContent();
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" });
|
|
}
|
|
}
|
|
[HttpGet("suggest")]
|
|
public async Task<IActionResult> 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<IActionResult>(Ok(result));
|
|
}
|
|
else
|
|
{
|
|
return NoContent();
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" });
|
|
}
|
|
}
|
|
[HttpGet("searchByCharacter")]
|
|
public async Task<IActionResult> 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<IActionResult>(Ok(result));
|
|
}
|
|
else
|
|
{
|
|
return NoContent();
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" });
|
|
}
|
|
}
|
|
[HttpGet("searchByCharac")]
|
|
public async Task<IActionResult> 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<IActionResult>(Ok(result));
|
|
}
|
|
else
|
|
{
|
|
return NoContent();
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" });
|
|
}
|
|
}
|
|
[HttpGet("searchByContent")]
|
|
public async Task<IActionResult> 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<IActionResult>(Ok(result));
|
|
}
|
|
else
|
|
{
|
|
return NoContent();
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" });
|
|
}
|
|
}
|
|
|
|
//===================================== ROUTE POST =====================================
|
|
|
|
[HttpPost]
|
|
public async Task<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> DeleteQuote([FromQuery] int idQuote)
|
|
{
|
|
try
|
|
{
|
|
var result = _quote.RemoveQuote(idQuote);
|
|
|
|
if (result.IsCompletedSuccessfully)
|
|
{
|
|
return await Task.FromResult<IActionResult>(Ok(result));
|
|
}
|
|
else
|
|
{
|
|
return NotFound();
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" });
|
|
}
|
|
}
|
|
}
|
|
}
|