优化博客与博客评论关系
This commit is contained in:
@@ -94,10 +94,10 @@ export class BlogController {
|
|||||||
|
|
||||||
const comment = {
|
const comment = {
|
||||||
...commentData,
|
...commentData,
|
||||||
blogId: id,
|
blog,
|
||||||
user: user,
|
user,
|
||||||
ip: ip,
|
ip,
|
||||||
address: address,
|
address,
|
||||||
};
|
};
|
||||||
|
|
||||||
return await this.blogService.createComment(comment);
|
return await this.blogService.createComment(comment);
|
||||||
|
|||||||
@@ -46,9 +46,14 @@ export class BlogService {
|
|||||||
await this.blogRepository.increment({ id }, 'viewCount', 1);
|
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({
|
return this.blogCommentRepository.find({
|
||||||
where: { blogId: id },
|
where: { blog },
|
||||||
relations: ['user'],
|
relations: ['user'],
|
||||||
order: {
|
order: {
|
||||||
createdAt: 'DESC',
|
createdAt: 'DESC',
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import {
|
|||||||
ManyToOne,
|
ManyToOne,
|
||||||
PrimaryGeneratedColumn,
|
PrimaryGeneratedColumn,
|
||||||
} from 'typeorm';
|
} from 'typeorm';
|
||||||
|
import { Blog } from './Blog.entity';
|
||||||
|
|
||||||
@Entity()
|
@Entity()
|
||||||
export class BlogComment {
|
export class BlogComment {
|
||||||
@@ -33,8 +34,9 @@ export class BlogComment {
|
|||||||
@JoinColumn({ name: 'userId' })
|
@JoinColumn({ name: 'userId' })
|
||||||
user: User | null;
|
user: User | null;
|
||||||
|
|
||||||
@Column({ type: 'uuid', nullable: true })
|
@ManyToOne(() => Blog)
|
||||||
blogId: string | null;
|
@JoinColumn({ name: 'blogId' })
|
||||||
|
blog: Blog | null;
|
||||||
|
|
||||||
@Column({ type: 'uuid', nullable: true })
|
@Column({ type: 'uuid', nullable: true })
|
||||||
parentId: string | null;
|
parentId: string | null;
|
||||||
|
|||||||
Reference in New Issue
Block a user