Currently utilizing Nestjs for the backend and in the process of developing an API call that will return all subsections pertaining to a specific section. Seeking guidance or suggestions on how to accomplish this task.
If a GET request is made to /subsection/167 (where 167 represents the section ID), the endpoint should provide all subsections associated with that particular section.
Entity
@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;
Controller
@Get('/subsection/:parentId')
async getHelpSubSection(@Req() req, @Param() params):Promise<HelpEntity[]>{
return this.helpService.getHelpbySubSection(req.user, params.id);
}
Service
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;
}
}
In my database, all parent sections have NULL values in the ParentId field.