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.
159 lines
5.8 KiB
159 lines
5.8 KiB
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 FavoriteController : ControllerBase
|
|
{
|
|
private readonly IFavoriteService<QuoteDTO> _favorite;
|
|
|
|
private readonly ILogger<FavoriteController> _logger;
|
|
public FavoriteController(IFavoriteService<QuoteDTO> favoriteService, ILogger<FavoriteController> logger)
|
|
{
|
|
_favorite = favoriteService;
|
|
_logger = logger;
|
|
}
|
|
|
|
|
|
|
|
[HttpGet("{idUser}")] // Indiquer que l'id est dans l'URL
|
|
public async Task<IActionResult> GetFavoriteByIdUser(int idUser, int index = 0, int count = 10)
|
|
{
|
|
try
|
|
{
|
|
var result = await _favorite.GetFavoriteByIdUser(idUser, 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()] // Indiquer que l'id est dans l'URL
|
|
public async Task<IActionResult> GetFavoriteByIdUser(int idUser , int idQuote)
|
|
{
|
|
try
|
|
{
|
|
var result = await _favorite.GetFavorite(idUser, idQuote);
|
|
|
|
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 = "Favorite 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("alluser")] // /api/v1/commentary?id=51
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> DeleteAllFavoriteForUser([FromQuery] int idUser)
|
|
{
|
|
try
|
|
{
|
|
|
|
await _favorite.RemoveAllFavoriteForUser(idUser);
|
|
|
|
return Ok(new { message = $"Favorite from user {idUser} deleted successfully." });
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal server error." });
|
|
}
|
|
}
|
|
[HttpDelete("allquote")] // /api/v1/commentary?id=51
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> DeleteAllFavoriteForQuote([FromQuery] int idQuote)
|
|
{
|
|
try
|
|
{
|
|
|
|
await _favorite.RemoveAllFavoriteForQuote(idQuote);
|
|
|
|
return Ok(new { message = $"Favorite from quote {idQuote} deleted successfully." });
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal server error." });
|
|
}
|
|
}
|
|
}
|
|
}
|