Skip to content

Commit c675c6e

Browse files
committed
✨ feat: feed의 comment_count와 comment 엔티티의 생성/삭제를 하나의 트랜잭션으로 관리하도록 코드 수정
1 parent b5d5300 commit c675c6e

File tree

1 file changed

+60
-19
lines changed

1 file changed

+60
-19
lines changed

server/src/comment/service/comment.service.ts

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ import { Payload } from '../../common/guard/jwt.guard';
1111
import { DeleteCommentRequestDto } from '../dto/request/delete-comment.dto';
1212
import { UpdateCommentRequestDto } from '../dto/request/update-comment.dto';
1313
import { GetCommentRequestDto } from '../dto/request/get-comment.dto';
14+
import { DataSource } from 'typeorm';
15+
import { Comment } from '../entity/comment.entity';
1416

1517
@Injectable()
1618
export class CommentService {
1719
constructor(
1820
private readonly commentRepository: CommentRepository,
1921
private readonly feedRepository: FeedRepository,
2022
private readonly userRepository: UserRepository,
23+
private readonly dataSource: DataSource,
2124
) {}
2225

2326
private async commentCheck(userInformation: Payload, commentId: number) {
@@ -35,6 +38,8 @@ export class CommentService {
3538
if (userInformation.id !== commentObj.user.id) {
3639
throw new UnauthorizedException('본인이 작성한 댓글이 아닙니다.');
3740
}
41+
42+
return commentObj;
3843
}
3944

4045
async get(commentDto: GetCommentRequestDto) {
@@ -62,30 +67,66 @@ export class CommentService {
6267
}
6368

6469
async create(userInformation: Payload, commentDto: CreateCommentRequestDto) {
65-
const feed = await this.feedRepository.findOneBy({ id: commentDto.feedId });
66-
if (!feed) {
67-
throw new NotFoundException('존재하지 않는 게시글입니다.');
68-
}
70+
const queryRunner = this.dataSource.createQueryRunner();
71+
await queryRunner.connect();
72+
await queryRunner.startTransaction();
6973

70-
const user = await this.userRepository.findOneBy({
71-
id: userInformation.id,
72-
});
73-
if (!user) {
74-
throw new NotFoundException('존재하지 않는 유저입니다.');
75-
}
74+
try {
75+
const feed = await this.feedRepository.findOneBy({
76+
id: commentDto.feedId,
77+
});
78+
if (!feed) {
79+
throw new NotFoundException('존재하지 않는 게시글입니다.');
80+
}
7681

77-
await this.commentRepository.save({
78-
comment: commentDto.comment,
79-
feed,
80-
user,
81-
});
82+
const user = await this.userRepository.findOneBy({
83+
id: userInformation.id,
84+
});
85+
86+
if (!user) {
87+
throw new NotFoundException('존재하지 않는 유저입니다.');
88+
}
89+
90+
feed.commentCount++;
91+
await queryRunner.manager.save(feed);
92+
await queryRunner.manager.save(Comment, {
93+
comment: commentDto.comment,
94+
feed: { id: commentDto.feedId },
95+
user: { id: userInformation.id },
96+
});
97+
98+
await queryRunner.commitTransaction();
99+
} catch (error) {
100+
await queryRunner.rollbackTransaction();
101+
throw error;
102+
} finally {
103+
await queryRunner.release();
104+
}
82105
}
83106

84107
async delete(userInformation: Payload, commentDto: DeleteCommentRequestDto) {
85-
await this.commentCheck(userInformation, commentDto.commentId);
86-
await this.feedRepository.delete({
87-
id: commentDto.commentId,
88-
});
108+
const comment = await this.commentCheck(
109+
userInformation,
110+
commentDto.commentId,
111+
);
112+
113+
const queryRunner = this.dataSource.createQueryRunner();
114+
await queryRunner.connect();
115+
await queryRunner.startTransaction();
116+
117+
try {
118+
const feed = comment.feed;
119+
feed.commentCount--;
120+
await queryRunner.manager.save(feed);
121+
await queryRunner.manager.remove(comment);
122+
123+
await queryRunner.commitTransaction();
124+
} catch (error) {
125+
await queryRunner.rollbackTransaction();
126+
throw error;
127+
} finally {
128+
await queryRunner.release();
129+
}
89130
}
90131

91132
async update(userInformation: Payload, commentDto: UpdateCommentRequestDto) {

0 commit comments

Comments
 (0)