@@ -5,11 +5,12 @@ import {
55} from '@nestjs/common' ;
66import { CommentRepository } from '../repository/comment.repository' ;
77import { CreateCommentRequestDto } from '../dto/request/create-comment.dto' ;
8- import { FeedRepository } from '../../feed/repository/feed.repository' ;
98import { Payload } from '../../common/guard/jwt.guard' ;
109import { DeleteCommentRequestDto } from '../dto/request/delete-comment.dto' ;
1110import { UpdateCommentRequestDto } from '../dto/request/update-comment.dto' ;
1211import { GetCommentRequestDto } from '../dto/request/get-comment.dto' ;
12+ import { DataSource } from 'typeorm' ;
13+ import { Comment } from '../entity/comment.entity' ;
1314import { GetCommentResponseDto } from '../dto/response/get-comment.dto' ;
1415import { FeedService } from '../../feed/service/feed.service' ;
1516import { UserService } from '../../user/service/user.service' ;
@@ -18,17 +19,20 @@ import { UserService } from '../../user/service/user.service';
1819export class CommentService {
1920 constructor (
2021 private readonly commentRepository : CommentRepository ,
21- private readonly feedRepository : FeedRepository ,
22+ private readonly dataSource : DataSource ,
2223 private readonly userService : UserService ,
2324 private readonly feedService : FeedService ,
2425 ) { }
2526
26- private async commentCheck ( userInformation : Payload , commentId : number ) {
27+ private async getValidatedComment (
28+ userInformation : Payload ,
29+ commentId : number ,
30+ ) {
2731 const commentObj = await this . commentRepository . findOne ( {
2832 where : {
2933 id : commentId ,
3034 } ,
31- relations : [ 'user' ] ,
35+ relations : [ 'user' , 'feed' ] ,
3236 } ) ;
3337
3438 if ( ! commentObj ) {
@@ -52,25 +56,57 @@ export class CommentService {
5256 }
5357
5458 async create ( userInformation : Payload , commentDto : CreateCommentRequestDto ) {
55- const feed = await this . feedService . getFeed ( commentDto . feedId ) ;
56- const user = await this . userService . getUser ( userInformation . id ) ;
59+ const queryRunner = this . dataSource . createQueryRunner ( ) ;
60+ await queryRunner . connect ( ) ;
61+ await queryRunner . startTransaction ( ) ;
5762
58- await this . commentRepository . save ( {
59- comment : commentDto . comment ,
60- feed,
61- user,
62- } ) ;
63+ try {
64+ const feed = await this . feedService . getFeed ( commentDto . feedId ) ;
65+ await this . userService . getUser ( userInformation . id ) ;
66+ feed . commentCount ++ ;
67+ await queryRunner . manager . save ( feed ) ;
68+ await queryRunner . manager . save ( Comment , {
69+ comment : commentDto . comment ,
70+ feed : { id : commentDto . feedId } ,
71+ user : { id : userInformation . id } ,
72+ } ) ;
73+
74+ await queryRunner . commitTransaction ( ) ;
75+ } catch ( error ) {
76+ await queryRunner . rollbackTransaction ( ) ;
77+ throw error ;
78+ } finally {
79+ await queryRunner . release ( ) ;
80+ }
6381 }
6482
6583 async delete ( userInformation : Payload , commentDto : DeleteCommentRequestDto ) {
66- await this . commentCheck ( userInformation , commentDto . commentId ) ;
67- await this . feedRepository . delete ( {
68- id : commentDto . commentId ,
69- } ) ;
84+ const comment = await this . getValidatedComment (
85+ userInformation ,
86+ commentDto . commentId ,
87+ ) ;
88+
89+ const queryRunner = this . dataSource . createQueryRunner ( ) ;
90+ await queryRunner . connect ( ) ;
91+ await queryRunner . startTransaction ( ) ;
92+
93+ try {
94+ const feed = comment . feed ;
95+ feed . commentCount -- ;
96+ await queryRunner . manager . save ( feed ) ;
97+ await queryRunner . manager . remove ( comment ) ;
98+
99+ await queryRunner . commitTransaction ( ) ;
100+ } catch ( error ) {
101+ await queryRunner . rollbackTransaction ( ) ;
102+ throw error ;
103+ } finally {
104+ await queryRunner . release ( ) ;
105+ }
70106 }
71107
72108 async update ( userInformation : Payload , commentDto : UpdateCommentRequestDto ) {
73- const commentObj = await this . commentCheck (
109+ const commentObj = await this . getValidatedComment (
74110 userInformation ,
75111 commentDto . commentId ,
76112 ) ;
0 commit comments