优化博客与博客评论关系

This commit is contained in:
2025-06-19 16:03:27 +08:00
parent bf3b2f7a94
commit bb9fa3bcaa
3 changed files with 16 additions and 9 deletions

View File

@@ -94,10 +94,10 @@ export class BlogController {
const comment = {
...commentData,
blogId: id,
user: user,
ip: ip,
address: address,
blog,
user,
ip,
address,
};
return await this.blogService.createComment(comment);

View File

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

View File

@@ -8,6 +8,7 @@ import {
ManyToOne,
PrimaryGeneratedColumn,
} from 'typeorm';
import { Blog } from './Blog.entity';
@Entity()
export class BlogComment {
@@ -33,8 +34,9 @@ export class BlogComment {
@JoinColumn({ name: 'userId' })
user: User | null;
@Column({ type: 'uuid', nullable: true })
blogId: string | null;
@ManyToOne(() => Blog)
@JoinColumn({ name: 'blogId' })
blog: Blog | null;
@Column({ type: 'uuid', nullable: true })
parentId: string | null;