diff --git a/src/main/java/kr/co/studyhubinu/studyhubserver/apply/domain/ApplyEntity.java b/src/main/java/kr/co/studyhubinu/studyhubserver/apply/domain/ApplyEntity.java index 623366b9..e8973cfe 100644 --- a/src/main/java/kr/co/studyhubinu/studyhubserver/apply/domain/ApplyEntity.java +++ b/src/main/java/kr/co/studyhubinu/studyhubserver/apply/domain/ApplyEntity.java @@ -35,17 +35,17 @@ public class ApplyEntity extends BaseTimeEntity { private Long userId; @Builder - public ApplyEntity(Inspection inspection, String introduce, Long study, Long userId) { + public ApplyEntity(Inspection inspection, String introduce, Long studyId, Long userId) { this.inspection = inspection; this.introduce = introduce; - this.studyId = study; + this.studyId = studyId; this.userId = userId; } public static ApplyEntity of(Long userId, Long studyId, String introduce) { return ApplyEntity.builder() .userId(userId) - .study(studyId) + .studyId(studyId) .introduce(introduce) .inspection(Inspection.STANDBY) .build(); diff --git a/src/main/java/kr/co/studyhubinu/studyhubserver/studypost/controller/StudyPostController.java b/src/main/java/kr/co/studyhubinu/studyhubserver/studypost/controller/StudyPostController.java index ee931e3e..54650320 100644 --- a/src/main/java/kr/co/studyhubinu/studyhubserver/studypost/controller/StudyPostController.java +++ b/src/main/java/kr/co/studyhubinu/studyhubserver/studypost/controller/StudyPostController.java @@ -67,7 +67,7 @@ public ResponseEntity getBookmarkedPosts(@RequestPar } @Operation(summary = "스터디 단건 조회", description = "url 끝에 postId를 넣어주세요") - @GetMapping("/v1/study-posts/{postId}") + @GetMapping("/v2/study-posts/{postId}") public ResponseEntity findPostById(@PathVariable Long postId, UserId userId) { FindPostResponseById findPostResponseById = studyPostFindService.findPostById(postId, userId.getId()); return ResponseEntity.ok(findPostResponseById); diff --git a/src/main/java/kr/co/studyhubinu/studyhubserver/studypost/dto/response/FindPostResponseById.java b/src/main/java/kr/co/studyhubinu/studyhubserver/studypost/dto/response/FindPostResponseById.java index faf572ac..f911a653 100644 --- a/src/main/java/kr/co/studyhubinu/studyhubserver/studypost/dto/response/FindPostResponseById.java +++ b/src/main/java/kr/co/studyhubinu/studyhubserver/studypost/dto/response/FindPostResponseById.java @@ -14,26 +14,27 @@ @Getter public class FindPostResponseById { - private Long postId; - private String title; - private LocalDateTime createdDate; - private String content; - private MajorType major; - private int studyPerson; - private GenderType filteredGender; - private StudyWayType studyWay; - private int penalty; - private String penaltyWay; - private LocalDate studyStartDate; - private LocalDate studyEndDate; - private int remainingSeat; - private String chatUrl; - private boolean isUsersPost; - private boolean isBookmarked; - private UserData postedUser; - private List relatedPost; + private final Long postId; + private final String title; + private final LocalDateTime createdDate; + private final String content; + private final MajorType major; + private final int studyPerson; + private final GenderType filteredGender; + private final StudyWayType studyWay; + private final int penalty; + private final String penaltyWay; + private final LocalDate studyStartDate; + private final LocalDate studyEndDate; + private final int remainingSeat; + private final String chatUrl; + private final boolean isUsersPost; + private final boolean isBookmarked; + private final boolean isApply; + private final UserData postedUser; + private final List relatedPost; - public FindPostResponseById(PostData postData, List relatedPosts) { + public FindPostResponseById(PostData postData, List relatedPosts, boolean isApply) { this.postId = postData.getPostId(); this.title = postData.getTitle(); this.createdDate = postData.getCreatedDate(); @@ -52,5 +53,6 @@ public FindPostResponseById(PostData postData, List relatedPost this.isBookmarked = postData.isBookmarked(); this.postedUser = postData.getPostedUser(); this.relatedPost = relatedPosts; + this.isApply = isApply; } } diff --git a/src/main/java/kr/co/studyhubinu/studyhubserver/studypost/service/StudyPostFindService.java b/src/main/java/kr/co/studyhubinu/studyhubserver/studypost/service/StudyPostFindService.java index 68720e1d..78c0af01 100644 --- a/src/main/java/kr/co/studyhubinu/studyhubserver/studypost/service/StudyPostFindService.java +++ b/src/main/java/kr/co/studyhubinu/studyhubserver/studypost/service/StudyPostFindService.java @@ -1,6 +1,8 @@ package kr.co.studyhubinu.studyhubserver.studypost.service; +import kr.co.studyhubinu.studyhubserver.apply.domain.ApplyEntity; +import kr.co.studyhubinu.studyhubserver.apply.repository.ApplyRepository; import kr.co.studyhubinu.studyhubserver.bookmark.repository.BookmarkRepository; import kr.co.studyhubinu.studyhubserver.common.dto.Converter; import kr.co.studyhubinu.studyhubserver.exception.study.PostNotFoundException; @@ -24,10 +26,11 @@ import org.springframework.transaction.annotation.Transactional; import java.util.List; +import java.util.Optional; @RequiredArgsConstructor @Service -@Transactional +@Transactional(readOnly = true) public class StudyPostFindService { private static final int POST_RECOMMEND_COUNT = 10; @@ -35,6 +38,7 @@ public class StudyPostFindService { private final StudyPostRepository studyPostRepository; private final UserRepository userRepository; private final BookmarkRepository bookMarkRepository; + private final ApplyRepository applyRepository; public FindPostResponseByInquiry findPostResponseByInquiry(final InquiryRequest inquiryRequest, final int page, final int size, Long userId) { final Pageable pageable = PageRequest.of(page, size); @@ -69,7 +73,14 @@ public FindPostResponseByUserId getMyPosts(int page, int size, Long userId) { public FindPostResponseById findPostById(Long postId, Long userId) { final PostData postData = findPostDataById(postId, userId); - return new FindPostResponseById(postData, getRelatedPosts(postData.getMajor(), postId)); + boolean isApply = getUserApply(userId, postData); + + return new FindPostResponseById(postData, getRelatedPosts(postData.getMajor(), postId), isApply); + } + + private boolean getUserApply(Long userId, PostData postData) { + Optional apply = applyRepository.findByUserIdAndStudyId(userId, postData.getStudyId()); + return apply.isPresent(); } public List getRelatedPosts(MajorType major, Long exceptPostId) { diff --git a/src/test/java/kr/co/studyhubinu/studyhubserver/apply/repository/ApplyRepositoryTest.java b/src/test/java/kr/co/studyhubinu/studyhubserver/apply/repository/ApplyRepositoryTest.java index b0b38a9c..8fc48aa8 100644 --- a/src/test/java/kr/co/studyhubinu/studyhubserver/apply/repository/ApplyRepositoryTest.java +++ b/src/test/java/kr/co/studyhubinu/studyhubserver/apply/repository/ApplyRepositoryTest.java @@ -53,7 +53,7 @@ class ApplyRepositoryTest { // when ApplyEntity apply = ApplyEntity.builder() .userId(user.getId()) - .study(study.getId()) + .studyId(study.getId()) .inspection(Inspection.ACCEPT) .build(); ApplyEntity result = applyRepository.save(apply); @@ -79,7 +79,7 @@ class ApplyRepositoryTest { // when ApplyEntity applyEntity = ApplyEntity.builder() .userId(user.getId()) - .study(study.getId()) + .studyId(study.getId()) .inspection(Inspection.ACCEPT) .build(); applyRepository.save(applyEntity); @@ -101,7 +101,7 @@ class ApplyRepositoryTest { StudyEntity study = studyRepository.save(StudyEntityFixture.INU.studyEntity_생성()); ApplyEntity apply = ApplyEntity.builder() .userId(user.getId()) - .study(study.getId()) + .studyId(study.getId()) .inspection(Inspection.ACCEPT) .build(); applyRepository.save(apply); @@ -132,14 +132,14 @@ class ApplyRepositoryTest { ApplyEntity apply1 = ApplyEntity.builder() .userId(user.getId()) - .study(study.getId()) + .studyId(study.getId()) .inspection(Inspection.ACCEPT) .introduce("벌금내러 왔습니다.") .build(); ApplyEntity apply2 = ApplyEntity.builder() .userId(user2.getId()) - .study(study.getId()) + .studyId(study.getId()) .inspection(Inspection.ACCEPT) .introduce("목숨을 걸겠습니다.") .build(); @@ -156,7 +156,14 @@ class ApplyRepositoryTest { } @Test - void 스터디_참가요청_조회_유저데이터_반환() { + void 로그인하지_않은_유저가_조회할_경우_반환값_없음() { + // given + applyRepository.save(ApplyEntity.builder().userId(1L).studyId(1L).build()); + + // when + Optional apply = applyRepository.findByUserIdAndStudyId(null, 1L); + // then + assertThat(apply).isEmpty(); } } \ No newline at end of file diff --git a/src/test/java/kr/co/studyhubinu/studyhubserver/apply/service/ApplyServiceTest.java b/src/test/java/kr/co/studyhubinu/studyhubserver/apply/service/ApplyServiceTest.java index a608cf38..82a0a570 100644 --- a/src/test/java/kr/co/studyhubinu/studyhubserver/apply/service/ApplyServiceTest.java +++ b/src/test/java/kr/co/studyhubinu/studyhubserver/apply/service/ApplyServiceTest.java @@ -63,7 +63,7 @@ class ApplyServiceTest { when(studyRepository.findById(anyLong())).thenReturn(Optional.ofNullable(study)); when(applyRepository.save(any())).thenReturn(ApplyEntity.builder() .userId(user.getId()) - .study(study.getId()) + .studyId(study.getId()) .build()); // when, then diff --git a/src/test/java/kr/co/studyhubinu/studyhubserver/studypost/controller/StudyPostControllerTest.java b/src/test/java/kr/co/studyhubinu/studyhubserver/studypost/controller/StudyPostControllerTest.java index ce8de2aa..3d00f832 100644 --- a/src/test/java/kr/co/studyhubinu/studyhubserver/studypost/controller/StudyPostControllerTest.java +++ b/src/test/java/kr/co/studyhubinu/studyhubserver/studypost/controller/StudyPostControllerTest.java @@ -194,10 +194,10 @@ static Stream requestParameters() { void 스터디_단건_조회_성공() throws Exception { // given PostData postData = PostData.builder().build(); - when(studyPostFindService.findPostById(any(), any())).thenReturn(new FindPostResponseById(postData, new ArrayList<>())); + when(studyPostFindService.findPostById(any(), any())).thenReturn(new FindPostResponseById(postData, new ArrayList<>(), true)); // when - ResultActions resultActions = performGetRequest("/api/v1/study-posts/1", null); + ResultActions resultActions = performGetRequest("/api/v2/study-posts/1", null); MvcResult mvcResult = resultActions.andReturn(); String responseBody = mvcResult.getResponse().getContentAsString(UTF_8); diff --git a/src/test/java/kr/co/studyhubinu/studyhubserver/studypost/service/StudyPostFindServiceTest.java b/src/test/java/kr/co/studyhubinu/studyhubserver/studypost/service/StudyPostFindServiceTest.java index a0a783e9..20588475 100644 --- a/src/test/java/kr/co/studyhubinu/studyhubserver/studypost/service/StudyPostFindServiceTest.java +++ b/src/test/java/kr/co/studyhubinu/studyhubserver/studypost/service/StudyPostFindServiceTest.java @@ -1,5 +1,7 @@ package kr.co.studyhubinu.studyhubserver.studypost.service; +import kr.co.studyhubinu.studyhubserver.apply.domain.ApplyEntity; +import kr.co.studyhubinu.studyhubserver.apply.repository.ApplyRepository; import kr.co.studyhubinu.studyhubserver.bookmark.repository.BookmarkRepository; import kr.co.studyhubinu.studyhubserver.exception.study.PostNotFoundException; import kr.co.studyhubinu.studyhubserver.exception.user.UserNotFoundException; @@ -46,6 +48,9 @@ class StudyPostFindServiceTest { @Mock BookmarkRepository bookmarkRepository; + @Mock + ApplyRepository applyRepository; + @Test void 스터디_게시글_전체_조회() { // given @@ -159,6 +164,7 @@ class StudyPostFindServiceTest { postsByMajor.add(postDataByMajor); when(studyPostRepository.findByMajor(any(), anyLong())).thenReturn(postsByMajor); when(studyPostRepository.findPostById(anyLong(), anyLong())).thenReturn(Optional.ofNullable(PostData.builder().postId(1L).build())); + when(applyRepository.findByUserIdAndStudyId(anyLong(), any())).thenReturn(Optional.empty()); // when FindPostResponseById postResponse = studyPostFindService.findPostById(1L, 1L);