From e60df3c80b8d8fb2ff1ee21f6b01f1f76984abda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=84=9D=ED=9D=AC=EC=84=B1?= Date: Tue, 14 Oct 2025 15:44:05 +0900 Subject: [PATCH 1/3] Refactor#179 (#328) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor : createPost메소드의 post엔티티 숨김 * refactor : LikeCount, DisLikeCount 숨김 * refactor : PathVariable 변수명 수정 * refactor : 페이징 처리 테스트 코드 수정 * refactor : PostComment 헬프 메소드 분리 && postRepository 주입 -> postService 주입 * refactor : PostCommentServiceTest 응답값 수정 * postService 헬프 메서드 분리 --- .../controller/PostCommentController.java | 27 ++-- .../comment/service/PostCommentService.java | 121 +++++++++--------- .../post/like/service/PostLikeService.java | 23 ++-- .../post/post/controller/PostController.java | 45 +++---- .../post/post/dto/PostCreateResponse.java | 4 +- .../domain/post/post/service/PostService.java | 31 +++-- .../back/domain/post/rq/PostDetailFacade.java | 7 +- .../comment/PostCommentControllerTest.java | 4 +- .../service/PostCommentServiceTest.java | 53 ++++---- .../post/controller/PostControllerTest.java | 39 +----- .../post/post/service/PostServiceTest.java | 113 ++++++++++------ .../com/back/fixture/Post/PostFixture.java | 55 ++++++++ 12 files changed, 289 insertions(+), 233 deletions(-) create mode 100644 back/src/test/java/com/back/fixture/Post/PostFixture.java diff --git a/back/src/main/java/com/back/domain/post/comment/controller/PostCommentController.java b/back/src/main/java/com/back/domain/post/comment/controller/PostCommentController.java index eeccba0b..99d9c0a0 100644 --- a/back/src/main/java/com/back/domain/post/comment/controller/PostCommentController.java +++ b/back/src/main/java/com/back/domain/post/comment/controller/PostCommentController.java @@ -5,17 +5,12 @@ 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.service.PostCommentService; import com.back.global.rq.Rq; import com.back.global.rsData.RsData; import io.swagger.v3.oas.annotations.Operation; -import io.swagger.v3.oas.annotations.tags.Tag; 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; @@ -30,40 +25,42 @@ public class PostCommentController { @Operation(summary = "댓글 생성", description = "comment는 공백이나 Null이 될 수 없습니다. comment의 글자 수 제한은 없습니다.") @PostMapping("/{post_id}/comment") - public RsData createComment(@PathVariable Long post_id, + public RsData createComment(@PathVariable(name = "post_id") Long postId, @Valid @RequestBody CommentCreateRequest commentCreateRequest ) { Member member = rq.getActor(); - postCommentService.createComment(member, post_id, commentCreateRequest); + postCommentService.createComment(member, postId, commentCreateRequest); return new RsData<>("200", "댓글 작성 완료", null); } @Operation(summary = "댓글 다건 조회") @GetMapping("/post/{post_id}") - public RsData> getAllPostComment(@PathVariable Long post_id) { - List postAllResponse = postCommentService.getAllPostCommentResponse(post_id); + public RsData> getAllPostComment(@PathVariable(name = "post_id") Long postId) { + List postAllResponse = postCommentService.getAllPostCommentResponse(postId); return new RsData<>("200", "댓글 조회 성공", postAllResponse); } @Operation(summary = "댓글 삭제", description = "commentId는 공백이나 Null이 될 수 없습니다.") @DeleteMapping("/{post_id}/comment") - public RsData removePostComment(@PathVariable @Positive Long post_id + public RsData removePostComment(@PathVariable(name = "post_id") Long postId + , @RequestBody @Valid CommentDeleteRequest commentDeleteRequest) { Member member = rq.getActor(); - postCommentService.removePostComment(post_id, commentDeleteRequest, member); + postCommentService.removePostComment(postId, commentDeleteRequest, member); return new RsData<>("200", "댓글 삭제 성공", null); } @Operation(summary = "댓글 수정", description = "commentId, content는 공백이나 Null이 될 수 없습니다. content의 글자 수 제한은 없습니다. ") @PutMapping("/{post_id}/comment") - public RsData updatePostComment(@PathVariable Long post_id + public RsData updatePostComment(@PathVariable(name = "post_id") Long postId + , @Valid @RequestBody CommentModifyRequest commentModifyRequest) { Member member = rq.getActor(); - postCommentService.updatePostComment(post_id, commentModifyRequest, member); + postCommentService.updatePostComment(postId, commentModifyRequest, member); return new RsData<>("200", "댓글 수정 성공", null); } @@ -77,8 +74,8 @@ public RsData adoptComment(@PathVariable Long commentId) { @Operation(summary = "채택된 댓글 가져오기 ") @GetMapping("isAdopted/{post_id}") - public RsData getAdoptComment(@PathVariable Long post_id) { - CommentAllResponse commentAllResponse = postCommentService.getAdoptedComment(post_id); + public RsData getAdoptComment(@PathVariable(name = "post_id") Long postId) { + CommentAllResponse commentAllResponse = postCommentService.getAdoptedComment(postId); return new RsData<>("200", "채택된 댓글 조회 성공", commentAllResponse); } diff --git a/back/src/main/java/com/back/domain/post/comment/service/PostCommentService.java b/back/src/main/java/com/back/domain/post/comment/service/PostCommentService.java index fddecd73..d8c736bb 100644 --- a/back/src/main/java/com/back/domain/post/comment/service/PostCommentService.java +++ b/back/src/main/java/com/back/domain/post/comment/service/PostCommentService.java @@ -8,7 +8,7 @@ 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.domain.post.post.service.PostService; import com.back.global.exception.ServiceException; import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; @@ -20,16 +20,13 @@ @Service @RequiredArgsConstructor public class PostCommentService { - private final PostRepository postRepository; + private final PostService postService; private final PostCommentRepository postCommentRepository; @Transactional public void createComment(Member member, Long postId, CommentCreateRequest commentCreateRequest) { - Post post = postRepository.findById(postId).orElseThrow(() -> new ServiceException("400", "해당 Id의 게시글이 없습니다.")); - - if ( commentCreateRequest.comment() == null || commentCreateRequest.comment().isEmpty()) { - throw new ServiceException("400", "댓글은 비어 있을 수 없습니다."); - } + validateComment(commentCreateRequest.comment()); + Post post = postService.findPostById(postId); PostComment postComment = PostComment.builder() .post(post) @@ -49,7 +46,6 @@ public List getAllPostCommentResponse(Long postId) { validatePostExists(postId); List listPostComment = postCommentRepository.findCommentsWithMemberByPostId(postId); - return listPostComment.stream() .map(CommentAllResponse::from) .toList(); @@ -57,103 +53,106 @@ public List getAllPostCommentResponse(Long postId) { @Transactional public void removePostComment(Long postId, CommentDeleteRequest commentDeleteRequest, Member member) { - validatePostExists(postId); - - PostComment postComment = getPostCommentById(commentDeleteRequest.commentId()); + PostComment postComment = findCommentById(commentDeleteRequest.commentId()); Member author = postComment.getMember(); - - if(!Objects.equals(member.getId(), author.getId())) { - throw new ServiceException("400", "삭제 권한이 없습니다."); - } - -// if(postComment.getIsAdopted()) { -// throw new ServiceException("400", "채택된 댓글은 삭제할 수 없습니다."); -// } + validateAuthorized(member,author); + validatePostExists(postId); postCommentRepository.delete(postComment); - } @Transactional public void updatePostComment(Long postId, CommentModifyRequest commentModifyRequest, Member member) { validatePostExists(postId); - PostComment postComment = getPostCommentById(commentModifyRequest.commentId()); + PostComment postComment = findCommentById(commentModifyRequest.commentId()); Member author = postComment.getMember(); - - if(!Objects.equals(member.getId(), author.getId())) { - throw new ServiceException("400", "수정 권한이 없습니다."); - } - - if ( commentModifyRequest.content() == null || commentModifyRequest.content().isEmpty()) { - throw new ServiceException("400", "댓글은 비어 있을 수 없습니다."); - } + validateAuthorized(member, author); + validateComment(commentModifyRequest.content()); postComment.updateContent(commentModifyRequest.content()); } + @Transactional + public void adoptComment(Long commentId, Member member) { + PostComment postComment = findCommentById(commentId); + Post post = postComment.getPost(); - private void validatePostExists(Long postId) { - if(postId == null || postId <= 0) { - throw new ServiceException("400", "유효하지 않은 게시글 Id입니다."); - } + validateIsPostAuthor(post, member); + validatePostType(post); + validateAlreadyAdoptedComment(postComment); + validateAlreadyExistsAdoptedComment(post); - if (!postRepository.existsById(postId)) { - throw new ServiceException("400", "해당 Id의 게시글이 없습니다."); - } + postComment.adoptComment(); + post.updateResolveStatus(true); + } + @Transactional + public CommentAllResponse getAdoptedComment(Long postId) { + Post post = postService.findPostById(postId); - } + validatePostType(post); - private PostComment getPostCommentById(Long commentId) { - return postCommentRepository.findById(commentId).orElseThrow(() -> new ServiceException("400", "해당 Id의 댓글이 없습니다.")); + PostComment postComment = validateAdoptedComment(post); + return CommentAllResponse.from(postComment); } - @Transactional - public void adoptComment(Long commentId, Member member) { - PostComment postComment = postCommentRepository.findById(commentId) - .orElseThrow(() -> new ServiceException("400", "해당 Id의 댓글이 없습니다.")); - Post post = postComment.getPost(); + + + // ========== 헬퍼 메소드들 ========== // + private void validateIsPostAuthor(Post post, Member member){ if (!post.isAuthor(member)) { throw new ServiceException("400", "채택 권한이 없습니다."); } + } - if (post.getPostType() != Post.PostType.QUESTIONPOST) { - throw new ServiceException("400", "질문 게시글에만 댓글 채택이 가능합니다."); - } - + private void validateAlreadyAdoptedComment(PostComment postComment){ if (postComment.getIsAdopted()) { throw new ServiceException("400", "이미 채택된 댓글입니다."); } + } - // 이미 채택된 댓글이 있는지 확인 - boolean alreadyAdopted = postCommentRepository.existsByPostAndIsAdoptedTrue(post); - if (alreadyAdopted) { + private void validateAlreadyExistsAdoptedComment(Post post) { + if (postCommentRepository.existsByPostAndIsAdoptedTrue(post)) { throw new ServiceException("400", "이미 채택된 댓글이 있습니다."); } + } - postComment.adoptComment(); - - post.updateResolveStatus(true); + private void validateAuthorized(Member member, Member author) { + if(!Objects.equals(member.getId(), author.getId())) { + throw new ServiceException("400", "변경 권한이 없습니다."); + } } - @Transactional - public CommentAllResponse getAdoptedComment(Long postId) { - Post post = postRepository.findById(postId) - .orElseThrow(() -> new ServiceException("400", "해당 Id의 게시글이 없습니다.")); + private PostComment validateAdoptedComment(Post post) { + return postCommentRepository.findByPostAndIsAdoptedTrue(post) + .orElseThrow(() -> new ServiceException("400", "채택된 댓글이 없습니다.")); + } + private void validatePostType(Post post) { if (post.getPostType() != Post.PostType.QUESTIONPOST) { throw new ServiceException("400", "질문 게시글만 채택된 댓글을 가질 수 있습니다."); } + } - PostComment postComment = postCommentRepository.findByPostAndIsAdoptedTrue(post) - .orElseThrow(() -> new ServiceException("400", "채택된 댓글이 없습니다.")); + private void validateComment(String comment) { + if (comment == null || comment.isEmpty()) { + throw new ServiceException("400", "댓글은 비어 있을 수 없습니다."); + } + } - return CommentAllResponse.from(postComment); + private void validatePostExists(Long postId) { + if (!postService.existsById(postId)) { + throw new ServiceException("400", "유효하지 않은 게시글 Id입니다."); + } + } + + private PostComment findCommentById(Long commentId) { + return postCommentRepository.findById(commentId).orElseThrow(() -> new ServiceException("400", "해당 Id의 댓글이 없습니다.")); } } diff --git a/back/src/main/java/com/back/domain/post/like/service/PostLikeService.java b/back/src/main/java/com/back/domain/post/like/service/PostLikeService.java index f2fc42d1..3fe14b2b 100644 --- a/back/src/main/java/com/back/domain/post/like/service/PostLikeService.java +++ b/back/src/main/java/com/back/domain/post/like/service/PostLikeService.java @@ -3,13 +3,12 @@ import com.back.domain.member.member.entity.Member; import com.back.domain.post.like.entity.PostLike; import com.back.domain.post.like.repository.PostLikeRepository; +import com.back.domain.post.post.dto.PostLikedResponse; import com.back.domain.post.post.entity.Post; import com.back.domain.post.post.service.PostService; -import com.back.domain.post.rq.PostDetailFacade; import com.back.global.rq.Rq; import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; -import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import java.util.Optional; @@ -18,13 +17,12 @@ public class PostLikeService { private final Rq rq; private final PostLikeRepository postLikeRepository; - //private final PostDetailFacade postDetailFacade; private final PostService postService; // 리팩토링 대상 @Transactional public void likePost(long postId) { Member member = rq.getActor(); - Post post = postService.findById(postId); + Post post = postService.findPostById(postId); // 기존 좋아요/싫어요 기록 조회 Optional existingLike = postLikeRepository.findByMemberAndPost(member, post); @@ -47,10 +45,15 @@ public void likePost(long postId) { } } + public PostLikedResponse getLikeCount(Long postId) { + int likeCount = postLikeRepository.countLikesByPostId(postId); + return new PostLikedResponse(likeCount); + } + @Transactional public void disLikePost(long postId) { Member member = rq.getActor(); - Post post = postService.findById(postId); + Post post = postService.findPostById(postId); // 기존 좋아요/싫어요 기록 조회 Optional existingLike = postLikeRepository.findByMemberAndPost(member, post); @@ -72,17 +75,15 @@ public void disLikePost(long postId) { } } - public int getDisLikeCount(Long postId) { - return postLikeRepository.countDislikesByPostId(postId); + public PostLikedResponse getDisLikeCount(Long postId) { + int disLikeCount = postLikeRepository.countDislikesByPostId(postId); + return new PostLikedResponse(disLikeCount); } - public int getLikeCount(Long postId) { - return postLikeRepository.countLikesByPostId(postId); - } public String getPresentStatus(Long postId) { Member member = rq.getActor(); - Post post = postService.findById(postId); + Post post = postService.findPostById(postId); Optional existingLike = postLikeRepository.findByMemberAndPost(member, post); diff --git a/back/src/main/java/com/back/domain/post/post/controller/PostController.java b/back/src/main/java/com/back/domain/post/post/controller/PostController.java index b5e9c4d2..8a00b2c9 100644 --- a/back/src/main/java/com/back/domain/post/post/controller/PostController.java +++ b/back/src/main/java/com/back/domain/post/post/controller/PostController.java @@ -50,8 +50,8 @@ public RsData createPost( @Valid @RequestBody PostCreateRequest postCreateRequest ) { Member member = rq.getActor(); - Post post = postService.createPost(postCreateRequest, member); - PostCreateResponse postCreateResponse = PostCreateResponse.from(post); + + PostCreateResponse postCreateResponse = postService.createPost(postCreateRequest, member); return new RsData<>("200", "게시글이 성공적으로 생성되었습니다.", postCreateResponse); } @@ -67,8 +67,8 @@ public RsData> getAllPost() { @Operation(summary = "게시글 단건 조회", description = "백오피스에서 필요시 사용, content 내용 없음. 상세페이지 조회는 /post/detail/{post_id} 사용") @GetMapping("/{post_id}") - public RsData getSinglePost(@PathVariable Long post_id) { - PostSingleResponse postSingleResponse = postService.makePostSingleResponse(post_id); + public RsData getSinglePost(@PathVariable(name = "post_id") Long postId) { + PostSingleResponse postSingleResponse = postService.makePostSingleResponse(postId); @@ -77,53 +77,46 @@ public RsData getSinglePost(@PathVariable Long post_id) { @Operation(summary = "게시글 삭제") @DeleteMapping("/{post_id}") - public RsData removePost(@PathVariable Long post_id) { + public RsData removePost(@PathVariable(name = "post_id") Long postId) { Member member = rq.getActor(); - postService.removePost(post_id, member); + postService.removePost(postId, member); return new RsData<>("200", "게시글 삭제 성공", null); } @Operation(summary = "게시글 수정", description = "title, content은 공백이거나 null 일 수 없습니다. 글자수 제한은 없습니다. ") @PutMapping("/{post_id}") - public RsData updatePost(@PathVariable Long post_id - ,@Valid @RequestBody PostModifyRequest postModifyRequest) { + public RsData updatePost( + @PathVariable(name = "post_id") Long postId, + @Valid @RequestBody PostModifyRequest postModifyRequest) { Member member = rq.getActor(); - postService.updatePost(post_id, member, postModifyRequest); + postService.updatePost(postId, member, postModifyRequest); return new RsData<>("200", "게시글 수정 성공", null); } @Operation(summary = "게시글 좋아요") @PostMapping("/{post_id}/liked") - public RsData likePost(@PathVariable Long post_id) { - postLikeService.likePost(post_id); - - int likeCount = postLikeService.getLikeCount(post_id); - PostLikedResponse postLikedResponse = new PostLikedResponse(likeCount); - + public RsData likePost(@PathVariable(name = "post_id") Long postId) { + postLikeService.likePost(postId); + PostLikedResponse postLikedResponse = postLikeService.getLikeCount(postId); return new RsData<>("200", "게시글 좋아요 성공", postLikedResponse); } @Operation(summary = "게시글 싫어요") @PostMapping("/{post_id}/disliked") - public RsData disLikePost(@PathVariable Long post_id) { - postLikeService.disLikePost(post_id); - - int likeCount = postLikeService.getDisLikeCount(post_id); - PostDisLikedResponse postdisLikedResponse = new PostDisLikedResponse(likeCount); - - return new RsData<>("200", "게시글 싫어요 성공", postdisLikedResponse); + public RsData disLikePost(@PathVariable(name = "post_id") Long postId) { + postLikeService.disLikePost(postId); + PostLikedResponse postLikedResponse = postLikeService.getDisLikeCount(postId); + return new RsData<>("200", "게시글 싫어요 성공", postLikedResponse); } @Operation(summary = "게시글 상세페이지", description = "사용자 단건 조회시 사용") @GetMapping("/detail/{post_id}") - public RsData getPostDetail(@PathVariable Long post_id) { - - PostDetailResponse response = postDetailFacade.getDetailWithViewIncrement(post_id); - + public RsData getPostDetail(@PathVariable(name = "post_id") Long postId) { + PostDetailResponse response = postDetailFacade.getDetailWithViewIncrement(postId); return new RsData<>("200", "게시글 상세 조회 성공", response); } } diff --git a/back/src/main/java/com/back/domain/post/post/dto/PostCreateResponse.java b/back/src/main/java/com/back/domain/post/post/dto/PostCreateResponse.java index 5f52c457..e42b854d 100644 --- a/back/src/main/java/com/back/domain/post/post/dto/PostCreateResponse.java +++ b/back/src/main/java/com/back/domain/post/post/dto/PostCreateResponse.java @@ -10,6 +10,8 @@ public record PostCreateResponse( public static PostCreateResponse from(Post post) { - return new PostCreateResponse(post.getId(), post.getTitle()); + return new PostCreateResponse( + post.getId(), + post.getTitle()); } } diff --git a/back/src/main/java/com/back/domain/post/post/service/PostService.java b/back/src/main/java/com/back/domain/post/post/service/PostService.java index e10c0c02..294395b3 100644 --- a/back/src/main/java/com/back/domain/post/post/service/PostService.java +++ b/back/src/main/java/com/back/domain/post/post/service/PostService.java @@ -30,7 +30,7 @@ public List getAllPosts() { } @Transactional - public Post createPost(PostCreateRequest postCreateRequest, Member member) { + public PostCreateResponse createPost(PostCreateRequest postCreateRequest, Member member) { String postTypeStr = postCreateRequest.postType(); Post.validPostType(postTypeStr); Post.PostType postType = Post.PostType.valueOf(postTypeStr); @@ -59,22 +59,22 @@ public Post createPost(PostCreateRequest postCreateRequest, Member member) { postRepository.save(post); - return post; + return PostCreateResponse.from(post); } @Transactional public void removePost(Long postId, Member member) { - Post post = findById(postId); - if (!post.isAuthor(member)) throw new ServiceException("400", "삭제 권한이 없습니다."); + Post post = findPostById(postId); + validateModifyAuthorization(post, member); postRepository.delete(post); } @Transactional public void updatePost(long postId, Member member, @Valid PostModifyRequest postModifyRequest) { - Post post = findById(postId); - if (!post.isAuthor(member)) throw new ServiceException("400", "수정 권한이 없습니다."); + Post post = findPostById(postId); + validateModifyAuthorization(post, member); if ( postModifyRequest.title() == null || postModifyRequest.title().isBlank()) { throw new ServiceException("400", "제목을 입력해주세요."); @@ -104,14 +104,9 @@ public Page getPosts(String keyword, int page, int size ,Post.PostType return postRepository.searchPosts(keyword, pageable, postType).map(PostDto::from); } - public Post findById(Long postId) { - Post post = postRepository.findById(postId).orElseThrow(() -> new ServiceException("400", "해당 Id의 게시글이 없습니다.")); - return post; - } - @Transactional public PostSingleResponse makePostSingleResponse(Long postId) { - Post post = postRepository.findById(postId).orElseThrow(() -> new ServiceException("400", "해당 Id의 게시글이 없습니다.")); + Post post = findPostById(postId); PostSingleResponse postSingleResponse = PostSingleResponse.from(post); return postSingleResponse; @@ -123,4 +118,16 @@ public List getAllPostResponse() { .toList(); } + public boolean existsById(Long postId) { + return postRepository.existsById(postId); + } + + public Post findPostById(Long postId) { + return postRepository.findById(postId).orElseThrow(() -> new ServiceException("400", "해당 Id의 게시글이 없습니다.")); + } + + private void validateModifyAuthorization(Post post, Member member) { + if (!post.isAuthor(member)) throw new ServiceException("400", "변경 권한이 없습니다."); + } + } diff --git a/back/src/main/java/com/back/domain/post/rq/PostDetailFacade.java b/back/src/main/java/com/back/domain/post/rq/PostDetailFacade.java index 11b67110..aa3c628b 100644 --- a/back/src/main/java/com/back/domain/post/rq/PostDetailFacade.java +++ b/back/src/main/java/com/back/domain/post/rq/PostDetailFacade.java @@ -4,6 +4,7 @@ import com.back.domain.post.comment.service.PostCommentService; import com.back.domain.post.like.service.PostLikeService; import com.back.domain.post.post.dto.PostDetailResponse; +import com.back.domain.post.post.dto.PostLikedResponse; import com.back.domain.post.post.entity.Post; import com.back.domain.post.post.service.PostService; import lombok.RequiredArgsConstructor; @@ -26,11 +27,11 @@ public PostDetailResponse getDetailWithViewIncrement(Long postId) { Post post = postService.getPostDetailWithViewIncrement(postId); List comments = postCommentService.getAllPostCommentResponse(postId); - int likeCount = postLikeService.getLikeCount(postId); - int dislikeCount = postLikeService.getDisLikeCount(postId); + PostLikedResponse postLikedResponse = postLikeService.getLikeCount(postId); + PostLikedResponse postDisLikedResponse = postLikeService.getDisLikeCount(postId); String userStatus = postLikeService.getPresentStatus(postId); - return PostDetailResponse.from(post, comments, likeCount, dislikeCount, userStatus); + return PostDetailResponse.from(post, comments, postLikedResponse.likeCount(), postDisLikedResponse.likeCount(), userStatus); } diff --git a/back/src/test/java/com/back/domain/post/comment/PostCommentControllerTest.java b/back/src/test/java/com/back/domain/post/comment/PostCommentControllerTest.java index 9382cd4e..df23d273 100644 --- a/back/src/test/java/com/back/domain/post/comment/PostCommentControllerTest.java +++ b/back/src/test/java/com/back/domain/post/comment/PostCommentControllerTest.java @@ -209,7 +209,7 @@ void t5() throws Exception { .andExpect(handler().handlerType(PostCommentController.class)) .andExpect(handler().methodName("removePostComment")) .andExpect(jsonPath("$.resultCode").value("400")) - .andExpect(jsonPath("$.msg").value("삭제 권한이 없습니다.")); + .andExpect(jsonPath("$.msg").value("변경 권한이 없습니다.")); } @Test @@ -270,7 +270,7 @@ void t7() throws Exception { .andExpect(handler().handlerType(PostCommentController.class)) .andExpect(handler().methodName("updatePostComment")) .andExpect(jsonPath("$.resultCode").value("400")) - .andExpect(jsonPath("$.msg").value("수정 권한이 없습니다.")); + .andExpect(jsonPath("$.msg").value("변경 권한이 없습니다.")); } @Test diff --git a/back/src/test/java/com/back/domain/post/comment/service/PostCommentServiceTest.java b/back/src/test/java/com/back/domain/post/comment/service/PostCommentServiceTest.java index 7a478265..5c3cb711 100644 --- a/back/src/test/java/com/back/domain/post/comment/service/PostCommentServiceTest.java +++ b/back/src/test/java/com/back/domain/post/comment/service/PostCommentServiceTest.java @@ -8,8 +8,9 @@ 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.domain.post.post.service.PostService; import com.back.fixture.MemberFixture; +import com.back.fixture.Post.PostFixture; import com.back.global.exception.ServiceException; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; @@ -31,7 +32,7 @@ class PostCommentServiceTest { @Mock - private PostRepository postRepository; + private PostService postService; @Mock private PostCommentRepository postCommentRepository; @@ -48,18 +49,19 @@ class CreateCommentTest { void createComment_success() { // given Member member = MemberFixture.create(1L, "user@test.com", "User", "password", Member.Role.MENTEE); - Post post = createDefaultPost(member); - Long postId = 1L; + Post post = PostFixture.createInformationPost(member,1L); CommentCreateRequest request = new CommentCreateRequest("테스트 댓글"); - when(postRepository.findById(postId)).thenReturn(Optional.of(post)); - when(postCommentRepository.save(any(PostComment.class))).thenReturn(any(PostComment.class)); + PostComment postComment = createComment(member, post, request.comment()); + + when(postService.findPostById(1L)).thenReturn(post); + when(postCommentRepository.save(any(PostComment.class))).thenReturn(postComment); // when - postCommentService.createComment(member, postId, request); + postCommentService.createComment(member, 1L, request); // then - verify(postRepository).findById(postId); + verify(postService).findPostById(1L); verify(postCommentRepository).save(any(PostComment.class)); } @@ -71,8 +73,7 @@ void createComment_postNotExists_failure() { Long postId = 999L; CommentCreateRequest request = new CommentCreateRequest("테스트 댓글"); - when(postRepository.findById(postId)).thenReturn(Optional.empty()); - + when(postService.findPostById(postId)).thenThrow(new ServiceException("400", "해당 Id의 게시글이 없습니다.")); // when & then assertThatThrownBy(() -> postCommentService.createComment(member, postId, request)) .isInstanceOf(ServiceException.class) @@ -100,7 +101,7 @@ void getAllPostCommentResponse_success() { createComment(member2, post, "두 번째 댓글") ); - when(postRepository.existsById(postId)).thenReturn(true); + when(postService.existsById(postId)).thenReturn(true); when(postCommentRepository.findCommentsWithMemberByPostId(postId)).thenReturn(comments); // when @@ -108,7 +109,7 @@ void getAllPostCommentResponse_success() { // then assertThat(result).hasSize(2); - verify(postRepository).existsById(postId); + verify(postService).existsById(postId); verify(postCommentRepository).findCommentsWithMemberByPostId(postId); } @@ -118,12 +119,12 @@ void getAllPostCommentResponse_postNotExists_failure() { // given Long postId = 999L; - when(postRepository.existsById(postId)).thenReturn(false); + when(postService.existsById(postId)).thenReturn(false); // when & then assertThatThrownBy(() -> postCommentService.getAllPostCommentResponse(postId)) .isInstanceOf(ServiceException.class) - .hasMessage("400 : 해당 Id의 게시글이 없습니다."); + .hasMessage("400 : 유효하지 않은 게시글 Id입니다."); verify(postCommentRepository, never()).findCommentsWithMemberByPostId(anyLong()); } @@ -145,7 +146,7 @@ void removePostComment_author_success() { CommentDeleteRequest request = new CommentDeleteRequest(commentId); - when(postRepository.existsById(postId)).thenReturn(true); + when(postService.existsById(postId)).thenReturn(true); when(postCommentRepository.findById(commentId)).thenReturn(Optional.of(comment)); // when @@ -167,14 +168,12 @@ void removePostComment_notAuthor_failure() { Long commentId = 1L; CommentDeleteRequest request = new CommentDeleteRequest(commentId); - - when(postRepository.existsById(postId)).thenReturn(true); when(postCommentRepository.findById(commentId)).thenReturn(Optional.of(comment)); // when & then assertThatThrownBy(() -> postCommentService.removePostComment(postId, request, otherUser)) .isInstanceOf(ServiceException.class) - .hasMessage("400 : 삭제 권한이 없습니다."); + .hasMessage("400 : 변경 권한이 없습니다."); verify(postCommentRepository, never()).delete(any(PostComment.class)); } @@ -184,11 +183,11 @@ void removePostComment_notAuthor_failure() { void removePostComment_commentNotExists_failure() { // given Member member = MemberFixture.createDefault(); + Long postId = 1L; Long commentId = 999L; CommentDeleteRequest request = new CommentDeleteRequest(commentId); - when(postRepository.existsById(postId)).thenReturn(true); when(postCommentRepository.findById(commentId)).thenReturn(Optional.empty()); // when & then @@ -215,7 +214,7 @@ void updatePostComment_author_success() { Long commentId = 1L; CommentModifyRequest request = new CommentModifyRequest(commentId,"수정된 댓글"); - when(postRepository.existsById(postId)).thenReturn(true); + when(postService.existsById(postId)).thenReturn(true); when(postCommentRepository.findById(commentId)).thenReturn(Optional.of(comment)); // when @@ -238,13 +237,13 @@ void updatePostComment_notAuthor_failure() { CommentModifyRequest request = new CommentModifyRequest(commentId, "400 : 수정된 댓글"); - when(postRepository.existsById(postId)).thenReturn(true); + when(postService.existsById(postId)).thenReturn(true); when(postCommentRepository.findById(commentId)).thenReturn(Optional.of(comment)); // when & then assertThatThrownBy(() -> postCommentService.updatePostComment(postId, request, otherUser)) .isInstanceOf(ServiceException.class) - .hasMessage("400 : 수정 권한이 없습니다."); + .hasMessage("400 : 변경 권한이 없습니다."); } @Test @@ -260,7 +259,7 @@ void updatePostComment_emptyContent_failure() { CommentModifyRequest request = new CommentModifyRequest(commentId, ""); - when(postRepository.existsById(postId)).thenReturn(true); + when(postService.existsById(postId)).thenReturn(true); when(postCommentRepository.findById(commentId)).thenReturn(Optional.of(comment)); // when & then @@ -274,13 +273,15 @@ void updatePostComment_emptyContent_failure() { void updatePostComment_nullContent_failure() { // given Member author = MemberFixture.create(1L, "author@test.com", "Author", "password", Member.Role.MENTEE); - Post post = createDefaultPost(author); + Post post = PostFixture.createInformationPost(author,1L); + + PostComment comment = createComment(author, post, "원본 댓글"); Long postId = 1L; Long commentId = 1L; CommentModifyRequest request = new CommentModifyRequest(commentId, null); - when(postRepository.existsById(postId)).thenReturn(true); + when(postService.existsById(postId)).thenReturn(true); when(postCommentRepository.findById(commentId)).thenReturn(Optional.of(comment)); // when & then @@ -349,7 +350,7 @@ void adoptComment_notQuestionPost_failure() { // when & then assertThatThrownBy(() -> postCommentService.adoptComment(commentId, postAuthor)) .isInstanceOf(ServiceException.class) - .hasMessage("400 : 질문 게시글에만 댓글 채택이 가능합니다."); + .hasMessage("400 : 질문 게시글만 채택된 댓글을 가질 수 있습니다."); } @Test diff --git a/back/src/test/java/com/back/domain/post/post/controller/PostControllerTest.java b/back/src/test/java/com/back/domain/post/post/controller/PostControllerTest.java index cf3278ed..70b82fc5 100644 --- a/back/src/test/java/com/back/domain/post/post/controller/PostControllerTest.java +++ b/back/src/test/java/com/back/domain/post/post/controller/PostControllerTest.java @@ -89,7 +89,7 @@ void t1() throws Exception { .andDo(print()); // 실제 생성된 게시글 조회 (실제 DB에서) - Post createdPost = postService.findById(4L); + Post createdPost = postService.findPostById(4L); resultActions .andExpect(handler().handlerType(PostController.class)) @@ -336,7 +336,7 @@ void t8() throws Exception { .andExpect(handler().handlerType(PostController.class)) .andExpect(handler().methodName("removePost")) .andExpect(jsonPath("$.resultCode").value("400")) - .andExpect(jsonPath("$.msg").value("삭제 권한이 없습니다.")); + .andExpect(jsonPath("$.msg").value("변경 권한이 없습니다.")); } @Test @@ -396,7 +396,7 @@ void t10() throws Exception { .andExpect(handler().handlerType(PostController.class)) .andExpect(handler().methodName("updatePost")) .andExpect(jsonPath("$.resultCode").value("400")) - .andExpect(jsonPath("$.msg").value("수정 권한이 없습니다.")); + .andExpect(jsonPath("$.msg").value("변경 권한이 없습니다.")); } @Test @@ -451,23 +451,6 @@ void t12() throws Exception { .andExpect(jsonPath("$.msg").value("게시글 좋아요 성공")); } -// @Test -// @DisplayName("게시글 좋아요 조회") -// void t13() throws Exception { -// ResultActions resultActions = mvc -// .perform( -// get("/post/{post_id}/liked", 1L) -// ) -// .andDo(print()); -// -// resultActions -// .andExpect(handler().handlerType(PostController.class)) -// .andExpect(handler().methodName("getLike")) -// .andExpect(jsonPath("$.resultCode").value("200")) -// .andExpect(jsonPath("$.msg").value("게시글 좋아요 조회 성공")) -// .andExpect(jsonPath("$.data.likeCount").exists()); -// } - @Test @DisplayName("게시글 싫어요 성공") void t14() throws Exception { @@ -484,22 +467,6 @@ void t14() throws Exception { .andExpect(jsonPath("$.msg").value("게시글 싫어요 성공")); } -// @Test -// @DisplayName("게시글 싫어요 조회") -// void t15() throws Exception { -// ResultActions resultActions = mvc -// .perform( -// get("/post/{post_id}/disliked", 1L) -// ) -// .andDo(print()); -// -// resultActions -// .andExpect(handler().handlerType(PostController.class)) -// .andExpect(handler().methodName("getDisLike")) -// .andExpect(jsonPath("$.resultCode").value("200")) -// .andExpect(jsonPath("$.msg").value("게시글 싫어요 조회 성공")) -// .andExpect(jsonPath("$.data.likeCount").exists()); -// } @Test @DisplayName("좋아요 -> 싫어요 토글 테스트") diff --git a/back/src/test/java/com/back/domain/post/post/service/PostServiceTest.java b/back/src/test/java/com/back/domain/post/post/service/PostServiceTest.java index 6535c331..f1cb0e3d 100644 --- a/back/src/test/java/com/back/domain/post/post/service/PostServiceTest.java +++ b/back/src/test/java/com/back/domain/post/post/service/PostServiceTest.java @@ -12,6 +12,7 @@ import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @@ -48,42 +49,48 @@ void createPost_informationPost_success() { Member member = MemberFixture.create(1L, "test@test.com", "Test User", "password", Member.Role.MENTEE); PostCreateRequest request = new PostCreateRequest("INFORMATIONPOST","제목","내용",""); + // when + postService.createPost(request, member); - Post expectedPost = createPost("제목", "내용", member, Post.PostType.INFORMATIONPOST); - - when(postRepository.save(any(Post.class))).thenReturn(expectedPost); + ArgumentCaptor captor = ArgumentCaptor.forClass(Post.class); + verify(postRepository).save(captor.capture()); - // when - Post result = postService.createPost(request, member); + Post savedPost = captor.getValue(); // then - assertThat(result.getTitle()).isEqualTo("제목"); - assertThat(result.getContent()).isEqualTo("내용"); - assertThat(result.getMember()).isEqualTo(member); - assertThat(result.getPostType()).isEqualTo(Post.PostType.INFORMATIONPOST); - verify(postRepository).save(any(Post.class)); + assertThat(savedPost.getPostType()).isEqualTo(Post.PostType.INFORMATIONPOST); + assertThat(savedPost.getTitle()).isEqualTo("제목"); + assertThat(savedPost.getContent()).isEqualTo("내용"); + assertThat(savedPost.getJob()).isNull(); + assertThat(savedPost.getMember()).isEqualTo(member); } @Test - @DisplayName("멘토가 실무 경험 공유 게시글 생성 성공") + @DisplayName("실무 경험 공유 게시글 생성 성공 - 멘토") void createPost_practicePost_mentor_success() { // given Member mentor = MemberFixture.create(1L, "mentor@test.com", "Mentor", "password", Member.Role.MENTOR); - PostCreateRequest request = new PostCreateRequest("PRACTICEPOST","실무경험","실무내용","123"); - Post expectedPost = createPost("실무 경험", "실무 내용", mentor, Post.PostType.PRACTICEPOST); - - when(postRepository.save(any(Post.class))).thenReturn(expectedPost); + PostCreateRequest request = new PostCreateRequest("PRACTICEPOST","실무경험","실무내용","백엔드"); // when - Post result = postService.createPost(request, mentor); + postService.createPost(request, mentor); + // then - assertThat(result.getPostType()).isEqualTo(Post.PostType.PRACTICEPOST); - verify(postRepository).save(any(Post.class)); + ArgumentCaptor captor = ArgumentCaptor.forClass(Post.class); + verify(postRepository).save(captor.capture()); + + Post savedPost = captor.getValue(); + + assertThat(savedPost.getPostType()).isEqualTo(Post.PostType.PRACTICEPOST); + assertThat(savedPost.getTitle()).isEqualTo("실무경험"); + assertThat(savedPost.getContent()).isEqualTo("실무내용"); + assertThat(savedPost.getJob()).isEqualTo("백엔드"); + assertThat(savedPost.getMember()).isEqualTo(mentor); } @Test - @DisplayName("멘티가 실무 경험 공유 게시글 생성 실패") + @DisplayName("실무 경험 공유 게시글 생성 실패 - 멘티") void createPost_practicePost_mentee_failure() { // given Member mentee = MemberFixture.create(1L, "mentee@test.com", "Mentee", "password", Member.Role.MENTEE); @@ -104,18 +111,25 @@ void createPost_questionPost_initializeIsResolve() { Member member = MemberFixture.create(1L, "test@test.com", "Test User", "password", Member.Role.MENTEE); PostCreateRequest request = new PostCreateRequest("QUESTIONPOST","질문경험","질문내용",""); - Post expectedPost = createPost("질문", "질문 내용", member, Post.PostType.QUESTIONPOST); - expectedPost.updateResolveStatus(false); - - when(postRepository.save(any(Post.class))).thenReturn(expectedPost); // when - Post result = postService.createPost(request, member); + postService.createPost(request, member); // then - assertThat(result.getPostType()).isEqualTo(Post.PostType.QUESTIONPOST); - assertThat(result.getIsResolve()).isFalse(); - verify(postRepository).save(any(Post.class)); + + ArgumentCaptor captor = ArgumentCaptor.forClass(Post.class); + verify(postRepository).save(captor.capture()); + + Post savedPost = captor.getValue(); + + assertThat(savedPost.getPostType()).isEqualTo(Post.PostType.QUESTIONPOST); + assertThat(savedPost.getTitle()).isEqualTo("질문경험"); + assertThat(savedPost.getContent()).isEqualTo("질문내용"); + assertThat(savedPost.getMember()).isEqualTo(member); + assertThat(savedPost.getIsResolve()).isFalse(); + + + } @Test @@ -169,7 +183,7 @@ void removePost_notAuthor_failure() { // when & then assertThatThrownBy(() -> postService.removePost(postId, otherUser)) .isInstanceOf(ServiceException.class) - .hasMessage("400 : 삭제 권한이 없습니다."); + .hasMessage("400 : 변경 권한이 없습니다."); verify(postRepository, never()).delete(any(Post.class)); } @@ -206,7 +220,6 @@ void updatePost_author_success() { PostModifyRequest updateRequest = new PostModifyRequest("새 제목","새 내용"); when(postRepository.findById(postId)).thenReturn(Optional.of(post)); - when(postRepository.save(any(Post.class))).thenReturn(post); // when postService.updatePost(postId, author, updateRequest); @@ -233,7 +246,7 @@ void updatePost_notAuthor_failure() { // when & then assertThatThrownBy(() -> postService.updatePost(postId, otherUser, updateRequest)) .isInstanceOf(ServiceException.class) - .hasMessage("400 : 수정 권한이 없습니다."); + .hasMessage("400 : 변경 권한이 없습니다."); verify(postRepository, never()).save(any(Post.class)); } @@ -282,7 +295,7 @@ void updatePost_nullOrBlankContent_failure() { class GetPostTest { @Test - @DisplayName("모든 게시글 조회 성공") + @DisplayName("게시글 다건 조회 성공") void getAllPosts_success() { // given Member member1 = MemberFixture.create(1L, "user1@test.com", "User1", "password", Member.Role.MENTEE); @@ -345,22 +358,42 @@ void getPosts_withPaging_success() { int page = 0; int size = 10; Post.PostType postType = Post.PostType.INFORMATIONPOST; - Pageable pageable = PageRequest.of(page, size); Member member = MemberFixture.create(1L, "user@test.com", "User", "password", Member.Role.MENTEE); - Post post = createPost("테스트 제목", "테스트 내용", member, Post.PostType.INFORMATIONPOST); - List posts = Arrays.asList(post); - Page postPage = new PageImpl<>(posts, pageable, 1); - when(postRepository.searchPosts(keyword, pageable, postType)).thenReturn(postPage); + List posts = Arrays.asList( + createPost("테스트 제목1", "테스트 내용1", member, Post.PostType.INFORMATIONPOST) + ); + + Pageable expectedPageable = PageRequest.of(page, size); + Page postPage = new PageImpl<>(posts, expectedPageable, 1); + + when(postRepository.searchPosts(anyString(), any(Pageable.class), any(Post.PostType.class))) + .thenReturn(postPage); // when Page result = postService.getPosts(keyword, page, size, postType); - // then + // then - ArgumentCaptor로 인자 캡처 + ArgumentCaptor keywordCaptor = ArgumentCaptor.forClass(String.class); + ArgumentCaptor pageableCaptor = ArgumentCaptor.forClass(Pageable.class); + ArgumentCaptor postTypeCaptor = ArgumentCaptor.forClass(Post.PostType.class); + + verify(postRepository).searchPosts( + keywordCaptor.capture(), + pageableCaptor.capture(), + postTypeCaptor.capture() + ); + + // 전달된 인자 검증 + assertThat(keywordCaptor.getValue()).isEqualTo("테스트"); + assertThat(pageableCaptor.getValue().getPageNumber()).isEqualTo(0); + assertThat(pageableCaptor.getValue().getPageSize()).isEqualTo(10); + assertThat(postTypeCaptor.getValue()).isEqualTo(Post.PostType.INFORMATIONPOST); + + // 결과 검증 assertThat(result.getContent()).hasSize(1); assertThat(result.getTotalElements()).isEqualTo(1); - verify(postRepository).searchPosts(keyword, pageable, postType); } @Test @@ -374,7 +407,7 @@ void findById_success() { when(postRepository.findById(postId)).thenReturn(Optional.of(post)); // when - Post result = postService.findById(postId); + Post result = postService.findPostById(postId); // then assertThat(result).isEqualTo(post); @@ -390,7 +423,7 @@ void findById_notExists_failure() { when(postRepository.findById(postId)).thenReturn(Optional.empty()); // when & then - assertThatThrownBy(() -> postService.findById(postId)) + assertThatThrownBy(() -> postService.findPostById(postId)) .isInstanceOf(ServiceException.class) .hasMessage("400 : 해당 Id의 게시글이 없습니다."); } diff --git a/back/src/test/java/com/back/fixture/Post/PostFixture.java b/back/src/test/java/com/back/fixture/Post/PostFixture.java new file mode 100644 index 00000000..68bab676 --- /dev/null +++ b/back/src/test/java/com/back/fixture/Post/PostFixture.java @@ -0,0 +1,55 @@ +package com.back.fixture.Post; + +import com.back.domain.member.member.entity.Member; +import com.back.domain.post.post.entity.Post; +import org.springframework.test.util.ReflectionTestUtils; + +public class PostFixture { + private static Post.PostType questionPost = Post.PostType.QUESTIONPOST; + private static final Post.PostType practicePost = Post.PostType.PRACTICEPOST; + private static final Post.PostType InformationPost = Post.PostType.INFORMATIONPOST; + private static final String title = "테스트 제목"; + private static final String content = "테스트 내용"; + private static final String job = "백엔드"; + + + + public static Post createQuestionPost(Member member) { + Post post = Post.builder() + .postType(questionPost) + .title(title) + .content(content) + .member(member) + .build(); + + post.updateResolveStatus(false); + + return post; + } + + public static Post createPracticePost(Member member) { + Post post = Post.builder() + .postType(practicePost) + .title(title) + .content(content) + .member(member) + .build(); + + post.updateJob(job); + + return post; + } + + public static Post createInformationPost(Member member, Long id) { + Post post = Post.builder() + .postType(InformationPost) + .title(title) + .content(content) + .member(member) + .build(); + + ReflectionTestUtils.setField(post, "id", id); + + return post; + } +} From cd192a57d59b57adcf1ed76b85554c2e8d081bc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=84=9D=ED=9D=AC=EC=84=B1?= Date: Tue, 14 Oct 2025 16:01:39 +0900 Subject: [PATCH 2/3] =?UTF-8?q?fix=20:=20Post=20=EB=A6=AC=ED=84=B4?= =?UTF-8?q?=ED=83=80=EC=9E=85=20=EC=88=98=EC=A0=95=20(#331)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/back/domain/post/like/service/PostLikeService.java | 5 +++-- .../com/back/domain/post/post/controller/PostController.java | 4 ++-- .../main/java/com/back/domain/post/rq/PostDetailFacade.java | 5 +++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/back/src/main/java/com/back/domain/post/like/service/PostLikeService.java b/back/src/main/java/com/back/domain/post/like/service/PostLikeService.java index 3fe14b2b..3a295b0e 100644 --- a/back/src/main/java/com/back/domain/post/like/service/PostLikeService.java +++ b/back/src/main/java/com/back/domain/post/like/service/PostLikeService.java @@ -3,6 +3,7 @@ import com.back.domain.member.member.entity.Member; import com.back.domain.post.like.entity.PostLike; import com.back.domain.post.like.repository.PostLikeRepository; +import com.back.domain.post.post.dto.PostDisLikedResponse; import com.back.domain.post.post.dto.PostLikedResponse; import com.back.domain.post.post.entity.Post; import com.back.domain.post.post.service.PostService; @@ -75,9 +76,9 @@ public void disLikePost(long postId) { } } - public PostLikedResponse getDisLikeCount(Long postId) { + public PostDisLikedResponse getDisLikeCount(Long postId) { int disLikeCount = postLikeRepository.countDislikesByPostId(postId); - return new PostLikedResponse(disLikeCount); + return new PostDisLikedResponse(disLikeCount); } diff --git a/back/src/main/java/com/back/domain/post/post/controller/PostController.java b/back/src/main/java/com/back/domain/post/post/controller/PostController.java index 8a00b2c9..a8bbbe44 100644 --- a/back/src/main/java/com/back/domain/post/post/controller/PostController.java +++ b/back/src/main/java/com/back/domain/post/post/controller/PostController.java @@ -108,8 +108,8 @@ public RsData likePost(@PathVariable(name = "post_id") Long p @PostMapping("/{post_id}/disliked") public RsData disLikePost(@PathVariable(name = "post_id") Long postId) { postLikeService.disLikePost(postId); - PostLikedResponse postLikedResponse = postLikeService.getDisLikeCount(postId); - return new RsData<>("200", "게시글 싫어요 성공", postLikedResponse); + PostDisLikedResponse postDisLikedResponse = postLikeService.getDisLikeCount(postId); + return new RsData<>("200", "게시글 싫어요 성공", postDisLikedResponse); } diff --git a/back/src/main/java/com/back/domain/post/rq/PostDetailFacade.java b/back/src/main/java/com/back/domain/post/rq/PostDetailFacade.java index aa3c628b..3fd2b6bc 100644 --- a/back/src/main/java/com/back/domain/post/rq/PostDetailFacade.java +++ b/back/src/main/java/com/back/domain/post/rq/PostDetailFacade.java @@ -4,6 +4,7 @@ import com.back.domain.post.comment.service.PostCommentService; import com.back.domain.post.like.service.PostLikeService; import com.back.domain.post.post.dto.PostDetailResponse; +import com.back.domain.post.post.dto.PostDisLikedResponse; import com.back.domain.post.post.dto.PostLikedResponse; import com.back.domain.post.post.entity.Post; import com.back.domain.post.post.service.PostService; @@ -28,10 +29,10 @@ public PostDetailResponse getDetailWithViewIncrement(Long postId) { List comments = postCommentService.getAllPostCommentResponse(postId); PostLikedResponse postLikedResponse = postLikeService.getLikeCount(postId); - PostLikedResponse postDisLikedResponse = postLikeService.getDisLikeCount(postId); + PostDisLikedResponse postDisLikedResponse = postLikeService.getDisLikeCount(postId); String userStatus = postLikeService.getPresentStatus(postId); - return PostDetailResponse.from(post, comments, postLikedResponse.likeCount(), postDisLikedResponse.likeCount(), userStatus); + return PostDetailResponse.from(post, comments, postLikedResponse.likeCount(), postDisLikedResponse.disLikeCount(), userStatus); } From 6b7b0c0aabe40096b92be6024103c5babf03177e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=8B=A0=EC=A7=80=EC=84=AD?= <41179427+tlswltjq@users.noreply.github.com> Date: Tue, 14 Oct 2025 16:15:48 +0900 Subject: [PATCH 3/3] Refactor/314 (#333) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Feat : uuid를 갖는 로우 존재확인하는 JPA메서드 추가 * Feat : 데이터베이스에 uuid를 갖는 로우 존재확인하는 메서드 추가 * Feat : uuid와 해상도를 전달하면 해상도를 검사하고 적절한 mpd파일을 반환하는 PresignedGetObjectRequest를 요청하도록 수정 * Chore : 패키지 변경 * Test/Chore : 테스트 추가, 업로드요청 모두 허용하던부분 수정 --- .../video/controller/VideoController.java | 4 +- .../video/repository/VideoRepository.java | 1 + .../file/video/service/FileManager.java | 13 ++- .../file/video/service/VideoService.java | 6 ++ .../back/global/security/SecurityConfig.java | 1 - .../video/controller/VideoControllerTest.java | 100 ++++++++++++++++++ .../file/{ => video}/entity/VideoTest.java | 2 +- .../{ => video}/service/S3ServiceTest.java | 2 +- .../{ => video}/service/VideoServiceTest.java | 2 +- 9 files changed, 124 insertions(+), 7 deletions(-) create mode 100644 back/src/test/java/com/back/domain/file/video/controller/VideoControllerTest.java rename back/src/test/java/com/back/domain/file/{ => video}/entity/VideoTest.java (98%) rename back/src/test/java/com/back/domain/file/{ => video}/service/S3ServiceTest.java (99%) rename back/src/test/java/com/back/domain/file/{ => video}/service/VideoServiceTest.java (99%) diff --git a/back/src/main/java/com/back/domain/file/video/controller/VideoController.java b/back/src/main/java/com/back/domain/file/video/controller/VideoController.java index 09e5e9b9..c6a1fb11 100644 --- a/back/src/main/java/com/back/domain/file/video/controller/VideoController.java +++ b/back/src/main/java/com/back/domain/file/video/controller/VideoController.java @@ -37,8 +37,8 @@ public RsData getUploadUrl(@RequestParam String filename) @GetMapping("/videos/download") @Operation(summary = "다운로드용 URL 요청", description = "파일 다운로드를 위한 Presigned URL을 발급받습니다.") - public RsData getDownloadUrls(@RequestParam String objectKey) { - PresignedUrlResponse downloadUrl = fileManager.getDownloadUrl(objectKey); + public RsData getDownloadUrls(@RequestParam String uuid, @RequestParam String resolution) { + PresignedUrlResponse downloadUrl = fileManager.getDownloadUrl(uuid, resolution); DownLoadUrlGetResponse response = new DownLoadUrlGetResponse(downloadUrl.url().toString(), downloadUrl.expiresAt()); return new RsData<>("200", "다운로드용 URL 요청완료", response); } diff --git a/back/src/main/java/com/back/domain/file/video/repository/VideoRepository.java b/back/src/main/java/com/back/domain/file/video/repository/VideoRepository.java index b3151f13..7afec625 100644 --- a/back/src/main/java/com/back/domain/file/video/repository/VideoRepository.java +++ b/back/src/main/java/com/back/domain/file/video/repository/VideoRepository.java @@ -9,4 +9,5 @@ @Repository public interface VideoRepository extends JpaRepository { Optional