@@ -11,21 +11,27 @@ 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
23- private async commentCheck ( userInformation : Payload , commentId : number ) {
26+ private async getValidatedComment (
27+ userInformation : Payload ,
28+ commentId : number ,
29+ ) {
2430 const commentObj = await this . commentRepository . findOne ( {
2531 where : {
2632 id : commentId ,
2733 } ,
28- relations : [ 'user' ] ,
34+ relations : [ 'user' , 'feed' ] ,
2935 } ) ;
3036
3137 if ( ! commentObj ) {
@@ -35,6 +41,8 @@ export class CommentService {
3541 if ( userInformation . id !== commentObj . user . id ) {
3642 throw new UnauthorizedException ( '본인이 작성한 댓글이 아닙니다.' ) ;
3743 }
44+
45+ return commentObj ;
3846 }
3947
4048 async get ( commentDto : GetCommentRequestDto ) {
@@ -62,37 +70,73 @@ export class CommentService {
6270 }
6371
6472 async create ( userInformation : Payload , commentDto : CreateCommentRequestDto ) {
65- const feed = await this . feedRepository . findOneBy ( { id : commentDto . feedId } ) ;
66- if ( ! feed ) {
67- throw new NotFoundException ( '존재하지 않는 게시글입니다.' ) ;
68- }
73+ const queryRunner = this . dataSource . createQueryRunner ( ) ;
74+ await queryRunner . connect ( ) ;
75+ await queryRunner . startTransaction ( ) ;
6976
70- const user = await this . userRepository . findOneBy ( {
71- id : userInformation . id ,
72- } ) ;
73- if ( ! user ) {
74- throw new NotFoundException ( '존재하지 않는 유저입니다.' ) ;
75- }
77+ try {
78+ const feed = await this . feedRepository . findOneBy ( {
79+ id : commentDto . feedId ,
80+ } ) ;
81+ if ( ! feed ) {
82+ throw new NotFoundException ( '존재하지 않는 게시글입니다.' ) ;
83+ }
7684
77- await this . commentRepository . save ( {
78- comment : commentDto . comment ,
79- feed,
80- user,
81- } ) ;
85+ const user = await this . userRepository . findOneBy ( {
86+ id : userInformation . id ,
87+ } ) ;
88+
89+ if ( ! user ) {
90+ throw new NotFoundException ( '존재하지 않는 유저입니다.' ) ;
91+ }
92+
93+ feed . commentCount ++ ;
94+ await queryRunner . manager . save ( feed ) ;
95+ await queryRunner . manager . save ( Comment , {
96+ comment : commentDto . comment ,
97+ feed : { id : commentDto . feedId } ,
98+ user : { id : userInformation . id } ,
99+ } ) ;
100+
101+ await queryRunner . commitTransaction ( ) ;
102+ } catch ( error ) {
103+ await queryRunner . rollbackTransaction ( ) ;
104+ throw error ;
105+ } finally {
106+ await queryRunner . release ( ) ;
107+ }
82108 }
83109
84110 async delete ( userInformation : Payload , commentDto : DeleteCommentRequestDto ) {
85- await this . commentCheck ( userInformation , commentDto . commentId ) ;
86- await this . feedRepository . delete ( {
87- id : commentDto . commentId ,
88- } ) ;
111+ const comment = await this . getValidatedComment (
112+ userInformation ,
113+ commentDto . commentId ,
114+ ) ;
115+
116+ const queryRunner = this . dataSource . createQueryRunner ( ) ;
117+ await queryRunner . connect ( ) ;
118+ await queryRunner . startTransaction ( ) ;
119+
120+ try {
121+ const feed = comment . feed ;
122+ feed . commentCount -- ;
123+ await queryRunner . manager . save ( feed ) ;
124+ await queryRunner . manager . remove ( comment ) ;
125+
126+ await queryRunner . commitTransaction ( ) ;
127+ } catch ( error ) {
128+ await queryRunner . rollbackTransaction ( ) ;
129+ throw error ;
130+ } finally {
131+ await queryRunner . release ( ) ;
132+ }
89133 }
90134
91135 async update ( userInformation : Payload , commentDto : UpdateCommentRequestDto ) {
92- await this . commentCheck ( userInformation , commentDto . commentId ) ;
93- const commentObj = await this . commentRepository . findOneBy ( {
94- id : commentDto . commentId ,
95- } ) ;
136+ const commentObj = await this . getValidatedComment (
137+ userInformation ,
138+ commentDto . commentId ,
139+ ) ;
96140
97141 commentObj . comment = commentDto . newComment ;
98142 await this . commentRepository . save ( commentObj ) ;
0 commit comments