Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package com.back.domain.post.comment.controller;

import com.back.domain.member.member.entity.Member;
import com.back.domain.post.comment.dto.CommentAllResponse;
import com.back.domain.post.comment.dto.CommentCreateRequest;
import com.back.domain.post.comment.dto.CommentDeleteRequest;
import com.back.domain.post.comment.dto.CommentModifyRequest;
import com.back.domain.post.comment.service.PostCommentService;
import com.back.global.rq.Rq;
import com.back.global.rsData.RsData;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Positive;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/post/comment")
@RequiredArgsConstructor
Expand All @@ -21,13 +28,44 @@ public class PostCommentController {
private PostCommentService postCommentService;

@Operation(summary = "댓글 생성")
@PostMapping("/{post_id}")
@PostMapping("/post/{post_id}")
public RsData<Void> createComment(@PathVariable Long post_id,
@Valid @RequestBody CommentCreateRequest commentCreateRequest
) {
@Valid @RequestBody CommentCreateRequest commentCreateRequest
) {
Member member = rq.getActor();
postCommentService.createComment(member, post_id, commentCreateRequest);

return new RsData<>("200", "댓글 작성 완료" , null);
return new RsData<>("200", "댓글 작성 완료", null);
}

@Operation(summary = "댓글 다건 조회")
@GetMapping("/post/{post_id}")
@Transactional(readOnly = true)
public RsData<List<CommentAllResponse>> getAllPostComment(@PathVariable Long post_id) {
List<CommentAllResponse> postAllResponse = postCommentService.getAllPostCommentResponse(post_id);
return new RsData<>("200", "게시글 다건 조회 성공", postAllResponse);
}

@Operation(summary = "댓글 삭제")
@DeleteMapping("/post/{post_id}/comment")
public RsData<Void> removePostComment(@PathVariable @Positive Long post_id
, @RequestBody @Valid CommentDeleteRequest commentDeleteRequest) {
Member member = rq.getActor();

postCommentService.removePostComment(post_id, commentDeleteRequest, member);

return new RsData<>("200", "게시글 삭제 성공", null);
}

@Operation(summary = "댓글 수정")
@PutMapping("/post/{post_id}/comment/")
public RsData<Void> updatePostComment(@PathVariable Long post_id
, @Valid @RequestBody CommentModifyRequest commentModifyRequest) {
Member member = rq.getActor();

postCommentService.updatePostComment(post_id, commentModifyRequest, member);

return new RsData<>("200", "댓글 수정 성공", null);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.back.domain.post.comment.dto;

import com.back.domain.post.comment.entity.PostComment;
import lombok.Data;

import java.time.LocalDateTime;

@Data
public class CommentAllResponse {
Long id;
String content;
String authorName;
LocalDateTime createdAt;

public static CommentAllResponse from(PostComment comment) {
CommentAllResponse response = new CommentAllResponse();
response.setId(comment.getId());
response.setContent(comment.getContent());
response.setAuthorName(comment.getMember().getName()); // Member에서 이름 가져오기
response.setCreatedAt(comment.getCreateDate());
return response;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.back.domain.post.comment.dto;

import jakarta.validation.constraints.NotBlank;
import lombok.Data;

@Data
public class CommentCreateRequest {
private Long postId;
private String role;
@NotBlank(message = "댓글을 입력해주세요")
private String comment;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.back.domain.post.comment.dto;

import jakarta.validation.constraints.NotNull;
import lombok.Data;

@Data
public class CommentDeleteRequest {
@NotNull
private Long CommentId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.back.domain.post.comment.dto;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;

@Data
public class CommentModifyRequest {
@NotNull
private Long commentId;
@NotBlank(message = "공백일 수 없습니다.")
private String content;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.back.domain.post.comment.service;

import com.back.domain.member.member.entity.Member;
import com.back.domain.post.comment.dto.CommentAllResponse;
import com.back.domain.post.comment.dto.CommentCreateRequest;
import com.back.domain.post.comment.dto.CommentDeleteRequest;
import com.back.domain.post.comment.dto.CommentModifyRequest;
import com.back.domain.post.comment.entity.PostComment;
import com.back.domain.post.comment.repository.PostCommentRepository;
import com.back.domain.post.post.entity.Post;
Expand All @@ -11,6 +14,9 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
@RequiredArgsConstructor
public class PostCommentService {
Expand All @@ -32,4 +38,45 @@ public void createComment(Member member, Long postId, CommentCreateRequest comme
postCommentRepository.save(postComment);

}

public List<CommentAllResponse> getAllPostCommentResponse(Long postId) {
Post post = postRepository.findById(postId).orElseThrow(() -> new ServiceException("400", "해당 Id의 게시글이 없습니다."));

List<PostComment> listPostComment = post.getComments();


return listPostComment.stream()
.map(CommentAllResponse::from)
.toList();
}

public void removePostComment(Long postId, CommentDeleteRequest commentDeleteRequest, Member member) {
Post post = postRepository.findById(postId).orElseThrow(() -> new ServiceException("400", "해당 Id의 게시글이 없습니다."));

PostComment postComment = postCommentRepository.findById(commentDeleteRequest.getCommentId()).orElseThrow(() -> new ServiceException("400", "해당 Id의 댓글이 없습니다."));
Member author = postComment.getMember();


if(!Objects.equals(member.getId(), author.getId())) {
throw new ServiceException("400", "삭제 권한이 없습니다.");
}

postCommentRepository.delete(postComment);

}

@Transactional
public void updatePostComment(Long postId, CommentModifyRequest commentModifyRequest, Member member) {
Post post = postRepository.findById(postId).orElseThrow(() -> new ServiceException("400", "해당 Id의 게시글이 없습니다."));

PostComment postComment = postCommentRepository.findById(commentModifyRequest.getCommentId()).orElseThrow(() -> new ServiceException("400", "해당 Id의 댓글이 없습니다."));
Member author = postComment.getMember();


if(!Objects.equals(member.getId(), author.getId())) {
throw new ServiceException("400", "수정 권한이 없습니다.");
}

postComment.setContent(commentModifyRequest.getContent());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.back.domain.post.post.dto.*;
import com.back.domain.post.post.entity.Post;
import com.back.domain.post.post.service.PostService;
import com.back.global.auth.CurrentUser;
import com.back.global.rq.Rq;
import com.back.global.rsData.RsData;
import io.swagger.v3.oas.annotations.Operation;
Expand Down Expand Up @@ -45,7 +44,7 @@ public RsData<List<PostAllResponse>> getAllPost() {

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

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

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

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

@Operation(summary = "게시글 수정")
@PutMapping("/{post_id}")
public RsData<Void> updatePost(@PathVariable long post_id
,@CurrentUser Member member
public RsData<Void> updatePost(@PathVariable Long post_id
,@Valid @RequestBody PostCreateRequest postCreateRequest) {

Member member = rq.getActor();
postService.updatePost(post_id, member, postCreateRequest);

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

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

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

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

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

@Operation(summary = "게시글 싫어요")
@PostMapping("/{post_id}/disliked")
public RsData<PostLikedResponse> disLikePost(@PathVariable long post_id) {
public RsData<PostLikedResponse> disLikePost(@PathVariable Long post_id) {
postService.disLikePost(post_id);

return new RsData<>("200", "게시글 싫어요 성공", null);
Expand Down
32 changes: 29 additions & 3 deletions back/src/main/java/com/back/global/init/PostInitData.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.back.domain.member.member.entity.Member;
import com.back.domain.member.member.service.MemberService;
import com.back.domain.post.comment.entity.PostComment;
import com.back.domain.post.comment.repository.PostCommentRepository;
import com.back.domain.post.post.entity.Post;
import com.back.domain.post.post.repository.PostRepository;
import com.back.global.exception.ServiceException;
Expand All @@ -20,12 +22,13 @@
public class PostInitData implements ApplicationRunner {
private final PostRepository postRepository;
private final MemberService memberService;
private final PostCommentRepository postCommentRepository;


@Override
public void run(ApplicationArguments args) throws Exception {
log.info("postinit데이터 생성");
initPostData();
initDateForPostDataAndPostCommentData();


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



@Transactional
protected void initPostData() {
protected void initDateForPostDataAndPostCommentData() {
if (postRepository.count() > 0) return;

Member member2 = memberService.joinMentee("user2", "사용자1", "nickname1","password123","");
Expand All @@ -47,6 +49,16 @@ protected void initPostData() {
createPost("정보글 제목", "정보글 내용", member2, Post.PostType.INFORMATIONPOST);
createPost("연습글 제목", "연습글 내용", member3, Post.PostType.PRACTICEPOST);
createPost("질문글 제목", "질문글 내용", member4, Post.PostType.QUESTIONPOST);

createComment(member2, 1L, "1번댓글");
createComment(member2, 1L, "2댓글");
createComment(member2, 1L, "3번댓글");
createComment(member2, 1L, "4번댓글");
createComment(member2, 1L, "5번댓글");
createComment(member2, 1L, "6번댓글");
createComment(member2, 1L, "7번댓글");


}

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

public void createComment(Member member, Long postId, String content) {
Post post = postRepository.findById(postId)
.orElseThrow(() -> new ServiceException("400", "해당 Id의 게시글이 없습니다."));

PostComment postComment = new PostComment();
postComment.setContent(content);
postComment.setMember(member);
postComment.setRole(String.valueOf(member.getRole()));
postComment.setPost(post); // 직접 설정

postCommentRepository.save(postComment);

}

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

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

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