Test des commentaires dans api
continuous-integration/drone/push Build is failing Details

pull/6/head
Leni BEAULATON 3 weeks ago
parent 4b1edf0e16
commit ad8d0023b6

@ -10,7 +10,7 @@ using System.Threading.Tasks;
namespace Contextlib
{
public class DbCommentaryManager : ICommentService<Commentary>
public class DbCommentaryManager : ICommentaryService<Commentary>
{
private WTFContext _context;
private GenericRepository<Commentary> _repo;
@ -44,7 +44,7 @@ namespace Contextlib
/// <param name="quoteId">The ID of the quote whose comments need to be deleted.</param>
/// <returns>A task representing the asynchronous operation.</returns>
/// <exception cref="KeyNotFoundException">Thrown when no comments are found for the provided quote ID.</exception>
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
/// <param name="userId">The ID of the user whose comments need to be deleted.</param>
/// <returns>A task representing the asynchronous operation.</returns>
/// <exception cref="KeyNotFoundException">Thrown when no comments are found for the provided user ID.</exception>
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<PaginationResult<Commentary>> GetAllComment()
public async Task<PaginationResult<Commentary>> GetAllCommentary()
{
var comments = await _context.comments.Include(c => c.User).ToListAsync();
return new PaginationResult<Commentary>(comments.Count, 0, comments.Count, comments);
}
public async Task<Commentary> GetCommentById(int id)
public async Task<Commentary> 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<PaginationResult<Commentary>> GetCommentByQuote(int quoteId, int index, int pageSize)
public async Task<PaginationResult<Commentary>> 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<Commentary>(comments.Count, index, pageSize, comments.Skip(index * pageSize).Take(pageSize).ToList());
}
public async Task<PaginationResult<Commentary>> GetCommentByUser(int userId, int index, int pageSize)
public async Task<PaginationResult<Commentary>> 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<Commentary>(comments.Count, index, pageSize, comments.Skip(index * pageSize).Take(pageSize).ToList());
}
public async Task<int> LastCommentId()
public async Task<int> 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();

@ -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<CommentaryDTO>
{
private ICommentaryService<Commentary> commentaryService;
public CommentaryService(ICommentaryService<Commentary> 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<PaginationResult<CommentaryDTO>> GetAllCommentary()
{
var commentaries = commentaryService.GetAllCommentary().Result.items;
return new PaginationResult<CommentaryDTO>(commentaries.Count(), 0, 10, commentaries.ToDto());
}
public async Task<CommentaryDTO> GetCommentaryById(int id)
{
return commentaryService.GetCommentaryById(id).Result.ToDto();
}
public async Task<PaginationResult<CommentaryDTO>> GetCommentaryByQuote(int quoteId, int index, int pageSize)
{
var commentaries = commentaryService.GetCommentaryByQuote(quoteId,index,pageSize).Result.items;
return new PaginationResult<CommentaryDTO>(commentaries.Count(), 0, 10, commentaries.ToDto());
}
public async Task<PaginationResult<CommentaryDTO>> GetCommentaryByUser(int userId, int index, int pageSize)
{
var commentaries = commentaryService.GetCommentaryByUser(userId, index,pageSize).Result.items;
return new PaginationResult<CommentaryDTO>(commentaries.Count(), 0, 10, commentaries.ToDto());
}
public async Task<int> 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());
}
}
}

@ -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<PaginationResult<QuoteDTO>> GetAllQuoteLang(int index, int pageSize, int lang)
{
throw new NotImplementedException();
var quotes = quoteService.GetAllQuoteLang(index, pageSize,lang).Result.items;
return new PaginationResult<QuoteDTO>(quotes.Count(), 0, 10, quotes.ToDto());
}
public async Task<QuoteDTO> GetDailyQuote(DateOnly date, int lang)
@ -82,7 +84,8 @@ namespace ServicesApi
public async Task<PaginationResult<QuoteDTO>> GetValidQuote(int index, int pageSize)
{
throw new NotImplementedException();
var quotes = quoteService.GetValidQuote(index, pageSize).Result.items;
return new PaginationResult<QuoteDTO>(quotes.Count(), 0, 10, quotes.ToDto());
}
public async Task RemoveQuote(int quoteId)
@ -92,17 +95,20 @@ namespace ServicesApi
public async Task<PaginationResult<QuoteDTO>> 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<QuoteDTO>(quotes.Count(), 0, 10, quotes.ToDto());
}
public async Task<PaginationResult<QuoteDTO>> 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<QuoteDTO>(quotes.Count(), 0, 10, quotes.ToDto());
}
public async Task<PaginationResult<QuoteDTO>> 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<QuoteDTO>(quotes.Count(), 0, 10, quotes.ToDto());
}
public async Task UpdateQuote(int quoteId, QuoteDTO quote)

@ -6,49 +6,49 @@ using System.Threading.Tasks;
namespace Shared
{
public interface ICommentService<TComment>
public interface ICommentaryService<TComment>
{
// Retrieves a comment by its unique identifier (id).
// 'id' is the unique identifier of the comment.
Task<TComment> GetCommentById(int id);
Task<TComment> 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<PaginationResult<TComment>> GetCommentByQuote(int quoteId, int index, int pageSize);
Task<PaginationResult<TComment>> GetCommentaryByQuote(int quoteId, int index, int pageSize);
// Retrieves all comments, with pagination support.
// This returns a list of all comments.
Task<PaginationResult<TComment>> GetAllComment();
Task<PaginationResult<TComment>> 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<PaginationResult<TComment>> GetCommentByUser(int userId, int index, int pageSize);
Task<PaginationResult<TComment>> 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<int> LastCommentId();
Task<int> LastCommentaryId();
}
}

@ -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<CommentaryDTO> _commentary;
private readonly ILogger<CommentariesController> _logger;
public CommentariesController(ICommentaryService<CommentaryDTO> commentaryService, ILogger<CommentariesController> logger)
{
_commentary = commentaryService;
_logger = logger;
}
[HttpGet("{id}")] // Indiquer que l'id est dans l'URL
public async Task<IActionResult> GetCommentary(int id, int index = 0, int count = 5)
{
try
{
var result = _commentary.GetCommentaryByQuote(id, index, count);
if (result.IsCompletedSuccessfully)
{
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> 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<IActionResult> 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." });
}
}
}
}

@ -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);

@ -9,6 +9,7 @@ var builder = WebApplication.CreateBuilder(args);
//API
builder.Services.AddScoped<IUserService<UserDTO>, UserService>();
builder.Services.AddScoped<IQuoteService<QuoteDTO>, QuoteService>();
builder.Services.AddScoped<ICommentaryService<CommentaryDTO>, CommentaryService>();
//EF
@ -16,7 +17,7 @@ builder.Services.AddScoped<WTFContext, StubWTFContext>();
builder.Services.AddScoped<IUserService<Users>, DbUsersManager>();
builder.Services.AddScoped<IQuoteService<Quote>, DbQuoteManager>();
//builder.Services.AddScoped<ICharacterService<Character>, DbCharacterManager>();
//builder.Services.AddScoped<ICommentaryService<Commentary>, DbCommentaryManager>();
builder.Services.AddScoped<ICommentaryService<Commentary>, DbCommentaryManager>();
//...
// Add services to the container.

Loading…
Cancel
Save