Skip to content

Commit

Permalink
특정 멘토 팔로우 여부 단건 조회 기능 구현 #298
Browse files Browse the repository at this point in the history
  • Loading branch information
ddongpuri authored Nov 28, 2023
2 parents 098a1a8 + b3cd637 commit 05e3f90
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 4 deletions.
10 changes: 10 additions & 0 deletions src/docs/asciidoc/mentee.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,14 @@ public List<FollowResponse> 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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public interface FollowRepository extends JpaRepository<Follow, Long> {

List<Follow> findAllByMenteeId(Long menteeId);

List<Follow> findAllByMentorId(Long mentorId);

Optional<Follow> findByMenteeIdAndMentorId(Long menteeId, Long mentorId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Mentor, Long> {
Expand All @@ -14,4 +15,7 @@ public interface MentorRepository extends JpaRepository<Mentor, Long> {
@Query("select m from Mentor m join fetch m.experiencedPositions where m.id = :mentorId")
Optional<Mentor> findWithPositions(@Param("mentorId") Long mentorId);

@Query("select m from Mentor m join fetch m.experiencedPositions where m.id in :mentorIds")
List<Mentor> findAllByIds(@Param("mentorIds") List<Long> mentorIds);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Follow> followList, Long mentorId) {
return followList.stream().anyMatch(follow -> follow.getMentorId().equals(mentorId));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -63,6 +63,6 @@ public void deleteRefreshToken(Long id) {
}

public List<Mentor> getAllByIds(List<Long> mentorIds) {
return mentorRepository.findAllById(mentorIds);
return mentorRepository.findAllByIds(mentorIds);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -147,6 +149,34 @@ class FollowControllerTest extends ControllerUnitTest {

}

@Test
@WithMockCustomUser
void 멘티는_멘토_아이디로_팔로우_여부를_조회할_수_있다() throws Exception {
// given
Optional<Follow> 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 {
Expand Down

0 comments on commit 05e3f90

Please sign in to comment.