【发布时间】:2021-02-23 21:49:50
【问题描述】:
我正在使用 Nestjs 作为我的后端,我正在尝试创建一个 api 调用来返回特定部分的所有小节,但不知道如何做到这一点,所以如果我能得到任何帮助或建议,我将不胜感激.
因此,如果我执行 Get/subsection/167(167 是部分 ID),那么我将获得该特定部分的所有小节。
实体
@PrimaryGeneratedColumn({
type: "int",
name: "Id",
})
id: number;
@Column("nvarchar", {
nullable: false,
unique: true,
name: "Name"
})
name: string;
@Column("nvarchar", {
nullable: false,
name: "ParentId"
})
parentId: number;
控制器
@Get('/subsection/:parentId')
async getHelpSubSection(@Req() req, @Param() params):Promise<HelpEntity[]>{
return this.helpService.getHelpbySubSection(req.user, params.id);
}
服务
constructor(@InjectRepository(HelpEntity) private readonly helpRepo: Repository<HelpEntity>,
async getHelpbySubSection(gmid: string, id: number) : Promise<any>{
let check = await this.checkIfAdmin(gmid);
if (!check) {
throw new HttpException('Unauthorized', 401);
} else {
const res = await this.helpRepo.findOne()
if (!res) {
throw new NotFoundException(`This ID: ${id} is not found`)
}
return res;
}
}
在我的数据库中,我所有的父部分在 ParentId 中都有 NULL。
【问题讨论】:
标签: angular typescript api backend nestjs