Skip to content

Commit 7ef35b6

Browse files
authored
Feat#36 댓글 CRUID 기능 구현 (#52)
* postComment 초기 데이터 작성 * 댓글 CRUD 기능 구현
1 parent b6a514b commit 7ef35b6

File tree

10 files changed

+375
-20
lines changed

10 files changed

+375
-20
lines changed
Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
package com.back.domain.post.comment.controller;
22

33
import com.back.domain.member.member.entity.Member;
4+
import com.back.domain.post.comment.dto.CommentAllResponse;
45
import com.back.domain.post.comment.dto.CommentCreateRequest;
6+
import com.back.domain.post.comment.dto.CommentDeleteRequest;
7+
import com.back.domain.post.comment.dto.CommentModifyRequest;
58
import com.back.domain.post.comment.service.PostCommentService;
69
import com.back.global.rq.Rq;
710
import com.back.global.rsData.RsData;
811
import io.swagger.v3.oas.annotations.Operation;
912
import jakarta.validation.Valid;
13+
import jakarta.validation.constraints.Positive;
1014
import lombok.RequiredArgsConstructor;
1115
import org.springframework.beans.factory.annotation.Autowired;
16+
import org.springframework.transaction.annotation.Transactional;
1217
import org.springframework.web.bind.annotation.*;
1318

19+
import java.util.List;
20+
1421
@RestController
1522
@RequestMapping("/post/comment")
1623
@RequiredArgsConstructor
@@ -21,13 +28,44 @@ public class PostCommentController {
2128
private PostCommentService postCommentService;
2229

2330
@Operation(summary = "댓글 생성")
24-
@PostMapping("/{post_id}")
31+
@PostMapping("/post/{post_id}")
2532
public RsData<Void> createComment(@PathVariable Long post_id,
26-
@Valid @RequestBody CommentCreateRequest commentCreateRequest
27-
) {
33+
@Valid @RequestBody CommentCreateRequest commentCreateRequest
34+
) {
2835
Member member = rq.getActor();
2936
postCommentService.createComment(member, post_id, commentCreateRequest);
3037

31-
return new RsData<>("200", "댓글 작성 완료" , null);
38+
return new RsData<>("200", "댓글 작성 완료", null);
39+
}
40+
41+
@Operation(summary = "댓글 다건 조회")
42+
@GetMapping("/post/{post_id}")
43+
@Transactional(readOnly = true)
44+
public RsData<List<CommentAllResponse>> getAllPostComment(@PathVariable Long post_id) {
45+
List<CommentAllResponse> postAllResponse = postCommentService.getAllPostCommentResponse(post_id);
46+
return new RsData<>("200", "게시글 다건 조회 성공", postAllResponse);
47+
}
48+
49+
@Operation(summary = "댓글 삭제")
50+
@DeleteMapping("/post/{post_id}/comment")
51+
public RsData<Void> removePostComment(@PathVariable @Positive Long post_id
52+
, @RequestBody @Valid CommentDeleteRequest commentDeleteRequest) {
53+
Member member = rq.getActor();
54+
55+
postCommentService.removePostComment(post_id, commentDeleteRequest, member);
56+
57+
return new RsData<>("200", "게시글 삭제 성공", null);
3258
}
59+
60+
@Operation(summary = "댓글 수정")
61+
@PutMapping("/post/{post_id}/comment/")
62+
public RsData<Void> updatePostComment(@PathVariable Long post_id
63+
, @Valid @RequestBody CommentModifyRequest commentModifyRequest) {
64+
Member member = rq.getActor();
65+
66+
postCommentService.updatePostComment(post_id, commentModifyRequest, member);
67+
68+
return new RsData<>("200", "댓글 수정 성공", null);
69+
}
70+
3371
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.back.domain.post.comment.dto;
2+
3+
import com.back.domain.post.comment.entity.PostComment;
4+
import lombok.Data;
5+
6+
import java.time.LocalDateTime;
7+
8+
@Data
9+
public class CommentAllResponse {
10+
Long id;
11+
String content;
12+
String authorName;
13+
LocalDateTime createdAt;
14+
15+
public static CommentAllResponse from(PostComment comment) {
16+
CommentAllResponse response = new CommentAllResponse();
17+
response.setId(comment.getId());
18+
response.setContent(comment.getContent());
19+
response.setAuthorName(comment.getMember().getName()); // Member에서 이름 가져오기
20+
response.setCreatedAt(comment.getCreateDate());
21+
return response;
22+
}
23+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.back.domain.post.comment.dto;
22

3+
import jakarta.validation.constraints.NotBlank;
34
import lombok.Data;
45

56
@Data
67
public class CommentCreateRequest {
78
private Long postId;
89
private String role;
10+
@NotBlank(message = "댓글을 입력해주세요")
911
private String comment;
1012
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.back.domain.post.comment.dto;
2+
3+
import jakarta.validation.constraints.NotNull;
4+
import lombok.Data;
5+
6+
@Data
7+
public class CommentDeleteRequest {
8+
@NotNull
9+
private Long CommentId;
10+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.back.domain.post.comment.dto;
2+
3+
import jakarta.validation.constraints.NotBlank;
4+
import jakarta.validation.constraints.NotNull;
5+
import lombok.Data;
6+
7+
@Data
8+
public class CommentModifyRequest {
9+
@NotNull
10+
private Long commentId;
11+
@NotBlank(message = "공백일 수 없습니다.")
12+
private String content;
13+
}

back/src/main/java/com/back/domain/post/comment/service/PostCommentService.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.back.domain.post.comment.service;
22

33
import com.back.domain.member.member.entity.Member;
4+
import com.back.domain.post.comment.dto.CommentAllResponse;
45
import com.back.domain.post.comment.dto.CommentCreateRequest;
6+
import com.back.domain.post.comment.dto.CommentDeleteRequest;
7+
import com.back.domain.post.comment.dto.CommentModifyRequest;
58
import com.back.domain.post.comment.entity.PostComment;
69
import com.back.domain.post.comment.repository.PostCommentRepository;
710
import com.back.domain.post.post.entity.Post;
@@ -11,6 +14,9 @@
1114
import lombok.RequiredArgsConstructor;
1215
import org.springframework.stereotype.Service;
1316

17+
import java.util.List;
18+
import java.util.Objects;
19+
1420
@Service
1521
@RequiredArgsConstructor
1622
public class PostCommentService {
@@ -32,4 +38,45 @@ public void createComment(Member member, Long postId, CommentCreateRequest comme
3238
postCommentRepository.save(postComment);
3339

3440
}
41+
42+
public List<CommentAllResponse> getAllPostCommentResponse(Long postId) {
43+
Post post = postRepository.findById(postId).orElseThrow(() -> new ServiceException("400", "해당 Id의 게시글이 없습니다."));
44+
45+
List<PostComment> listPostComment = post.getComments();
46+
47+
48+
return listPostComment.stream()
49+
.map(CommentAllResponse::from)
50+
.toList();
51+
}
52+
53+
public void removePostComment(Long postId, CommentDeleteRequest commentDeleteRequest, Member member) {
54+
Post post = postRepository.findById(postId).orElseThrow(() -> new ServiceException("400", "해당 Id의 게시글이 없습니다."));
55+
56+
PostComment postComment = postCommentRepository.findById(commentDeleteRequest.getCommentId()).orElseThrow(() -> new ServiceException("400", "해당 Id의 댓글이 없습니다."));
57+
Member author = postComment.getMember();
58+
59+
60+
if(!Objects.equals(member.getId(), author.getId())) {
61+
throw new ServiceException("400", "삭제 권한이 없습니다.");
62+
}
63+
64+
postCommentRepository.delete(postComment);
65+
66+
}
67+
68+
@Transactional
69+
public void updatePostComment(Long postId, CommentModifyRequest commentModifyRequest, Member member) {
70+
Post post = postRepository.findById(postId).orElseThrow(() -> new ServiceException("400", "해당 Id의 게시글이 없습니다."));
71+
72+
PostComment postComment = postCommentRepository.findById(commentModifyRequest.getCommentId()).orElseThrow(() -> new ServiceException("400", "해당 Id의 댓글이 없습니다."));
73+
Member author = postComment.getMember();
74+
75+
76+
if(!Objects.equals(member.getId(), author.getId())) {
77+
throw new ServiceException("400", "수정 권한이 없습니다.");
78+
}
79+
80+
postComment.setContent(commentModifyRequest.getContent());
81+
}
3582
}

back/src/main/java/com/back/domain/post/post/controller/InformationPostController.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.back.domain.post.post.dto.*;
55
import com.back.domain.post.post.entity.Post;
66
import com.back.domain.post.post.service.PostService;
7-
import com.back.global.auth.CurrentUser;
87
import com.back.global.rq.Rq;
98
import com.back.global.rsData.RsData;
109
import io.swagger.v3.oas.annotations.Operation;
@@ -45,7 +44,7 @@ public RsData<List<PostAllResponse>> getAllPost() {
4544

4645
@Operation(summary = "게시글 단건 조회")
4746
@GetMapping("/{post_id}")
48-
public RsData<PostSingleResponse> getSinglePost(@PathVariable long post_id) {
47+
public RsData<PostSingleResponse> getSinglePost(@PathVariable Long post_id) {
4948
Post post = postService.findById(post_id);
5049

5150
PostSingleResponse postSingleResponse = new PostSingleResponse(post);
@@ -55,7 +54,7 @@ public RsData<PostSingleResponse> getSinglePost(@PathVariable long post_id) {
5554

5655
@Operation(summary = "게시글 삭제")
5756
@DeleteMapping("/{post_id}")
58-
public RsData<Void> removePost(@PathVariable long post_id) {
57+
public RsData<Void> removePost(@PathVariable Long post_id) {
5958
Member member = rq.getActor();
6059

6160
postService.removePost(post_id, member);
@@ -65,26 +64,25 @@ public RsData<Void> removePost(@PathVariable long post_id) {
6564

6665
@Operation(summary = "게시글 수정")
6766
@PutMapping("/{post_id}")
68-
public RsData<Void> updatePost(@PathVariable long post_id
69-
,@CurrentUser Member member
67+
public RsData<Void> updatePost(@PathVariable Long post_id
7068
,@Valid @RequestBody PostCreateRequest postCreateRequest) {
71-
69+
Member member = rq.getActor();
7270
postService.updatePost(post_id, member, postCreateRequest);
7371

7472
return new RsData<>("200", "게시글 수정 성공", null);
7573
}
7674

7775
@Operation(summary = "게시글 좋아요 + ")
7876
@PostMapping("/{post_id}/liked")
79-
public RsData<Void> likePost(@PathVariable long post_id) {
77+
public RsData<Void> likePost(@PathVariable Long post_id) {
8078
postService.likePost(post_id);
8179

8280
return new RsData<>("200", "게시글 좋아요 성공", null);
8381
}
8482

8583
@Operation(summary = "게시글 좋아요 (Show)")
8684
@GetMapping("/{post_id}/liked")
87-
public RsData<PostLikedResponse> getlike(@PathVariable long post_id) {
85+
public RsData<PostLikedResponse> getlike(@PathVariable Long post_id) {
8886
int likeCount = postService.showLikeCount(post_id);
8987
PostLikedResponse postLikedResponse = new PostLikedResponse(likeCount);
9088

@@ -93,7 +91,7 @@ public RsData<PostLikedResponse> getlike(@PathVariable long post_id) {
9391

9492
@Operation(summary = "게시글 싫어요")
9593
@PostMapping("/{post_id}/disliked")
96-
public RsData<PostLikedResponse> disLikePost(@PathVariable long post_id) {
94+
public RsData<PostLikedResponse> disLikePost(@PathVariable Long post_id) {
9795
postService.disLikePost(post_id);
9896

9997
return new RsData<>("200", "게시글 싫어요 성공", null);

back/src/main/java/com/back/global/init/PostInitData.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.back.domain.member.member.entity.Member;
44
import com.back.domain.member.member.service.MemberService;
5+
import com.back.domain.post.comment.entity.PostComment;
6+
import com.back.domain.post.comment.repository.PostCommentRepository;
57
import com.back.domain.post.post.entity.Post;
68
import com.back.domain.post.post.repository.PostRepository;
79
import com.back.global.exception.ServiceException;
@@ -20,12 +22,13 @@
2022
public class PostInitData implements ApplicationRunner {
2123
private final PostRepository postRepository;
2224
private final MemberService memberService;
25+
private final PostCommentRepository postCommentRepository;
2326

2427

2528
@Override
2629
public void run(ApplicationArguments args) throws Exception {
2730
log.info("postinit데이터 생성");
28-
initPostData();
31+
initDateForPostDataAndPostCommentData();
2932

3033

3134
log.info("postRepo개수는 " + postRepository.count());
@@ -34,9 +37,8 @@ public void run(ApplicationArguments args) throws Exception {
3437
}
3538

3639

37-
3840
@Transactional
39-
protected void initPostData() {
41+
protected void initDateForPostDataAndPostCommentData() {
4042
if (postRepository.count() > 0) return;
4143

4244
Member member2 = memberService.joinMentee("user2", "사용자1", "nickname1","password123","");
@@ -47,6 +49,16 @@ protected void initPostData() {
4749
createPost("정보글 제목", "정보글 내용", member2, Post.PostType.INFORMATIONPOST);
4850
createPost("연습글 제목", "연습글 내용", member3, Post.PostType.PRACTICEPOST);
4951
createPost("질문글 제목", "질문글 내용", member4, Post.PostType.QUESTIONPOST);
52+
53+
createComment(member2, 1L, "1번댓글");
54+
createComment(member2, 1L, "2댓글");
55+
createComment(member2, 1L, "3번댓글");
56+
createComment(member2, 1L, "4번댓글");
57+
createComment(member2, 1L, "5번댓글");
58+
createComment(member2, 1L, "6번댓글");
59+
createComment(member2, 1L, "7번댓글");
60+
61+
5062
}
5163

5264
private void createPost(String title, String content, Member member, Post.PostType type) {
@@ -62,6 +74,19 @@ private void createPost(String title, String content, Member member, Post.PostTy
6274
postRepository.save(post);
6375
}
6476

77+
public void createComment(Member member, Long postId, String content) {
78+
Post post = postRepository.findById(postId)
79+
.orElseThrow(() -> new ServiceException("400", "해당 Id의 게시글이 없습니다."));
80+
81+
PostComment postComment = new PostComment();
82+
postComment.setContent(content);
83+
postComment.setMember(member);
84+
postComment.setRole(String.valueOf(member.getRole()));
85+
postComment.setPost(post); // 직접 설정
86+
87+
postCommentRepository.save(postComment);
88+
89+
}
6590

6691
private void validPostType(String postTypeStr) {
6792
boolean eq = false;
@@ -75,4 +100,5 @@ private void validPostType(String postTypeStr) {
75100

76101
if(!eq) throw new ServiceException("400-2", "유효하지 않은 PostType입니다.");
77102
}
103+
78104
}

back/src/main/java/com/back/global/security/CustomAuthenticationFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
3737
try {
3838
work(request, response, filterChain);
3939
} catch (Exception e) {
40-
log.error("CustomAuthenticationFilter에서 예외 발생: ",e);
40+
log.error("CustomAuthenticationFilter에서 예외 발생: ",e); //401 에러로 빠지는거 추적 가능
4141
RsData<Void> rsData = new RsData<>("401-1", "인증 오류가 발생했습니다.");
4242
response.setContentType("application/json;charset=UTF-8");
4343
response.setStatus(rsData.statusCode());

0 commit comments

Comments
 (0)