@@ -11,13 +11,16 @@ import { Payload } from '../../common/guard/jwt.guard';
1111import { DeleteCommentRequestDto } from '../dto/request/delete-comment.dto' ;
1212import { UpdateCommentRequestDto } from '../dto/request/update-comment.dto' ;
1313import { GetCommentRequestDto } from '../dto/request/get-comment.dto' ;
14+ import { DataSource } from 'typeorm' ;
15+ import { Comment } from '../entity/comment.entity' ;
1416
1517@Injectable ( )
1618export 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