@@ -21,6 +21,8 @@ import { PatchPostDto } from './dtos/patch-Post.dto';
21
21
import { UserBlockService } from 'src/user-block/user-block.service' ;
22
22
import { PostClothingService } from 'src/post-clothing/post-clothing.service' ;
23
23
import dayjs from 'dayjs' ;
24
+ import { PostLikeService } from 'src/post-like/post-like.service' ;
25
+ import { PostCommentService } from 'src/post-comment/post-comment.service' ;
24
26
@Injectable ( )
25
27
export class PostService {
26
28
constructor (
@@ -31,6 +33,8 @@ export class PostService {
31
33
private readonly postImageService : PostImageService ,
32
34
private readonly postStyletagService : PostStyletagService ,
33
35
private readonly postClothingService : PostClothingService ,
36
+ private readonly postLikeService : PostLikeService ,
37
+ private readonly postCommentService : PostCommentService ,
34
38
private readonly dataSource : DataSource ,
35
39
) { }
36
40
@@ -255,6 +259,54 @@ export class PostService {
255
259
}
256
260
}
257
261
262
+ // 게시글 삭제
263
+ async deletePost ( postId : number , userId : number ) : Promise < void > {
264
+ const queryRunner = this . dataSource . createQueryRunner ( ) ;
265
+
266
+ await queryRunner . startTransaction ( ) ;
267
+
268
+ // 게시글 조회
269
+ const post = await this . postRepository . findOne ( {
270
+ where : { id : postId , user : { id : userId } , status : 'activated' } ,
271
+ } ) ;
272
+
273
+ try {
274
+ // 게시글 삭제
275
+ post . status = 'deactivated' ;
276
+ post . softDelete ( ) ;
277
+
278
+ await queryRunner . manager . save ( post ) ;
279
+
280
+ // 연결된 PostImage 삭제
281
+ await this . postImageService . deleteImagesByPostId ( postId , queryRunner ) ;
282
+
283
+ // 연결된 PostLike 삭제
284
+ await this . postLikeService . deletePostLikeByPostId ( postId , queryRunner ) ;
285
+
286
+ // 연결된 PostComment 삭제
287
+ await this . postCommentService . deleteCommentsByPostId ( postId , queryRunner ) ;
288
+
289
+ // 연결된 PostClothing 삭제
290
+ await this . postClothingService . deletePostClothingByPostId (
291
+ postId ,
292
+ queryRunner ,
293
+ ) ;
294
+
295
+ // 연결된 PostStyleTag 삭제
296
+ await this . postStyletagService . deletePostStyletagsByPostId (
297
+ postId ,
298
+ queryRunner ,
299
+ ) ;
300
+
301
+ await queryRunner . commitTransaction ( ) ;
302
+ } catch ( error ) {
303
+ await queryRunner . rollbackTransaction ( ) ;
304
+ throw InternalServerException ( '게시글 삭제에 실패했습니다.' ) ;
305
+ } finally {
306
+ await queryRunner . release ( ) ;
307
+ }
308
+ }
309
+
258
310
// 게시글 상세 조회
259
311
async getPost ( postId : number ) : Promise < Post > {
260
312
const post = await this . postRepository . findOne ( {
0 commit comments