如何返回属于特定节的所有子节? [英] How to return all the subsections that belong to particular section?

查看:61
本文介绍了如何返回属于特定节的所有子节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Nestjs作为后端,并且尝试创建一个api调用,该调用返回特定部分的所有子部分,但不确定如何执行该操作,因此,如果能获得任何帮助或建议,我将不胜感激

I'm using Nestjs for my backend and I'm trying to create an api call that return all the subsections for particular section but not sure how to do that so I would be really appreciated if I can get any help or suggestion.

因此,如果我执行Get/subsection/167(167是小节ID),那么我将获得该特定小节的所有小节.

so if I do Get/subsection/167 (167 is the section Id) then I will get all the subsections for that particular section.

实体

    @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.

In my database all my parent section has NULL in ParentId.

推荐答案

您需要做的是编辑查找对象以根据您的条件进行查找:

What you need to do is to edit the findone to find with your condition:

 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.find({parentId : id})
        if (!res) {
            throw new NotFoundException(`This ID: ${id} is not found`)
        }
        return res;
    }
}

更新
并在控制器中确定参数的名称

Update
and in your controller fix the name of the param

  @Get('/subsection/:parentId')
async getHelpSubSection(@Req() req, @Param() params):Promise<HelpEntity[]>{
    return this.helpService.getHelpbySubSection(req.user, params.parentId);
}

有关更多详细信息,请检查:查找选项

for more details please check : find-options

这篇关于如何返回属于特定节的所有子节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆