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; } }