后端修复获取评论失败的bug

This commit is contained in:
2025-06-23 09:15:10 +08:00
parent 5fdd69acb8
commit b75765a6d8
2 changed files with 4 additions and 9 deletions

View File

@@ -77,7 +77,7 @@ export class BlogController {
throw new BadRequestException('文章不存在或未公开'); throw new BadRequestException('文章不存在或未公开');
} }
return await this.blogService.getComments(id); return await this.blogService.getComments(blog);
} }
// 该接口允许匿名评论但仍需验证userId合法性 // 该接口允许匿名评论但仍需验证userId合法性

View File

@@ -13,7 +13,7 @@ export class BlogService {
private readonly blogRepository: Repository<Blog>, private readonly blogRepository: Repository<Blog>,
@InjectRepository(BlogComment) @InjectRepository(BlogComment)
private readonly blogCommentRepository: Repository<BlogComment>, private readonly blogCommentRepository: Repository<BlogComment>,
) {} ) { }
async list( async list(
option: { option: {
@@ -95,14 +95,9 @@ export class BlogService {
await this.blogRepository.increment({ id }, 'viewCount', 1); await this.blogRepository.increment({ id }, 'viewCount', 1);
} }
async getComments(blogId: string) { async getComments(blog: Blog) {
const blog = await this.findById(blogId);
if (!blog) {
throw new Error('文章不存在');
}
return this.blogCommentRepository.find({ return this.blogCommentRepository.find({
where: { blog }, where: { blog: { id: blog.id } },
relations: ['user'], relations: ['user'],
order: { order: {
createdAt: 'DESC', createdAt: 'DESC',