diff --git a/src/article/controller/category.controller.ts b/src/article/controller/category.controller.ts index 6e90461..f61de33 100644 --- a/src/article/controller/category.controller.ts +++ b/src/article/controller/category.controller.ts @@ -17,10 +17,14 @@ import { SearchCategoryFilter } from '../dto/search-category.filter'; import { PageResultMapper } from '../../core/mapper/page-result.mapper'; import { PageResult } from '../../core/vo/page-result'; import { UpdateCategoryRequest } from '../dto/update-category.request'; +import { ArticleService } from '../service/article.service'; @Controller('categories') export class CategoryController { - constructor(private readonly categoryService: CategoryService) {} + constructor( + private readonly categoryService: CategoryService, + private readonly articleService: ArticleService, + ) {} @Post() @ApiOkResponse({ diff --git a/src/article/error/category.error.ts b/src/article/error/category.error.ts index 6a41017..0bd3579 100644 --- a/src/article/error/category.error.ts +++ b/src/article/error/category.error.ts @@ -5,4 +5,8 @@ export const CategoryError: Record = { code: 3001, message: 'Category not found', }, + HAS_ARTICLE: { + code: 3002, + message: '该分类下还有文章,请先删除文章', + }, }; diff --git a/src/article/service/article.service.ts b/src/article/service/article.service.ts index e58c074..5531de1 100644 --- a/src/article/service/article.service.ts +++ b/src/article/service/article.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@nestjs/common'; +import { forwardRef, Inject, Injectable } from '@nestjs/common'; import { CreateArticleRequest } from '../dto/create-article.request'; import { Repository } from 'typeorm'; import { Article } from '../entity/article.entity'; @@ -14,6 +14,7 @@ export class ArticleService { constructor( @InjectRepository(Article) private readonly articleRepository: Repository
, + @Inject(forwardRef(() => CategoryService)) private readonly categoryService: CategoryService, ) {} @@ -82,4 +83,12 @@ export class ArticleService { const article = await this.get(id); await this.articleRepository.remove(article); } + + async countByCategoryId(id: string) { + return await this.articleRepository.countBy({ + category: { + id, + }, + }); + } } diff --git a/src/article/service/category.service.ts b/src/article/service/category.service.ts index a4d5683..8b677d7 100644 --- a/src/article/service/category.service.ts +++ b/src/article/service/category.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@nestjs/common'; +import { forwardRef, Inject, Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Category } from '../entity/category.entity'; import { Repository } from 'typeorm'; @@ -8,6 +8,7 @@ import { BizException } from '../../core/exception/biz.exception'; import { CategoryError } from '../error/category.error'; import { UpdateCategoryRequest } from '../dto/update-category.request'; import { UserService } from '../../core/service/user.service'; +import { ArticleService } from './article.service'; @Injectable() export class CategoryService { @@ -15,6 +16,8 @@ export class CategoryService { @InjectRepository(Category) private readonly categoryRepository: Repository, private readonly userService: UserService, + @Inject(forwardRef(() => ArticleService)) + private readonly articleService: ArticleService, ) {} async create(createCategoryRequest: CreateCategoryRequest) { @@ -62,6 +65,10 @@ export class CategoryService { if (!category) { throw new BizException(CategoryError.NOT_FOUND); } + const articleCount = await this.articleService.countByCategoryId(id); + if (articleCount > 0) { + throw new BizException(CategoryError.HAS_ARTICLE); + } await this.categoryRepository.remove(category); } diff --git a/src/we-chat/entity/mini-program-user.entity.ts b/src/we-chat/entity/mini-program-user.entity.ts index 6f9403f..991645c 100644 --- a/src/we-chat/entity/mini-program-user.entity.ts +++ b/src/we-chat/entity/mini-program-user.entity.ts @@ -1,6 +1,7 @@ import { BaseEntity } from '../../core/entity/base.entity'; -import { Column } from 'typeorm'; +import { Column, Entity } from 'typeorm'; +@Entity() export class MiniProgramUser extends BaseEntity { @Column({ unique: true }) openId: string;