Skip to content

Commit

Permalink
Merge pull request #260 from Quickchive/refactor/#259-content-transac…
Browse files Browse the repository at this point in the history
…tional

Refactor/#259 content transactional
  • Loading branch information
stae1102 authored Feb 5, 2024
2 parents f341507 + bd4ee66 commit bd7fa71
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 51 deletions.
7 changes: 0 additions & 7 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@ import {
} from '@nestjs/common';
import { Cache } from 'cache-manager';
import { v4 as uuidv4 } from 'uuid';
import axios from 'axios';
import * as CryptoJS from 'crypto-js';

import { MailService } from '../mail/mail.service';
import { User } from '../users/entities/user.entity';
import {
refreshTokenExpirationInCache,
refreshTokenExpirationInCacheShortVersion,
Expand All @@ -28,12 +25,8 @@ import { sendPasswordResetEmailOutput } from './dtos/send-password-reset-email.d
import { RefreshTokenDto, RefreshTokenOutput } from './dtos/token.dto';
import { ValidateUserDto, ValidateUserOutput } from './dtos/validate-user.dto';
import { ONEYEAR, Payload } from './jwt/jwt.payload';
import { KakaoAuthorizeOutput, LoginWithKakaoDto } from './dtos/kakao.dto';
import { googleUserInfo } from './dtos/google.dto';
import { customJwtService } from './jwt/jwt.service';
import { UserRepository } from '../users/repository/user.repository';
import { CategoryRepository } from '../categories/category.repository';
import { OAuthUtil } from './util/oauth.util';

@Injectable()
export class AuthService {
Expand Down
34 changes: 5 additions & 29 deletions src/contents/contents.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,11 @@ export class ContentsController {
type: ErrorOutput,
})
@Post('multiple')
@UseInterceptors(TransactionInterceptor)
async addMultipleContents(
@AuthUser() user: User,
@Body() contentLinks: AddMultipleContentsBodyDto,
@TransactionManager() queryRunnerManager: EntityManager,
): Promise<AddContentOutput> {
return this.contentsService.addMultipleContents(
user,
contentLinks,
queryRunnerManager,
);
return this.contentsService.addMultipleContents(user, contentLinks);
}

@ApiOperation({
Expand All @@ -117,17 +111,11 @@ export class ContentsController {
type: ErrorOutput,
})
@Patch()
@UseInterceptors(TransactionInterceptor)
async updateContent(
@AuthUser() user: User,
@Body() content: UpdateContentBodyDto,
@TransactionManager() queryRunnerManager: EntityManager,
): Promise<UpdateContentOutput> {
return this.contentsService.updateContent(
user,
content,
queryRunnerManager,
);
return this.contentsService.updateContent(user, content);
}

@ApiOperation({
Expand All @@ -143,17 +131,11 @@ export class ContentsController {
type: ErrorOutput,
})
@Patch(':contentId/favorite')
@UseInterceptors(TransactionInterceptor)
async toggleFavorite(
@AuthUser() user: User,
@Param('contentId', new ParseIntPipe()) contentId: number,
@TransactionManager() queryRunnerManager: EntityManager,
@Param('contentId', ParseIntPipe) contentId: number,
): Promise<toggleFavoriteOutput> {
return this.contentsService.toggleFavorite(
user,
contentId,
queryRunnerManager,
);
return this.contentsService.toggleFavorite(user, contentId);
}

@ApiOperation({
Expand All @@ -169,17 +151,11 @@ export class ContentsController {
type: ErrorOutput,
})
@Delete(':contentId')
@UseInterceptors(TransactionInterceptor)
async deleteContent(
@AuthUser() user: User,
@Param('contentId', new ParseIntPipe()) contentId: number,
@TransactionManager() queryRunnerManager: EntityManager,
): Promise<DeleteContentOutput> {
return this.contentsService.deleteContent(
user,
contentId,
queryRunnerManager,
);
return this.contentsService.deleteContent(user, contentId);
}

@ApiOperation({
Expand Down
27 changes: 13 additions & 14 deletions src/contents/contents.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { CategoryRepository } from '../categories/category.repository';
import { getLinkInfo } from './util/content.util';
import { GetLinkInfoResponseDto } from './dtos/get-link.response.dto';
import { checkContentDuplicateAndAddCategorySaveLog } from '../categories/utils/category.util';
import { Transactional } from '../common/aop/transactional';

@Injectable()
export class ContentsService {
Expand Down Expand Up @@ -116,10 +117,11 @@ export class ContentsService {
}
}

@Transactional()
async addMultipleContents(
user: User,
{ contentLinks, categoryName, parentId }: AddMultipleContentsBodyDto,
queryRunnerManager: EntityManager,
entityManager?: EntityManager,
): Promise<AddContentOutput> {
try {
const userInDb =
Expand All @@ -136,7 +138,7 @@ export class ContentsService {
categoryName,
parentId,
userInDb,
queryRunnerManager,
entityManager!,
);
}
for (const link of contentLinks) {
Expand All @@ -152,7 +154,7 @@ export class ContentsService {
);
}

const newContent = queryRunnerManager.create(Content, {
const newContent = entityManager!.create(Content, {
link,
title,
siteName,
Expand All @@ -161,10 +163,7 @@ export class ContentsService {
description,
user: userInDb,
});
await queryRunnerManager.save(newContent);

// 각 링크마다 처리 후 transaction commit
await queryRunnerManager.query('COMMIT');
await this.contentRepository.saveOne(newContent, entityManager);
}
}

Expand All @@ -187,7 +186,7 @@ export class ContentsService {
categoryName,
parentId,
}: UpdateContentBodyDto,
queryRunnerManager: EntityManager,
entityManager?: EntityManager,
): Promise<AddContentOutput> {
const newContentObj = {
link,
Expand Down Expand Up @@ -217,7 +216,7 @@ export class ContentsService {
categoryName,
parentId,
userInDb,
queryRunnerManager,
entityManager!,
);

await checkContentDuplicateAndAddCategorySaveLog(
Expand All @@ -227,7 +226,7 @@ export class ContentsService {
);
}

await queryRunnerManager.save(Content, [
await entityManager!.save(Content, [
{ id: content.id, ...newContentObj, ...(category && { category }) },
]);

Expand All @@ -240,7 +239,7 @@ export class ContentsService {
async toggleFavorite(
user: User,
contentId: number,
queryRunnerManager: EntityManager,
entityManager?: EntityManager,
): Promise<toggleFavoriteOutput> {
try {
const userInDb = await this.userRepository.findOneWithContents(user.id);
Expand All @@ -258,7 +257,7 @@ export class ContentsService {
}

content.favorite = !content.favorite;
await queryRunnerManager.save(content);
await entityManager!.save(content);

return {};
} catch (e) {
Expand All @@ -269,7 +268,7 @@ export class ContentsService {
async deleteContent(
user: User,
contentId: number,
queryRunnerManager: EntityManager,
entityManager?: EntityManager,
): Promise<DeleteContentOutput> {
try {
const content = await this.contentRepository.findOneBy({ id: contentId });
Expand All @@ -284,7 +283,7 @@ export class ContentsService {
}

// delete content
await queryRunnerManager.delete(Content, content.id);
await entityManager!.delete(Content, content.id);

return {};
} catch (e) {
Expand Down
9 changes: 8 additions & 1 deletion src/contents/repository/content.repository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DataSource, Repository } from 'typeorm';
import { DataSource, EntityManager, Repository } from 'typeorm';
import { Content } from '../entities/content.entity';
import { Injectable } from '@nestjs/common';

Expand Down Expand Up @@ -43,4 +43,11 @@ export class ContentRepository extends Repository<Content> {
.andWhere('content.reminder < :now', { now: new Date() })
.getCount();
}

async saveOne(
content: Content,
entityManager?: EntityManager,
): Promise<Content> {
return entityManager ? entityManager.save(content) : this.save(content);
}
}

0 comments on commit bd7fa71

Please sign in to comment.