fix:补充分类的存储库方法、修复分类服务没有正确添加children

This commit is contained in:
2023-12-12 16:39:12 +08:00
parent dbc14c2137
commit d30273d180
2 changed files with 13 additions and 8 deletions
@@ -13,6 +13,16 @@ export class CategoryRepository extends TreeRepository<CategoryEntity> {
return this.createQueryBuilder('category').leftJoinAndSelect('category.parent', 'parent'); 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 * @param options
@@ -105,25 +105,20 @@ export class CategoryService {
/** /**
* 获取请求传入的父分类 * 获取请求传入的父分类
* @param current 当前分类的ID * @param current 当前分类的ID
* @param parentId * @param id
*/ */
protected async getParent(current?: string, parentId?: string) { protected async getParent(current?: string, parentId?: string) {
if (current === parentId) return undefined; if (current === parentId) return undefined;
let parent: CategoryEntity | undefined; let parent: CategoryEntity | undefined;
if (parentId !== undefined) { if (parentId !== undefined) {
if (parentId !== null) return null; if (parentId === null) return null;
parent = await this.repository.findOne({ where: { id: parentId } }); parent = await this.repository.findOne({ where: { id: parentId } });
if (!parent) if (!parent)
throw new EntityNotFoundError( throw new EntityNotFoundError(
CategoryEntity, CategoryEntity,
`Parent category ${parentId} not exists !`, `Parent category ${parentId} not exists!`,
); );
} }
return parent; return parent;
} }
} }