From d30273d180e06e5967ee8171aee1ddc39c0fba17 Mon Sep 17 00:00:00 2001 From: xidongdong-153 Date: Tue, 12 Dec 2023 16:39:12 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E8=A1=A5=E5=85=85=E5=88=86=E7=B1=BB?= =?UTF-8?q?=E7=9A=84=E5=AD=98=E5=82=A8=E5=BA=93=E6=96=B9=E6=B3=95=E3=80=81?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=88=86=E7=B1=BB=E6=9C=8D=E5=8A=A1=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E6=AD=A3=E7=A1=AE=E6=B7=BB=E5=8A=A0children?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../content/repositories/category.repository.ts | 10 ++++++++++ src/modules/content/services/category.service.ts | 11 +++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/modules/content/repositories/category.repository.ts b/src/modules/content/repositories/category.repository.ts index c5807fe..41649fc 100644 --- a/src/modules/content/repositories/category.repository.ts +++ b/src/modules/content/repositories/category.repository.ts @@ -13,6 +13,16 @@ export class CategoryRepository extends TreeRepository { return this.createQueryBuilder('category').leftJoinAndSelect('category.parent', 'parent'); } + /** + * 树形结构查询 + * @param options + */ + async findTrees(options?: FindTreeOptions) { + const roots = await this.findRoots(options); + await Promise.all(roots.map((root) => this.findDescendantsTree(root, options))); + return roots; + } + /** * 查询顶级分类 * @param options diff --git a/src/modules/content/services/category.service.ts b/src/modules/content/services/category.service.ts index 4ac6fb5..9e32fd0 100644 --- a/src/modules/content/services/category.service.ts +++ b/src/modules/content/services/category.service.ts @@ -105,25 +105,20 @@ export class CategoryService { /** * 获取请求传入的父分类 * @param current 当前分类的ID - * @param parentId + * @param id */ protected async getParent(current?: string, parentId?: string) { if (current === parentId) return undefined; - let parent: CategoryEntity | undefined; - if (parentId !== undefined) { - if (parentId !== null) return null; - + if (parentId === null) return null; parent = await this.repository.findOne({ where: { id: parentId } }); - if (!parent) throw new EntityNotFoundError( CategoryEntity, - `Parent category ${parentId} not exists !`, + `Parent category ${parentId} not exists!`, ); } - return parent; } }