diff --git a/src/main/java/com/example/spot/service/studypost/StudyPostQueryService.java b/src/main/java/com/example/spot/service/studypost/StudyPostQueryService.java index 2c154d42..e4868bd5 100644 --- a/src/main/java/com/example/spot/service/studypost/StudyPostQueryService.java +++ b/src/main/java/com/example/spot/service/studypost/StudyPostQueryService.java @@ -11,7 +11,7 @@ public interface StudyPostQueryService { StudyPostResDTO.PostListDTO getAllPosts(PageRequest pageRequest, Long studyId, ThemeQuery themeQuery); // 스터디 게시글 불러오기 - StudyPostResDTO.PostDetailDTO getPost(Long studyId, Long postId); + StudyPostResDTO.PostDetailDTO getPost(Long studyId, Long postId, Boolean likeOrScrap); // 스터디 게시글 댓글 목록 불러오기 StudyPostCommentResponseDTO.CommentReplyListDTO getAllComments(Long studyId, Long postId); diff --git a/src/main/java/com/example/spot/service/studypost/StudyPostQueryServiceImpl.java b/src/main/java/com/example/spot/service/studypost/StudyPostQueryServiceImpl.java index 647a9bb3..45322bbf 100644 --- a/src/main/java/com/example/spot/service/studypost/StudyPostQueryServiceImpl.java +++ b/src/main/java/com/example/spot/service/studypost/StudyPostQueryServiceImpl.java @@ -93,13 +93,15 @@ public StudyPostResDTO.PostListDTO getAllPosts(PageRequest pageRequest, Long stu /** * 스터디 게시판의 특정 게시글을 조회하는 메서드입니다. - * @param studyId 게시글을 조회할 타겟 스터디의 아이디를 입력 받습니다. - * @param postId 조회할 타겟 게시글의 아이디를 입력 받습니다. + * + * @param studyId 게시글을 조회할 타겟 스터디의 아이디를 입력 받습니다. + * @param postId 조회할 타겟 게시글의 아이디를 입력 받습니다. + * @param likeOrScrap * @return 스터디 게시글의 정보를 반환합니다. */ @Override @Transactional(readOnly = false) - public StudyPostResDTO.PostDetailDTO getPost(Long studyId, Long postId) { + public StudyPostResDTO.PostDetailDTO getPost(Long studyId, Long postId, Boolean likeOrScrap) { //=== Exception ===// Long memberId = SecurityUtils.getCurrentUserId(); @@ -121,7 +123,12 @@ public StudyPostResDTO.PostDetailDTO getPost(Long studyId, Long postId) { .orElseThrow(() -> new StudyHandler(ErrorStatus._STUDY_MEMBER_NOT_FOUND)); //=== Feature ===// - studyPost.plusHitNum(); + + // 조회수 증가는 일반 조회 시에만 실행 + if (!likeOrScrap) { + studyPost.plusHitNum(); + } + studyPost = studyPostRepository.save(studyPost); memberRepository.save(member); studyRepository.save(study); diff --git a/src/main/java/com/example/spot/web/controller/StudyPostController.java b/src/main/java/com/example/spot/web/controller/StudyPostController.java index f4a3b8e0..4ed48aff 100644 --- a/src/main/java/com/example/spot/web/controller/StudyPostController.java +++ b/src/main/java/com/example/spot/web/controller/StudyPostController.java @@ -127,8 +127,9 @@ public ApiResponse getAllPosts( @GetMapping("/studies/{studyId}/posts/{postId}") public ApiResponse getPost( @PathVariable @ExistStudy Long studyId, - @PathVariable @ExistStudyPost Long postId) { - StudyPostResDTO.PostDetailDTO postDetailDTO = studyPostQueryService.getPost(studyId, postId); + @PathVariable @ExistStudyPost Long postId, + @RequestParam Boolean likeOrScrap) { + StudyPostResDTO.PostDetailDTO postDetailDTO = studyPostQueryService.getPost(studyId, postId, likeOrScrap); return ApiResponse.onSuccess(SuccessStatus._STUDY_POST_FOUND, postDetailDTO); } diff --git a/src/test/java/com/example/spot/service/studypost/StudyPostQueryServiceTest.java b/src/test/java/com/example/spot/service/studypost/StudyPostQueryServiceTest.java index 09fe76e1..55b0ab7a 100644 --- a/src/test/java/com/example/spot/service/studypost/StudyPostQueryServiceTest.java +++ b/src/test/java/com/example/spot/service/studypost/StudyPostQueryServiceTest.java @@ -259,8 +259,8 @@ void getAllPosts_NotCategorized_Fail() { /*-------------------------------------------------------- 게시글 조회 ------------------------------------------------------------------------*/ @Test - @DisplayName("스터디 게시글 단건 조회 - (성공)") - void getPost_Success() { + @DisplayName("스터디 게시글 단건 조회 - 일반 조회 (성공)") + void getPost_Common_Success() { // given Long studyId = 1L; @@ -280,7 +280,7 @@ void getPost_Success() { .thenReturn(false); // when - StudyPostResDTO.PostDetailDTO result = studyPostQueryService.getPost(studyId, postId); + StudyPostResDTO.PostDetailDTO result = studyPostQueryService.getPost(studyId, postId, false); // then assertNotNull(result); @@ -289,11 +289,43 @@ void getPost_Success() { assertThat(result.getTitle()).isEqualTo("잡담"); assertThat(result.getCommentNum()).isEqualTo(2); assertThat(result.getIsLiked()).isEqualTo(false); + } + + @Test + @DisplayName("스터디 게시글 단건 조회 - 스크랩 혹은 좋아요 후 업데이트 (성공)") + void getPost_LikeOrScrap_Success() { + + // given + Long studyId = 1L; + Long memberId = 1L; + Long postId = 1L; + + getAuthentication(memberId); + + when(studyPostRepository.findByIdAndStudyId(postId, studyId)) + .thenReturn(Optional.of(studyPost1)); + when(studyPostRepository.save(studyPost1)).thenReturn(studyPost1); + when(memberRepository.save(member1)).thenReturn(member1); + when(studyRepository.save(study)).thenReturn(study); + when(studyPostCommentRepository.findAllByStudyPostId(postId)) + .thenReturn(List.of(studyPost1Comment1, studyPost1Comment2)); + when(studyLikedPostRepository.existsByMemberIdAndStudyPostId(memberId, postId)) + .thenReturn(false); + + // when + StudyPostResDTO.PostDetailDTO result = studyPostQueryService.getPost(studyId, postId, true); + // then + assertNotNull(result); + assertThat(result.getPostId()).isEqualTo(1L); + assertThat(result.getHitNum()).isEqualTo(10); + assertThat(result.getTitle()).isEqualTo("잡담"); + assertThat(result.getCommentNum()).isEqualTo(2); + assertThat(result.getIsLiked()).isEqualTo(false); } @Test - @DisplayName("스터디 게시글 단건 조회 - 스터디 회원이 아닌 경우(실패)") + @DisplayName("스터디 게시글 단건 조회 - 스터디 회원이 아닌 경우 (실패)") void getPost_NotStudyMember_Fail() { // given @@ -314,7 +346,7 @@ void getPost_NotStudyMember_Fail() { .thenReturn(false); // when & then - assertThrows(StudyHandler.class, () ->studyPostQueryService.getPost(studyId, postId)); + assertThrows(StudyHandler.class, () ->studyPostQueryService.getPost(studyId, postId, false)); } @Test @@ -334,7 +366,7 @@ void getPost_NotStudyPost_Fail() { .thenReturn(List.of()); // when & then - assertThrows(StudyHandler.class, () ->studyPostQueryService.getPost(studyId, postId)); + assertThrows(StudyHandler.class, () ->studyPostQueryService.getPost(studyId, postId, false)); } /*-------------------------------------------------------- 댓글 목록 조회 ------------------------------------------------------------------------*/