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

@@ -179,7 +179,7 @@ public class PostController(
if (post == null) return NotFound();
var fileRecord = await fileService.GetFileById(fileId);
if (fileRecord == null) return NotFound();
if (fileRecord == null || post.File.Id != fileRecord.Id) return NotFound();
// If not the resource owner
var userId = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value;
@@ -264,7 +264,7 @@ public class PostController(
if (post == null) return NotFound();
var fileRecord = await fileService.GetFileById(fileId);
if (fileRecord == null) return NotFound();
if (fileRecord == null || post.File.Id != fileRecord.Id) return NotFound();
// Ensure the file has been successfully uploaded and finalized
if (string.IsNullOrEmpty(fileRecord.FilePath) || fileRecord.FinishedDate == null)
@@ -466,7 +466,7 @@ public class PostController(
if (entry == null) return NotFound();
var comment = await commentService.GetById(commentId);
if (comment == null) return NotFound();
if (comment == null || entry.Id != comment.OriginalPost.Id) return NotFound();
return Ok(CommentDto.FromComment(comment));
}
@@ -491,7 +491,7 @@ public class PostController(
var post = await postService.GetById(postId);
if (post == null) return NotFound();
var list = await commentService.GetAll(pageNumber);
var list = await commentService.GetAll(postId, pageNumber);
var newItems = list.Items.Select(CommentDto.FromComment).ToList();
return Ok(new PagedList<CommentDto>(newItems, list.CurrentPage, list.PageSize, list.TotalCount));
}
@@ -519,7 +519,7 @@ public class PostController(
if (post == null) return NotFound();
var comment = await commentService.GetById(commentId);
if (comment == null) return NotFound();
if (comment == null || post.Id != comment.OriginalPost.Id) return NotFound();
var userId = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value;
var isAdmin = HttpContext.User.IsInRole(UserRoles.Admin);
@@ -556,8 +556,8 @@ public class PostController(
if (post == null) return NotFound();
var comment = await commentService.GetById(commentId);
if (comment == null) return NotFound();
if (comment == null || post.Id != comment.OriginalPost.Id) return NotFound();
var userId = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value;
var isAdmin = HttpContext.User.IsInRole(UserRoles.Admin);

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)