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 _favorite; private readonly ILogger _logger; public FavoriteController(IFavoriteService favoriteService, ILogger logger) { _favorite = favoriteService; _logger = logger; } [HttpGet("{idUser}")] // Indiquer que l'id est dans l'URL public async Task 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(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 GetFavoriteByIdUser(int idUser , int idQuote) { try { var result = await _favorite.GetFavorite(idUser, idQuote); if (result != null) { return await Task.FromResult(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 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 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 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 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." }); } } } }