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

This commit is contained in:
3R-喜东东 2023-12-12 16:39:12 +08:00
parent dbc14c2137
commit d30273d180
2 changed files with 13 additions and 8 deletions

View File

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

View File

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