1
+ package com .cona .KUsukKusuk .comment .controller ;
2
+
3
+ import com .cona .KUsukKusuk .comment .domain .Comment ;
4
+ import com .cona .KUsukKusuk .comment .dto .*;
5
+ import com .cona .KUsukKusuk .comment .exception .CommentNotFoundException ;
6
+ import com .cona .KUsukKusuk .comment .exception .CommentUserNotMatchedException ;
7
+ import com .cona .KUsukKusuk .comment .service .CommentService ;
8
+ import com .cona .KUsukKusuk .global .response .HttpResponse ;
9
+ import com .cona .KUsukKusuk .spot .domain .Spot ;
10
+ import com .cona .KUsukKusuk .user .domain .User ;
11
+ import com .cona .KUsukKusuk .user .service .UserService ;
12
+ import io .swagger .v3 .oas .annotations .Operation ;
13
+ import org .springframework .web .bind .annotation .*;
14
+
15
+
16
+ @ RestController
17
+ @ RequestMapping ("comment" )
18
+ public class CommentController {
19
+ final private CommentService commentService ;
20
+ final private UserService userService ;
21
+ private CommentJoinRequest CommentJoinRequest ;
22
+
23
+ public CommentController (CommentService commentService , UserService userService ) {
24
+ this .commentService = commentService ;
25
+ this .userService = userService ;
26
+ }
27
+
28
+ @ PostMapping ("/{spotId}/register" )
29
+ @ Operation (summary = "특정 장소 글에 댓글 등록" , description = "로그인한 사용자의 댓글을 등록합니다." )
30
+ public HttpResponse <CommentJoinResponse > saveComment (@ PathVariable ("spotId" ) Long spotId , @ RequestBody CommentJoinRequest commentJoinRequest ){
31
+ //user, spot 가져오기 - 수정 필요
32
+ User user = commentService .getCurrentUser ();
33
+ Spot spot = commentService .getCurrentSpot (spotId );
34
+ //위 user, spot 사용해서 comment 객체 만들기
35
+ System .out .println ("user, spot 까지 담기 완료" );
36
+ Comment comment = commentJoinRequest .toEntity (user , spot );
37
+ System .out .println ("comment 객체 생성 완료" );
38
+ Comment savedComment = commentService .save (comment );
39
+ return HttpResponse .okBuild (
40
+ CommentJoinResponse .of (savedComment ));
41
+ }
42
+
43
+ @ PostMapping ("/{spotId}/{commentId}/update" )
44
+ @ Operation (summary = "특정 장소 글의 댓글 수정" , description = "로그인한 사용자의 자신의 댓글을 수정합니다." )
45
+ public HttpResponse <CommentUpdateResponse > updateComment (@ PathVariable ("spotId" )Long spotId , @ PathVariable ("commentId" )Long commentId , @ RequestBody CommentUpdateRequest commentUpdateRequest ) throws CommentNotFoundException , CommentUserNotMatchedException {
46
+
47
+ String commentUserName = userService .getUsernameBySecurityContext ();
48
+
49
+
50
+ Spot spot = commentService .getCurrentSpot (spotId );
51
+ Comment updateComment = commentService .getCurrentComment (commentUserName ,spot ,commentId );
52
+ updateComment .setComment (commentUpdateRequest .comment ());
53
+
54
+ Comment savedComment = commentService .save (updateComment );
55
+ return HttpResponse .okBuild (
56
+ CommentUpdateResponse .of (updateComment )
57
+ );
58
+ }
59
+
60
+ @ GetMapping ("/{spotId}/{commentId}/delete" )
61
+ @ Operation (summary = "특정 장소 글의 댓글 삭제" , description = "로그인한 사용자의 자신의 댓글을 삭제합니다." )
62
+ public HttpResponse <CommentDeleteResponse > deleteComment (@ PathVariable ("spotId" )Long spotId , @ PathVariable ("commentId" )Long commentId ) throws CommentNotFoundException , CommentUserNotMatchedException {
63
+
64
+ String commentUserName = userService .getUsernameBySecurityContext ();
65
+ Spot spot = commentService .getCurrentSpot (spotId );
66
+ Comment comment = commentService .getCurrentComment (commentUserName ,spot ,commentId );
67
+
68
+ commentService .delete (comment );
69
+ return HttpResponse .okBuild (
70
+ CommentDeleteResponse .of ("댓글이 삭제 되었습니다." )
71
+ );
72
+ }
73
+
74
+
75
+ }
0 commit comments