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.
WF-PmAPI/WF_EF_Api/WfApi/Controllers/QuotesController.cs

362 lines
13 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
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> 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<IActionResult> GetAllQuote(int index = 0, int count = 10)
{
try
{
var result = await _quote.GetSomeQuote(index, count);
if (result != null)
{
return await Task.FromResult<IActionResult>(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<IActionResult> 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<IActionResult>(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<IActionResult> 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<IActionResult>(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<IActionResult> 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<IActionResult>(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<IActionResult> 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<IActionResult>(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<IActionResult> 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<IActionResult>(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<IActionResult> 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<IActionResult>(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<IActionResult> 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<IActionResult>(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<IActionResult> 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<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")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
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")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> DeleteQuote([FromQuery] int idQuote)
{
try {
try
{
_quote.RemoveQuote(idQuote).Wait();
return await Task.FromResult<IActionResult>(Ok());
}
catch (KeyNotFoundException e)
{
return StatusCode((int)HttpStatusCode.NotFound, e);
}
}
catch (Exception)
{
return StatusCode((int) HttpStatusCode.InternalServerError, new { message = "Erreur interne du serveur." });
}
}
}
}