【发布时间】:2021-11-24 20:42:45
【问题描述】:
我在文章上有 cmets。但我决定稍微扩展评论的可能性,添加回复评论的功能。
评论实体:
@Entity()
export class Comment {
@PrimaryColumn()
id: string;
@Column({ type: 'varchar', length: 50 })
content: string;
@Column()
authorId: string;
@Column()
entityId: string;
@CreateDateColumn()
creatAt: Date;
@Column('text', { nullable: true })
replyId?: string;
@Column({ type: 'enum', enum: CommentTypeEnum })
type: CommentTypeEnum;
}
一些细节: 当我创建对评论的回复时,我会获取父评论的 id(我回复的评论)并将其写入 replyId。父级可以有 NULL 也可以包含一个 id。简而言之,一条新评论包含其父级的 id。
[
{
"id": "dee96b97-cd45-4a09-a27d-985617cc5a16",
"content": "comment",
"authorId": "a30bfd0b-0519-4b4f-bbc5-04178f8af782",
"entityId": "91e22cb7-cb42-49a3-a5e7-8d111111",
"creatAt": "2021-10-04T08:43:35.204Z",
"replyId": "cab2d3fd-7fba-4d02-a911-538246d92cfd",
"type": "COMMENT_TYPE"
},
{
"id": "d61e0049-9075-4f25-8f6d-65ed61e245a8",
"content": "comment",
"authorId": "a30bfd0b-0519-4b4f-bbc5-04178f8af782",
"entityId": "91e22cb7-cb42-49a3-a5e7-8d111111",
"creatAt": "2021-10-04T08:43:21.271Z",
"replyId": "4f7b2bb3-b224-45d9-8093-9c0de7514bd4",
"type": "COMMENT_TYPE"
},
{
"id": "cab2d3fd-7fba-4d02-a911-538246d92cfd",
"content": "comment",
"authorId": "a30bfd0b-0519-4b4f-bbc5-04178f8af782",
"entityId": "91e22cb7-cb42-49a3-a5e7-8d111111",
"creatAt": "2021-10-04T08:43:22.663Z",
"replyId": "4f7b2bb3-b224-45d9-8093-9c0de7514bd4",
"type": "COMMENT_TYPE"
},
{
"id": "4f7b2bb3-b224-45d9-8093-9c0de7514bd4",
"content": "comment",
"authorId": "a30bfd0b-0519-4b4f-bbc5-04178f8af782",
"entityId": "91e22cb7-cb42-49a3-a5e7-8d111111",
"creatAt": "2021-10-04T08:43:04.391Z",
"replyId": null,
"type": "COMMENT_TYPE"
},
{
"id": "1ea5cdcb-2f19-4b00-94e0-5b245bb91237",
"content": "comment",
"authorId": "a30bfd0b-0519-4b4f-bbc5-04178f8af782",
"entityId": "91e22cb7-cb42-49a3-a5e7-8d111111",
"creatAt": "2021-10-04T08:43:22.094Z",
"replyId": "4f7b2bb3-b224-45d9-8093-9c0de7514bd4",
"type": "COMMENT_TYPE"
}
]
获取特定记录的所有记录的方法:
@Get('/:entityId/comments')
async getAll(@Param('entityId') entityId: string): Promise<CommentDto[]> {
const comments = await this.commentRepository.find({ where: { entityId: entityId }, order: { id: 'DESC' } });
return comments.map(it => ({
id: it.id,
content: it.content,
authorId: it.authorId,
entityId: it.entityId,
creatAt: it.creatAt,
replyId: it.replyId || null,
type: it.type,
}));
}
DTO:
export class CommentDto {
id: string;
content: string;
authorId: string;
entityId: string;
creatAt: Date;
replyId?: string;
type: CommentTypeEnum;
}
如何获取记录,如下例所示:
[
{
"id": "4f7b2bb3-b224-45d9-8093-9c0de7514bd4",
"content": "comment",
"authorId": "a30bfd0b-0519-4b4f-bbc5-04178f8af782",
"entityId": "91e22cb7-cb42-49a3-a5e7-8d111111",
"creatAt": "2021-10-04T08:43:04.391Z",
"replyId": [
{
"id": "cab2d3fd-7fba-4d02-a911-538246d92cfd",
"content": "comment",
"authorId": "a30bfd0b-0519-4b4f-bbc5-04178f8af782",
"entityId": "91e22cb7-cb42-49a3-a5e7-8d111111",
"creatAt": "2021-10-04T08:43:22.663Z",
"replyId": [
{
"id": "dee96b97-cd45-4a09-a27d-985617cc5a16",
"content": "comment",
"authorId": "a30bfd0b-0519-4b4f-bbc5-04178f8af782",
"entityId": "91e22cb7-cb42-49a3-a5e7-8d111111",
"creatAt": "2021-10-04T08:43:35.204Z",
"replyId": "cab2d3fd-7fba-4d02-a911-538246d92cfd",
"type": "COMMENT_TYPE"
}
],
"type": "COMMENT_TYPE"
},
{
"id": "d61e0049-9075-4f25-8f6d-65ed61e245a8",
"content": "comment",
"authorId": "a30bfd0b-0519-4b4f-bbc5-04178f8af782",
"entityId": "91e22cb7-cb42-49a3-a5e7-8d111111",
"creatAt": "2021-10-04T08:43:21.271Z",
"replyId": "4f7b2bb3-b224-45d9-8093-9c0de7514bd4",
"type": "COMMENT_TYPE"
},
{
"id": "cab2d3fd-7fba-4d02-a911-538246d92cfd",
"content": "comment",
"authorId": "a30bfd0b-0519-4b4f-bbc5-04178f8af782",
"entityId": "91e22cb7-cb42-49a3-a5e7-8d111111",
"creatAt": "2021-10-04T08:43:22.663Z",
"replyId": "4f7b2bb3-b224-45d9-8093-9c0de7514bd4",
"type": "COMMENT_TYPE"
},
{
"id": "1ea5cdcb-2f19-4b00-94e0-5b245bb91237",
"content": "comment",
"authorId": "a30bfd0b-0519-4b4f-bbc5-04178f8af782",
"entityId": "91e22cb7-cb42-49a3-a5e7-8d111111",
"creatAt": "2021-10-04T08:43:22.094Z",
"replyId": "4f7b2bb3-b224-45d9-8093-9c0de7514bd4",
"type": "COMMENT_TYPE"
}
],
"type": "COMMENT_TYPE"
}
]
提前感谢您的回答
【问题讨论】:
标签: arrays database typescript list typeorm