|
|
@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
using System.Net;
|
|
|
|
|
|
|
|
using DTO;
|
|
|
|
|
|
|
|
using Entity;
|
|
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
using ServicesApi;
|
|
|
|
|
|
|
|
using Shared;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace WfApi.Controllers
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
[ApiController]
|
|
|
|
|
|
|
|
[Route("api/v1/favorite")] //Version API
|
|
|
|
|
|
|
|
public class FavoriteControleur : ControllerBase
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
private readonly IFavoriteService<QuoteDTO> _favorite;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private readonly ILogger<FavoriteControleur> _logger;
|
|
|
|
|
|
|
|
public FavoriteControleur(IFavoriteService<QuoteDTO> favoriteService, ILogger<FavoriteControleur> logger)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_favorite = favoriteService;
|
|
|
|
|
|
|
|
_logger = logger;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet("{id}")] // Indiquer que l'id est dans l'URL
|
|
|
|
|
|
|
|
public async Task<IActionResult> GetFavoriteByIdUser(int id, int index = 0, int count = 10)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var result = await _favorite.GetFavoriteByIdUser(id, 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" });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status201Created)]
|
|
|
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
|
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status409Conflict)]
|
|
|
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
|
|
|
|
|
|
public async Task<IActionResult> CreateFavorite(int idUser , int idQuote)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var existingFavorite = _favorite.GetFavorite(idUser,idQuote).Result;
|
|
|
|
|
|
|
|
if (existingFavorite != null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return Conflict(new { message = "A favorite with this ID already exists." });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await _favorite.AddFavorite(idUser, idQuote);
|
|
|
|
|
|
|
|
var fav = new Favorite();
|
|
|
|
|
|
|
|
fav.IdQuote = idQuote;
|
|
|
|
|
|
|
|
fav.IdUsers = idUser;
|
|
|
|
|
|
|
|
return CreatedAtAction(nameof(GetFavoriteByIdUser), new { id = idUser }, fav);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Erreur interne du serveur." });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[HttpDelete] // /api/v1/commentary?id=51
|
|
|
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
|
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
|
|
|
|
|
|
public async Task<IActionResult> DeleteFavorite([FromQuery] int idUser, [FromQuery] int idQuote)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var existingFavorite = await _favorite.GetFavorite(idUser, idQuote);
|
|
|
|
|
|
|
|
if (existingFavorite == null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return NotFound(new { message = "Commentary not found." });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await _favorite.RemoveFavorite(idUser, idQuote);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return Ok(new { message = $"Favorite from user {idUser} and quote {idQuote} deleted successfully." });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal server error." });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpDelete] // /api/v1/commentary?id=51
|
|
|
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
|
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
|
|
|
|
|
|
public async Task<IActionResult> DeleteFavorite([FromQuery] int idUser, [FromQuery] int idQuote)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var existingFavorite = await _favorite.GetFavorite(idUser, idQuote);
|
|
|
|
|
|
|
|
if (existingFavorite == null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return NotFound(new { message = "Commentary not found." });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await _favorite.RemoveFavorite(idUser, idQuote);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return Ok(new { message = $"Favorite from user {idUser} and quote {idQuote} deleted successfully." });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal server error." });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|