From ad8d0023b6a74b022138399c1b5c033f00e25af1 Mon Sep 17 00:00:00 2001 From: Leni BEAULATON Date: Mon, 31 Mar 2025 17:14:07 +0200 Subject: [PATCH] Test des commentaires dans api --- WF_EF_Api/Contextlib/DbCommentaryManager.cs | 20 ++-- WF_EF_Api/ServicesApi/CommentaryService.cs | 75 +++++++++++++ WF_EF_Api/ServicesApi/QuoteService.cs | 16 ++- WF_EF_Api/Shared/IComentaryService.cs | 22 ++-- .../Controllers/CommentariesController.cs | 102 ++++++++++++++++++ .../WfApi/Controllers/UsersController.cs | 6 +- WF_EF_Api/WfApi/Program.cs | 3 +- 7 files changed, 214 insertions(+), 30 deletions(-) create mode 100644 WF_EF_Api/ServicesApi/CommentaryService.cs create mode 100644 WF_EF_Api/WfApi/Controllers/CommentariesController.cs diff --git a/WF_EF_Api/Contextlib/DbCommentaryManager.cs b/WF_EF_Api/Contextlib/DbCommentaryManager.cs index c866245..b18ae99 100644 --- a/WF_EF_Api/Contextlib/DbCommentaryManager.cs +++ b/WF_EF_Api/Contextlib/DbCommentaryManager.cs @@ -10,7 +10,7 @@ using System.Threading.Tasks; namespace Contextlib { - public class DbCommentaryManager : ICommentService + public class DbCommentaryManager : ICommentaryService { private WTFContext _context; private GenericRepository _repo; @@ -44,7 +44,7 @@ namespace Contextlib /// The ID of the quote whose comments need to be deleted. /// A task representing the asynchronous operation. /// Thrown when no comments are found for the provided quote ID. - public async Task DeleteCommentForQuote(int quoteId) + public async Task DeleteCommentaryForQuote(int quoteId) { var comments = await _context.comments.Where(x => x.IdQuote == quoteId).ToListAsync(); if (!comments.Any()) @@ -62,7 +62,7 @@ namespace Contextlib /// The ID of the user whose comments need to be deleted. /// A task representing the asynchronous operation. /// Thrown when no comments are found for the provided user ID. - public async Task DeleteCommentForUser(int userId) + public async Task DeleteCommentaryForUser(int userId) { var comments = await _context.comments.Include(c => c.User).Where(x => x.IdUser == userId).ToListAsync(); if (!comments.Any()) @@ -74,13 +74,13 @@ namespace Contextlib await _context.SaveChangesAsync(); } - public async Task> GetAllComment() + public async Task> GetAllCommentary() { var comments = await _context.comments.Include(c => c.User).ToListAsync(); return new PaginationResult(comments.Count, 0, comments.Count, comments); } - public async Task GetCommentById(int id) + public async Task GetCommentaryById(int id) { var comment = await _context.comments.Include(c => c.User).Where(x => x.Id == id).FirstOrDefaultAsync(); if(comment == null) @@ -90,7 +90,7 @@ namespace Contextlib return comment; } - public async Task> GetCommentByQuote(int quoteId, int index, int pageSize) + public async Task> GetCommentaryByQuote(int quoteId, int index, int pageSize) { var comments = await _context.comments.Include(c => c.User).Where(x => x.IdQuote == quoteId).ToListAsync(); if (!comments.Any()) @@ -112,7 +112,7 @@ namespace Contextlib return new PaginationResult(comments.Count, index, pageSize, comments.Skip(index * pageSize).Take(pageSize).ToList()); } - public async Task> GetCommentByUser(int userId, int index, int pageSize) + public async Task> GetCommentaryByUser(int userId, int index, int pageSize) { var comments = await _context.comments.Include(c => c.User).Where(x => x.IdUser == userId).ToListAsync(); if (!comments.Any()) @@ -134,7 +134,7 @@ namespace Contextlib return new PaginationResult(comments.Count, index, pageSize, comments.Skip(index * pageSize).Take(pageSize).ToList()); } - public async Task LastCommentId() + public async Task LastCommentaryId() { var last = await _context.comments.OrderByDescending(x => x.Id).FirstOrDefaultAsync(); if(last == null) @@ -144,13 +144,13 @@ namespace Contextlib return last.Id; } - public async Task RemoveComment(int id) + public async Task RemoveCommentary(int id) { _repo.Delete(id); await _context.SaveChangesAsync(); } - public async Task UpdateComment(int id, Commentary comment) + public async Task UpdateCommentary(int id, Commentary comment) { var modif = false; var com = await _context.comments.Where(x => x.Id == id).FirstOrDefaultAsync(); diff --git a/WF_EF_Api/ServicesApi/CommentaryService.cs b/WF_EF_Api/ServicesApi/CommentaryService.cs new file mode 100644 index 0000000..211f6a4 --- /dev/null +++ b/WF_EF_Api/ServicesApi/CommentaryService.cs @@ -0,0 +1,75 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using DTO; +using Entity; +using Shared; +using Dto2Entities; + +namespace ServicesApi +{ + public class CommentaryService : ICommentaryService + { + private ICommentaryService commentaryService; + + public CommentaryService(ICommentaryService commentary) + { + commentaryService = commentary; + } + + public async Task AddComment(CommentaryDTO commentary) + { + await commentaryService.AddComment(commentary.ToEntity()); + } + + public async Task DeleteCommentaryForQuote(int quoteId) + { + await commentaryService.DeleteCommentaryForQuote(quoteId); + } + + public async Task DeleteCommentaryForUser(int userId) + { + await commentaryService.DeleteCommentaryForUser(userId); + } + + public async Task> GetAllCommentary() + { + var commentaries = commentaryService.GetAllCommentary().Result.items; + return new PaginationResult(commentaries.Count(), 0, 10, commentaries.ToDto()); + } + + public async Task GetCommentaryById(int id) + { + return commentaryService.GetCommentaryById(id).Result.ToDto(); + } + + public async Task> GetCommentaryByQuote(int quoteId, int index, int pageSize) + { + var commentaries = commentaryService.GetCommentaryByQuote(quoteId,index,pageSize).Result.items; + return new PaginationResult(commentaries.Count(), 0, 10, commentaries.ToDto()); + } + + public async Task> GetCommentaryByUser(int userId, int index, int pageSize) + { + var commentaries = commentaryService.GetCommentaryByUser(userId, index,pageSize).Result.items; + return new PaginationResult(commentaries.Count(), 0, 10, commentaries.ToDto()); + } + + public async Task LastCommentaryId() + { + return await commentaryService.LastCommentaryId(); + } + + public async Task RemoveCommentary(int id) + { + await commentaryService.RemoveCommentary(id); + } + + public async Task UpdateCommentary(int id, CommentaryDTO commentary) + { + await commentaryService.UpdateCommentary(id,commentary.ToEntity()); + } + } +} diff --git a/WF_EF_Api/ServicesApi/QuoteService.cs b/WF_EF_Api/ServicesApi/QuoteService.cs index 2a76c86..fc1be81 100644 --- a/WF_EF_Api/ServicesApi/QuoteService.cs +++ b/WF_EF_Api/ServicesApi/QuoteService.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reflection.Metadata; using System.Text; using System.Threading.Tasks; using DTO; @@ -32,7 +33,8 @@ namespace ServicesApi public async Task> GetAllQuoteLang(int index, int pageSize, int lang) { - throw new NotImplementedException(); + var quotes = quoteService.GetAllQuoteLang(index, pageSize,lang).Result.items; + return new PaginationResult(quotes.Count(), 0, 10, quotes.ToDto()); } public async Task GetDailyQuote(DateOnly date, int lang) @@ -82,7 +84,8 @@ namespace ServicesApi public async Task> GetValidQuote(int index, int pageSize) { - throw new NotImplementedException(); + var quotes = quoteService.GetValidQuote(index, pageSize).Result.items; + return new PaginationResult(quotes.Count(), 0, 10, quotes.ToDto()); } public async Task RemoveQuote(int quoteId) @@ -92,17 +95,20 @@ namespace ServicesApi public async Task> SearchByCharacter(string character, int index, int pageSize, int lang) { - throw new NotImplementedException(); + var quotes = quoteService.SearchByCharacter(character, index, pageSize, lang).Result.items; + return new PaginationResult(quotes.Count(), 0, 10, quotes.ToDto()); } public async Task> SearchByContent(string content, int index, int pageSize, int lang) { - throw new NotImplementedException(); + var quotes = quoteService.SearchByContent(content, index, pageSize, lang).Result.items; + return new PaginationResult(quotes.Count(), 0, 10, quotes.ToDto()); } public async Task> SearchBySource(string source, int index, int pageSize, int lang) { - throw new NotImplementedException(); + var quotes = quoteService.SearchBySource(source, index, pageSize, lang).Result.items; + return new PaginationResult(quotes.Count(), 0, 10, quotes.ToDto()); } public async Task UpdateQuote(int quoteId, QuoteDTO quote) diff --git a/WF_EF_Api/Shared/IComentaryService.cs b/WF_EF_Api/Shared/IComentaryService.cs index d5c4fc6..b546360 100644 --- a/WF_EF_Api/Shared/IComentaryService.cs +++ b/WF_EF_Api/Shared/IComentaryService.cs @@ -6,49 +6,49 @@ using System.Threading.Tasks; namespace Shared { - public interface ICommentService + public interface ICommentaryService { // Retrieves a comment by its unique identifier (id). // 'id' is the unique identifier of the comment. - Task GetCommentById(int id); + Task GetCommentaryById(int id); // Retrieves comments related to a specific quote, with pagination. // 'quoteId' is the unique identifier of the quote. // 'index' is the page number (for pagination). // 'pageSize' is the number of comments per page. - Task> GetCommentByQuote(int quoteId, int index, int pageSize); + Task> GetCommentaryByQuote(int quoteId, int index, int pageSize); // Retrieves all comments, with pagination support. // This returns a list of all comments. - Task> GetAllComment(); + Task> GetAllCommentary(); // Retrieves comments made by a specific user, with pagination. // 'userId' is the unique identifier of the user. // 'index' is the page number (for pagination). // 'pageSize' is the number of comments per page. - Task> GetCommentByUser(int userId, int index, int pageSize); + Task> GetCommentaryByUser(int userId, int index, int pageSize); // Adds a new commenT. // 'comment' is the comment object that will be added. - Task AddComment(TComment comment); + Task AddComment(TComment commentary); // Updates an existing comment identified by 'id'. // 'id' is the unique identifier of the comment, and 'comment' contains the updated comment data. - Task UpdateComment(int id, TComment comment); + Task UpdateCommentary(int id, TComment comment); // Removes a comment based on its unique identifier ('id'). // 'id' is the unique identifier of the comment to be removed. - Task RemoveComment(int id); + Task RemoveCommentary(int id); // Deletes all comments related to a specific quote. // 'quoteId' is the unique identifier of the quote for which comments will be deleted. - Task DeleteCommentForQuote(int quoteId); + Task DeleteCommentaryForQuote(int quoteId); // Deletes all comments made by a specific user. // 'userId' is the unique identifier of the user whose comments will be deleted. - Task DeleteCommentForUser(int userId); + Task DeleteCommentaryForUser(int userId); // Retrieves the last comment ID. - Task LastCommentId(); + Task LastCommentaryId(); } } diff --git a/WF_EF_Api/WfApi/Controllers/CommentariesController.cs b/WF_EF_Api/WfApi/Controllers/CommentariesController.cs new file mode 100644 index 0000000..737acea --- /dev/null +++ b/WF_EF_Api/WfApi/Controllers/CommentariesController.cs @@ -0,0 +1,102 @@ +using System.Net; +using DTO; +using Entity; +using Microsoft.AspNetCore.Mvc; +using Shared; + +namespace WfApi.Controllers +{ + [ApiController] + [Route("api/v1/commentary")] //Version API + public class CommentariesController : ControllerBase + { + private readonly ICommentaryService _commentary; + + private readonly ILogger _logger; + public CommentariesController(ICommentaryService commentaryService, ILogger logger) + { + _commentary = commentaryService; + _logger = logger; + } + + + + [HttpGet("{id}")] // Indiquer que l'id est dans l'URL + public async Task GetCommentary(int id, int index = 0, int count = 5) + { + try + { + var result = _commentary.GetCommentaryByQuote(id, index, count); + + if (result.IsCompletedSuccessfully) + { + 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 CreateCommentary([FromBody] CommentaryDTO newCommentary) + { + try + { + if (newCommentary == null) + { + return BadRequest(new { message = "Comment data is required." }); + } + + var existingPlayer = _commentary.GetCommentaryById(newCommentary.Id).Result; + if (existingPlayer != null) + { + return Conflict(new { message = "A comment with this ID already exists." }); + } + + _commentary.AddComment(newCommentary); + + return CreatedAtAction(nameof(GetCommentary), new { id = newCommentary.Id }, newCommentary); + } + 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 DeleteCommentary([FromQuery] int id) + { + try + { + + var existingCommentary = _commentary.GetCommentaryById(id).Result; + if (existingCommentary == null) + { + return NotFound(new { message = "Commentary not found." }); + } + + _commentary.RemoveCommentary(existingCommentary.Id); + + return Ok(new { message = $"Commentary {id} deleted successfully." }); + } + catch (Exception) + { + return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal server error." }); + } + } + } +} diff --git a/WF_EF_Api/WfApi/Controllers/UsersController.cs b/WF_EF_Api/WfApi/Controllers/UsersController.cs index 400c835..1950bbd 100644 --- a/WF_EF_Api/WfApi/Controllers/UsersController.cs +++ b/WF_EF_Api/WfApi/Controllers/UsersController.cs @@ -573,13 +573,13 @@ namespace WfApi.Controllers { if (newUser == null) { - return BadRequest(new { message = "Les données du joueur sont requises." }); - } + return BadRequest(new { message = "User data is required." }); + } var existingPlayer = _user.GetUserById(newUser.Id).Result; if (existingPlayer != null) { - return Conflict(new { message = "Un utilisateur avec cet ID existe déjà." }); + return Conflict(new { message = "A user with this ID already exists." }); } _user.AddUser(newUser); diff --git a/WF_EF_Api/WfApi/Program.cs b/WF_EF_Api/WfApi/Program.cs index 54d221a..6e4cd69 100644 --- a/WF_EF_Api/WfApi/Program.cs +++ b/WF_EF_Api/WfApi/Program.cs @@ -9,6 +9,7 @@ var builder = WebApplication.CreateBuilder(args); //API builder.Services.AddScoped, UserService>(); builder.Services.AddScoped, QuoteService>(); +builder.Services.AddScoped, CommentaryService>(); //EF @@ -16,7 +17,7 @@ builder.Services.AddScoped(); builder.Services.AddScoped, DbUsersManager>(); builder.Services.AddScoped, DbQuoteManager>(); //builder.Services.AddScoped, DbCharacterManager>(); -//builder.Services.AddScoped, DbCommentaryManager>(); +builder.Services.AddScoped, DbCommentaryManager>(); //... // Add services to the container.