Skip to content

Commit

Permalink
Merge pull request #266 from Quickchive/feat/#261-apply-transactional…
Browse files Browse the repository at this point in the history
…-to-adding-category

refactor: replace category entity manager
  • Loading branch information
stae1102 authored Feb 10, 2024
2 parents 6ef8b77 + 373e5d7 commit 8d98b0e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
8 changes: 1 addition & 7 deletions src/categories/category.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,11 @@ export class CategoryController {
type: ErrorOutput,
})
@Post()
@UseInterceptors(TransactionInterceptor)
async addCategory(
@AuthUser() user: User,
@Body() addCategoryBody: AddCategoryBodyDto,
@TransactionManager() queryRunnerManager: EntityManager,
): Promise<AddCategoryOutput> {
return this.categoryService.addCategory(
user,
addCategoryBody,
queryRunnerManager,
);
return this.categoryService.addCategory(user, addCategoryBody);
}

@ApiOperation({
Expand Down
17 changes: 17 additions & 0 deletions src/categories/category.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@ export class CategoryRepository extends Repository<Category> {
super(Category, dataSource.createEntityManager());
}

async findParentCategory(
parentId: number,
entityManager?: EntityManager,
): Promise<Category | null> {
return (
(await entityManager?.findOneBy(Category, { id: parentId })) ??
(await this.findOneBy({ id: parentId }))
);
}

async createOne(
category: Category,
entityManager?: EntityManager,
): Promise<Category> {
return (await entityManager?.save(category)) ?? (await this.save(category));
}

/**
* category를 생성하거나, 이미 존재하는 category를 가져옴
* content service의 method 내에서 중복되는 로직을 분리함
Expand Down
33 changes: 16 additions & 17 deletions src/categories/category.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
loadLogs,
makeCategoryListWithSaveCount,
} from './utils/category.util';
import { Transactional } from '../common/aop/transactional';

@Injectable()
export class CategoryService {
Expand All @@ -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<AddCategoryOutput> {
try {
const userInDb = await this.userRepository.findOneWithCategories(user.id);
Expand All @@ -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');
}
Expand Down Expand Up @@ -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 {};
Expand Down

0 comments on commit 8d98b0e

Please sign in to comment.