|
@@ -0,0 +1,67 @@
|
|
|
|
|
+import { Injectable } from '@nestjs/common';
|
|
|
|
|
+import { InjectRepository } from '@nestjs/typeorm';
|
|
|
|
|
+import { Category } from '../entity/category.entity';
|
|
|
|
|
+import { Repository } from 'typeorm';
|
|
|
|
|
+import { CreateCategoryRequest } from '../dto/create-category.request';
|
|
|
|
|
+import { SearchCategoryFilter } from '../dto/search-category.filter';
|
|
|
|
|
+import { BizException } from '../../core/exceptions/biz.exception';
|
|
|
|
|
+import { CategoryError } from '../error/category.error';
|
|
|
|
|
+import { UpdateCategoryRequest } from '../dto/update-category.request';
|
|
|
|
|
+import { UserService } from '../../core/service/user.service';
|
|
|
|
|
+
|
|
|
|
|
+@Injectable()
|
|
|
|
|
+export class CategoryService {
|
|
|
|
|
+ constructor(
|
|
|
|
|
+ @InjectRepository(Category)
|
|
|
|
|
+ private readonly categoryRepository: Repository<Category>,
|
|
|
|
|
+ private readonly userService: UserService,
|
|
|
|
|
+ ) {}
|
|
|
|
|
+
|
|
|
|
|
+ async create(createCategoryRequest: CreateCategoryRequest) {
|
|
|
|
|
+ const category = new Category();
|
|
|
|
|
+ category.name = createCategoryRequest.name;
|
|
|
|
|
+ category.code = createCategoryRequest.code;
|
|
|
|
|
+ category.order = createCategoryRequest.order;
|
|
|
|
|
+ category.createBy = this.userService.getCurrentUser();
|
|
|
|
|
+ category.updateBy = this.userService.getCurrentUser();
|
|
|
|
|
+
|
|
|
|
|
+ return this.categoryRepository.save(category);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ async search(searchCategoryFilter: SearchCategoryFilter) {
|
|
|
|
|
+ return this.categoryRepository.findAndCount({
|
|
|
|
|
+ where: searchCategoryFilter.getConditions(),
|
|
|
|
|
+ skip: searchCategoryFilter.getSkip(),
|
|
|
|
|
+ take: searchCategoryFilter.getSize(),
|
|
|
|
|
+ order: searchCategoryFilter.getOrderBy(),
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ async get(id: string) {
|
|
|
|
|
+ const category = await this.categoryRepository.findOneBy({ id });
|
|
|
|
|
+ if (!category) {
|
|
|
|
|
+ throw new BizException(CategoryError.NOT_FOUND);
|
|
|
|
|
+ }
|
|
|
|
|
+ return category;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ async update(id: string, updateCategoryRequest: UpdateCategoryRequest) {
|
|
|
|
|
+ const category = await this.categoryRepository.findOneBy({ id });
|
|
|
|
|
+ if (!category) {
|
|
|
|
|
+ throw new BizException(CategoryError.NOT_FOUND);
|
|
|
|
|
+ }
|
|
|
|
|
+ category.name = updateCategoryRequest.name;
|
|
|
|
|
+ category.code = updateCategoryRequest.code;
|
|
|
|
|
+ category.order = updateCategoryRequest.order;
|
|
|
|
|
+ category.updateBy = this.userService.getCurrentUser();
|
|
|
|
|
+ return this.categoryRepository.save(category);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ async delete(id: string) {
|
|
|
|
|
+ const category = await this.categoryRepository.findOneBy({ id });
|
|
|
|
|
+ if (!category) {
|
|
|
|
|
+ throw new BizException(CategoryError.NOT_FOUND);
|
|
|
|
|
+ }
|
|
|
|
|
+ await this.categoryRepository.remove(category);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|