diff --git a/src/docs/asciidoc/mentee.adoc b/src/docs/asciidoc/mentee.adoc index 9a113d0c..443680a7 100644 --- a/src/docs/asciidoc/mentee.adoc +++ b/src/docs/asciidoc/mentee.adoc @@ -324,6 +324,16 @@ include::{snippets}/follows/getAll/http-request.adoc[] include::{snippets}/follows/getAll/http-response.adoc[] include::{snippets}/follows/getAll/response-fields.adoc[] +=== 특정 멘토 팔로우 여부 확인하기 +*Request* +include::{snippets}/follows/find/http-request.adoc[] +include::{snippets}/follows/find/path-parameters.adoc[] + +*Response* +include::{snippets}/follows/find/http-response.adoc[] +include::{snippets}/follows/find/response-fields.adoc[] + + === 멘토 팔로우 취소하기 *Request* diff --git a/src/main/java/org/devcourse/resumeme/business/user/controller/FollowController.java b/src/main/java/org/devcourse/resumeme/business/user/controller/FollowController.java index 20b78aaf..6d58af95 100644 --- a/src/main/java/org/devcourse/resumeme/business/user/controller/FollowController.java +++ b/src/main/java/org/devcourse/resumeme/business/user/controller/FollowController.java @@ -40,8 +40,14 @@ public List getFollowList(@AuthenticationPrincipal JwtUser user) .toList(); } + @GetMapping("/mentors/{mentorId}") + public IdResponse getFollow(@AuthenticationPrincipal JwtUser user, @PathVariable Long mentorId) { + return new IdResponse(followService.getFollow(user.id(), mentorId)); + } + @PostMapping public IdResponse doFollow(@RequestBody FollowRequest request, @AuthenticationPrincipal JwtUser user) { + mentorService.getOne(request.mentorId()); Follow follow = new Follow(user.id(), request.mentorId()); Long followId = followService.create(follow); diff --git a/src/main/java/org/devcourse/resumeme/business/user/repository/FollowRepository.java b/src/main/java/org/devcourse/resumeme/business/user/repository/FollowRepository.java index 43454328..af379f64 100644 --- a/src/main/java/org/devcourse/resumeme/business/user/repository/FollowRepository.java +++ b/src/main/java/org/devcourse/resumeme/business/user/repository/FollowRepository.java @@ -4,6 +4,7 @@ import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; +import java.util.Optional; public interface FollowRepository extends JpaRepository { @@ -11,4 +12,6 @@ public interface FollowRepository extends JpaRepository { List findAllByMentorId(Long mentorId); + Optional findByMenteeIdAndMentorId(Long menteeId, Long mentorId); + } diff --git a/src/main/java/org/devcourse/resumeme/business/user/repository/mentor/MentorRepository.java b/src/main/java/org/devcourse/resumeme/business/user/repository/mentor/MentorRepository.java index a9ac2795..6ddb24d9 100644 --- a/src/main/java/org/devcourse/resumeme/business/user/repository/mentor/MentorRepository.java +++ b/src/main/java/org/devcourse/resumeme/business/user/repository/mentor/MentorRepository.java @@ -5,6 +5,7 @@ import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; +import java.util.List; import java.util.Optional; public interface MentorRepository extends JpaRepository { @@ -14,4 +15,7 @@ public interface MentorRepository extends JpaRepository { @Query("select m from Mentor m join fetch m.experiencedPositions where m.id = :mentorId") Optional findWithPositions(@Param("mentorId") Long mentorId); + @Query("select m from Mentor m join fetch m.experiencedPositions where m.id in :mentorIds") + List findAllByIds(@Param("mentorIds") List mentorIds); + } diff --git a/src/main/java/org/devcourse/resumeme/business/user/service/mentee/FollowService.java b/src/main/java/org/devcourse/resumeme/business/user/service/mentee/FollowService.java index 02c3ecb9..0a4e3200 100644 --- a/src/main/java/org/devcourse/resumeme/business/user/service/mentee/FollowService.java +++ b/src/main/java/org/devcourse/resumeme/business/user/service/mentee/FollowService.java @@ -48,6 +48,13 @@ public void unfollow(Long followId) { followRepository.deleteById(followId); } + @Transactional(readOnly = true) + public Long getFollow(Long menteeId, Long mentorId) { + return followRepository.findByMenteeIdAndMentorId(menteeId, mentorId) + .map(Follow::getId) + .orElse(null); + } + private boolean isFollowing(List followList, Long mentorId) { return followList.stream().anyMatch(follow -> follow.getMentorId().equals(mentorId)); } diff --git a/src/main/java/org/devcourse/resumeme/business/user/service/mentor/MentorService.java b/src/main/java/org/devcourse/resumeme/business/user/service/mentor/MentorService.java index 27cd9026..33ea31a8 100644 --- a/src/main/java/org/devcourse/resumeme/business/user/service/mentor/MentorService.java +++ b/src/main/java/org/devcourse/resumeme/business/user/service/mentor/MentorService.java @@ -1,12 +1,12 @@ package org.devcourse.resumeme.business.user.service.mentor; import lombok.RequiredArgsConstructor; -import org.devcourse.resumeme.business.user.domain.mentor.Mentor; -import org.devcourse.resumeme.business.user.service.admin.MentorApplicationEventPublisher; import org.devcourse.resumeme.business.user.controller.admin.dto.ApplicationProcessType; import org.devcourse.resumeme.business.user.controller.mentor.dto.MentorInfoUpdateRequest; -import org.devcourse.resumeme.global.exception.CustomException; +import org.devcourse.resumeme.business.user.domain.mentor.Mentor; import org.devcourse.resumeme.business.user.repository.mentor.MentorRepository; +import org.devcourse.resumeme.business.user.service.admin.MentorApplicationEventPublisher; +import org.devcourse.resumeme.global.exception.CustomException; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -63,6 +63,6 @@ public void deleteRefreshToken(Long id) { } public List getAllByIds(List mentorIds) { - return mentorRepository.findAllById(mentorIds); + return mentorRepository.findAllByIds(mentorIds); } } diff --git a/src/test/java/org/devcourse/resumeme/business/user/controller/FollowControllerTest.java b/src/test/java/org/devcourse/resumeme/business/user/controller/FollowControllerTest.java index 62c0ef4e..7b9f4006 100644 --- a/src/test/java/org/devcourse/resumeme/business/user/controller/FollowControllerTest.java +++ b/src/test/java/org/devcourse/resumeme/business/user/controller/FollowControllerTest.java @@ -14,10 +14,12 @@ import org.springframework.test.web.servlet.ResultActions; import java.util.List; +import java.util.Optional; import java.util.Set; import static org.assertj.core.api.InstanceOfAssertFactories.INTEGER; import static org.assertj.core.api.InstanceOfAssertFactories.LONG; +import static org.devcourse.resumeme.common.util.ApiDocumentUtils.constraints; import static org.devcourse.resumeme.common.util.ApiDocumentUtils.getDocumentRequest; import static org.devcourse.resumeme.common.util.DocumentLinkGenerator.generateLinkCode; import static org.devcourse.resumeme.global.exception.ExceptionCode.ALREADY_FOLLOWING; @@ -147,6 +149,34 @@ class FollowControllerTest extends ControllerUnitTest { } + @Test + @WithMockCustomUser + void 멘티는_멘토_아이디로_팔로우_여부를_조회할_수_있다() throws Exception { + // given + Optional follow = Optional.of(new Follow(1L, 1L)); + setId(follow.get(), 1L); + + given(followService.getFollow(any(Long.class), any(Long.class))).willReturn(any(Long.class)); + + // when + ResultActions result = mvc.perform(get("/api/v1/follows/mentors/{mentorId}", 1L)); + + // then + result + .andExpect(status().isOk()) + .andDo( + document("follows/find", + getDocumentRequest(), + pathParameters( + parameterWithName("mentorId").description("팔로우 여부 조회할 멘토 아이디") + ), + responseFields( + fieldWithPath("id").type(NUMBER).description("팔로우 아이디").attributes(constraints("팔로우 상태가 아닐경우 null")) + ) + ) + ); + } + @Test @WithMockCustomUser void 특정_멘토에_대해_팔로우를_취소할_수_있다() throws Exception {