feat: wechat api问题修复

This commit is contained in:
ilhamtahir committed 2024-12-04 21:50:33 +08:00
1 parent fe63b5edc9
commit 925bcdb987
5 files changed
+29 -4

No files matched your search

@@ -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({
+4
View File
@@ -5,4 +5,8 @@ export const CategoryError: Record<string, ErrorResponse> = {
code: 3001,
message: 'Category not found',
},
HAS_ARTICLE: {
code: 3002,
message: '该分类下还有文章,请先删除文章',
},
};
+10 -1
View File
@@ -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<Article>,
@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,
},
});
}
}
+8 -1
View File
@@ -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<Category>,
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);
}
@@ -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;