初步完成评论

This commit is contained in:
2025-06-07 03:21:27 +08:00
parent c872b55083
commit 3821ef6657
12 changed files with 208 additions and 4 deletions

View File

@@ -1,4 +1,4 @@
import { BadRequestException, Controller, Get, Param, ParseUUIDPipe } from '@nestjs/common';
import { BadRequestException, Body, Controller, Get, Param, ParseUUIDPipe, Post } from '@nestjs/common';
import { BlogService } from './blog.service';
@Controller('blog')
@@ -31,4 +31,31 @@ export class BlogController {
content: blogContent,
};
}
@Get(':id/comments')
async getBlogComments(
@Param('id', new ParseUUIDPipe({ version: '4' })) id: string,
) {
const blog = await this.blogService.findById(id);
if (!blog) throw new BadRequestException('文章不存在');
return await this.blogService.getComments(id);
}
// TODO鉴权该接口允许匿名评论但仍需验证userId合法性
@Post(':id/comment')
async createBlogComment(
@Param('id', new ParseUUIDPipe({ version: '4' })) id: string,
@Body() commentData: { content: string },
) {
const blog = await this.blogService.findById(id);
if (!blog) throw new BadRequestException('文章不存在');
const comment = {
...commentData,
blogId: id,
};
return await this.blogService.createComment(comment);
}
}