diff --git a/src/categories/category.controller.ts b/src/categories/category.controller.ts index 0053fbe..7bf178c 100644 --- a/src/categories/category.controller.ts +++ b/src/categories/category.controller.ts @@ -66,17 +66,11 @@ export class CategoryController { type: ErrorOutput, }) @Post() - @UseInterceptors(TransactionInterceptor) async addCategory( @AuthUser() user: User, @Body() addCategoryBody: AddCategoryBodyDto, - @TransactionManager() queryRunnerManager: EntityManager, ): Promise { - return this.categoryService.addCategory( - user, - addCategoryBody, - queryRunnerManager, - ); + return this.categoryService.addCategory(user, addCategoryBody); } @ApiOperation({ diff --git a/src/categories/category.repository.ts b/src/categories/category.repository.ts index 9b691a7..c10e91e 100644 --- a/src/categories/category.repository.ts +++ b/src/categories/category.repository.ts @@ -14,6 +14,23 @@ export class CategoryRepository extends Repository { super(Category, dataSource.createEntityManager()); } + async findParentCategory( + parentId: number, + entityManager?: EntityManager, + ): Promise { + return ( + (await entityManager?.findOneBy(Category, { id: parentId })) ?? + (await this.findOneBy({ id: parentId })) + ); + } + + async createOne( + category: Category, + entityManager?: EntityManager, + ): Promise { + return (await entityManager?.save(category)) ?? (await this.save(category)); + } + /** * category를 생성하거나, 이미 존재하는 category를 가져옴 * content service의 method 내에서 중복되는 로직을 분리함 diff --git a/src/categories/category.service.ts b/src/categories/category.service.ts index 09416ce..131e810 100644 --- a/src/categories/category.service.ts +++ b/src/categories/category.service.ts @@ -33,6 +33,7 @@ import { loadLogs, makeCategoryListWithSaveCount, } from './utils/category.util'; +import { Transactional } from '../common/aop/transactional'; @Injectable() export class CategoryService { @@ -43,10 +44,11 @@ export class CategoryService { private readonly openaiService: OpenaiService, ) {} + @Transactional() async addCategory( user: User, { categoryName, iconName, parentId }: AddCategoryBodyDto, - queryRunnerManager: EntityManager, + entityManager?: EntityManager, ): Promise { try { const userInDb = await this.userRepository.findOneWithCategories(user.id); @@ -62,9 +64,10 @@ export class CategoryService { let currentParentId: number | undefined = parentId; let parentCategory: Category | null; for (let i = 0; i < 2; i++) { - parentCategory = await queryRunnerManager.findOne(Category, { - where: { id: currentParentId }, - }); + parentCategory = await this.categoryRepository.findParentCategory( + currentParentId, + entityManager, + ); if (i == 1 && parentCategory?.parentId !== null) { throw new ConflictException('Category depth should be 3'); } @@ -100,27 +103,23 @@ export class CategoryService { } else { // if parent category exists, get parent category const parentCategory: Category | null = parentId - ? await queryRunnerManager.findOne(Category, { - where: { id: parentId }, - }) + ? await this.categoryRepository.findParentCategory(parentId) : null; // if parent category doesn't exist, throw error if (!parentCategory && parentId) { throw new NotFoundException('Parent category not found'); } - const category = await queryRunnerManager.save( - queryRunnerManager.create(Category, { - slug: categorySlug, - name: categoryName, - iconName, - parentId: parentCategory?.id, - user: userInDb, - }), - ); + const category = await this.categoryRepository.save({ + slug: categorySlug, + name: categoryName, + iconName, + parentId: parentCategory?.id, + user: userInDb, + }); userInDb.categories?.push(category); - await queryRunnerManager.save(userInDb); + await entityManager!.save(userInDb); } return {};