add base service
This commit is contained in:
@@ -2,33 +2,23 @@ import { Injectable } from '@nestjs/common';
|
||||
import { isNil, omit } from 'lodash';
|
||||
import { EntityNotFoundError } from 'typeorm';
|
||||
|
||||
import {
|
||||
CreateCategoryDto,
|
||||
QueryCategoryDto,
|
||||
UpdateCategoryDto,
|
||||
} from '@/modules/content/dtos/category.dto';
|
||||
import { CreateCategoryDto, UpdateCategoryDto } from '@/modules/content/dtos/category.dto';
|
||||
import { CategoryEntity } from '@/modules/content/entities/category.entity';
|
||||
import { CategoryRepository } from '@/modules/content/repositories/category.repository';
|
||||
import { treePaginate } from '@/modules/database/utils';
|
||||
import { BaseService } from '@/modules/database/base/service';
|
||||
|
||||
@Injectable()
|
||||
export class CategoryService {
|
||||
constructor(protected repository: CategoryRepository) {}
|
||||
export class CategoryService extends BaseService<CategoryEntity, CategoryRepository> {
|
||||
protected enableTrash = true;
|
||||
|
||||
constructor(protected repository: CategoryRepository) {
|
||||
super(repository);
|
||||
}
|
||||
|
||||
async findTrees() {
|
||||
return this.repository.findTrees();
|
||||
}
|
||||
|
||||
async paginate(options?: QueryCategoryDto) {
|
||||
const tree = await this.findTrees();
|
||||
const data = await this.repository.toFlatTrees(tree);
|
||||
return treePaginate(options, data);
|
||||
}
|
||||
|
||||
async detail(id: string) {
|
||||
return this.repository.findOneOrFail({ where: { id }, relations: ['parent', 'children'] });
|
||||
}
|
||||
|
||||
async create(data: CreateCategoryDto) {
|
||||
const item = await this.repository.save({
|
||||
...data,
|
||||
@@ -56,21 +46,6 @@ export class CategoryService {
|
||||
return item;
|
||||
}
|
||||
|
||||
async delete(id: string) {
|
||||
const item = await this.repository.findOneOrFail({
|
||||
where: { id },
|
||||
relations: ['parent', 'children'],
|
||||
});
|
||||
if (!isNil(item.children) && item.children.length > 0) {
|
||||
const childrenCategories = [...item.children].map((c) => {
|
||||
c.parent = item.parent;
|
||||
return item;
|
||||
});
|
||||
await this.repository.save(childrenCategories, { reload: true });
|
||||
}
|
||||
return this.repository.remove(item);
|
||||
}
|
||||
|
||||
async getParent(current?: string, parentId?: string) {
|
||||
if (current === parentId) {
|
||||
return undefined;
|
||||
|
||||
Reference in New Issue
Block a user