Fix all comments were returned instead of post-specific comments

This commit is contained in:
2025-10-06 22:42:44 +03:00
parent b94392aaf2
commit 1b24f67983
3 changed files with 12 additions and 10 deletions

View File

@@ -9,7 +9,7 @@ public interface ICommentService
{
Task<Comment> Create(string text, User author, Post post);
Task<Comment?> GetById(int commentId);
Task<PagedList<Comment>> GetAll(int pageNumber = 1);
Task<PagedList<Comment>> GetAll(int postId, int pageNumber = 1);
Task<bool> Delete(Comment comment);
Task<Comment> Update(Comment comment);
}
@@ -40,10 +40,11 @@ public class CommentService(ImgBoardContext context): ICommentService
.FirstOrDefaultAsync();
}
public async Task<PagedList<Comment>> GetAll(int pageNumber = 1)
public async Task<PagedList<Comment>> GetAll(int postId, int pageNumber = 1)
{
var totalCount = await context.Comments.CountAsync();
var totalCount = await context.Comments.Where(c => c.OriginalPost.Id == postId).CountAsync();
var items = await context.Comments
.Where(c => c.OriginalPost.Id == postId)
.Skip((pageNumber - 1) * PageSize)
.Take(PageSize)
.Include(b => b.Author)