From ff3a1a0efa367c9ca0de4506d9fc70daca269ec4 Mon Sep 17 00:00:00 2001 From: kekentin Date: Wed, 2 Apr 2025 16:04:21 +0200 Subject: [PATCH] mise a jour route --- WF_EF_Api/Contextlib/DbSourceManager.cs | 2 +- WF_EF_Api/Shared/IFavoriteService.cs | 6 +- .../Migrations/StubWTFContextModelSnapshot.cs | 28 ++-- .../WfApi/Controllers/FavoriteControleur.cs | 125 ++++++++++++++++++ WF_EF_Api/WfApi/Program.cs | 4 +- 5 files changed, 148 insertions(+), 17 deletions(-) create mode 100644 WF_EF_Api/WfApi/Controllers/FavoriteControleur.cs diff --git a/WF_EF_Api/Contextlib/DbSourceManager.cs b/WF_EF_Api/Contextlib/DbSourceManager.cs index c40f04e..6f470f7 100644 --- a/WF_EF_Api/Contextlib/DbSourceManager.cs +++ b/WF_EF_Api/Contextlib/DbSourceManager.cs @@ -8,7 +8,7 @@ using Shared; namespace Contextlib { - internal class DbSourceManager : ISourceService + public class DbSourceManager : ISourceService { private WTFContext _context; private GenericRepository _repo; diff --git a/WF_EF_Api/Shared/IFavoriteService.cs b/WF_EF_Api/Shared/IFavoriteService.cs index 3b2de7f..08a32de 100644 --- a/WF_EF_Api/Shared/IFavoriteService.cs +++ b/WF_EF_Api/Shared/IFavoriteService.cs @@ -1,12 +1,13 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using System.Text; using System.Threading.Tasks; namespace Shared { - public interface IFavoriteService + public interface IFavoriteService { // Adds a quote to a user's list of favorites. // 'quoteid' is the unique identifier of the quote to be added to favorites. @@ -25,5 +26,8 @@ namespace Shared // Removes a specific quote from the favorite lists of all users. // 'quoteId' is the unique identifier of the quote to be removed from all users' favorites. Task RemoveAllFavoriteForQuote(int quoteId); + + Task> GetFavoriteByIdUser(int userId, int index, int count); + Task GetFavorite(int userId, int idQuote); } } diff --git a/WF_EF_Api/StubbedContextLib/Migrations/StubWTFContextModelSnapshot.cs b/WF_EF_Api/StubbedContextLib/Migrations/StubWTFContextModelSnapshot.cs index 37b2e66..08ef8a9 100644 --- a/WF_EF_Api/StubbedContextLib/Migrations/StubWTFContextModelSnapshot.cs +++ b/WF_EF_Api/StubbedContextLib/Migrations/StubWTFContextModelSnapshot.cs @@ -127,11 +127,11 @@ namespace StubbedContextLib.Migrations modelBuilder.Entity("Entity.Commentary", b => { - b.Property("IdUser") + b.Property("Id") + .ValueGeneratedOnAdd() .HasColumnType("int"); - b.Property("IdQuote") - .HasColumnType("int"); + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); b.Property("Comment") .IsRequired() @@ -142,34 +142,36 @@ namespace StubbedContextLib.Migrations .HasColumnType("date") .HasColumnName("DateCommentary"); - b.Property("Id") - .ValueGeneratedOnAdd() + b.Property("IdQuote") .HasColumnType("int"); - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + b.Property("IdUser") + .HasColumnType("int"); - b.HasKey("IdUser", "IdQuote"); + b.HasKey("Id"); b.HasIndex("IdQuote"); + b.HasIndex("IdUser"); + b.ToTable("comments"); b.HasData( new { - IdUser = 2, - IdQuote = 1, + Id = 1, Comment = "Ce film est le meilleur", DateCommentary = new DateTime(2025, 2, 3, 0, 0, 0, 0, DateTimeKind.Unspecified), - Id = 1 + IdQuote = 1, + IdUser = 2 }, new { - IdUser = 3, - IdQuote = 1, + Id = 2, Comment = "Very good", DateCommentary = new DateTime(2025, 3, 11, 0, 0, 0, 0, DateTimeKind.Unspecified), - Id = 2 + IdQuote = 1, + IdUser = 3 }); }); diff --git a/WF_EF_Api/WfApi/Controllers/FavoriteControleur.cs b/WF_EF_Api/WfApi/Controllers/FavoriteControleur.cs new file mode 100644 index 0000000..2529717 --- /dev/null +++ b/WF_EF_Api/WfApi/Controllers/FavoriteControleur.cs @@ -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 _favorite; + + private readonly ILogger _logger; + public FavoriteControleur(IFavoriteService favoriteService, ILogger logger) + { + _favorite = favoriteService; + _logger = logger; + } + + + + [HttpGet("{id}")] // Indiquer que l'id est dans l'URL + public async Task 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(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 = "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 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." }); + } + } + } +} diff --git a/WF_EF_Api/WfApi/Program.cs b/WF_EF_Api/WfApi/Program.cs index f4dd552..6d308be 100644 --- a/WF_EF_Api/WfApi/Program.cs +++ b/WF_EF_Api/WfApi/Program.cs @@ -15,7 +15,7 @@ builder.Services.AddScoped, QuoteService>(); builder.Services.AddScoped, CommentaryService>(); builder.Services.AddScoped, CharacterService>(); builder.Services.AddScoped, ImageService>(); -//builder.Services.AddScoped, SourceService>(); +builder.Services.AddScoped, SourceService>(); builder.Services.AddScoped, QuestionService>(); @@ -28,7 +28,7 @@ builder.Services.AddScoped, DbQuoteManager>(); builder.Services.AddScoped, DbCommentaryManager>(); builder.Services.AddScoped, DbCharacterManager>(); builder.Services.AddScoped, DbImagesManager>(); -//builder.Services.AddScoped, DbSourceManager>(); +builder.Services.AddScoped, DbSourceManager>(); builder.Services.AddScoped, DbQuestionManager>(); //...