Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 유저 팔로잉/팔로워 목록 조회 API #274

Merged
merged 4 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.depromeet.domain.follow.dto.request.FollowDeleteRequest;
import com.depromeet.domain.follow.dto.response.FollowFindMeInfoResponse;
import com.depromeet.domain.follow.dto.response.FollowFindTargetInfoResponse;
import com.depromeet.domain.follow.dto.response.FollowListResponse;
import com.depromeet.domain.follow.dto.response.MemberFollowedResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
Expand Down Expand Up @@ -56,4 +57,11 @@ public FollowFindMeInfoResponse followFindMe() {
public List<MemberFollowedResponse> followedUserFindAll() {
return followService.findAllFollowedMember();
}

// 팔로잉, 팔로워 리스트 응답
@GetMapping("/{targetId}/list")
@Operation(summary = "팔로잉, 팔로워 유저 리스트를 반환합니다", description = "팔로잉, 팔로워 유저들을 반환합니다.")
public FollowListResponse followList(@PathVariable Long targetId) {
return followService.findFollowList(targetId);
}
Comment on lines +60 to +66
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GetMapping("/{targetId}")
    @Operation(
            summary = "타인의 팔로우 카운트 확인",
            description = "타인의 팔로잉/팔로워 카운트와 내가 타인을 팔로우를 하고있는지 확인합니다.")
    public FollowFindTargetInfoResponse followFindTarget(@PathVariable Long targetId) {
        return followService.findTargetFollowInfo(targetId);
    }

위 코드는 count를 확인하는 용도라 혹시 프론트와 협의해서 추가한 엔드포인트와 같이 변경 가능하실까요??

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

윤범님이 주신 코멘트 저도 동감하는데요..!
음 기존에 동작하고 있던게 있기도해서 프론트 공수도 체크해봐야 할 것 같아요ㅜ
스펙도 프론트랑 정해두어서 우선순위에 있는대로 우선 가고 나중에 3차 스프린트 끝나고 리팩토링 하는게 좋을 것 같아용

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
import com.depromeet.domain.follow.domain.MemberRelation;
import com.depromeet.domain.follow.dto.request.FollowCreateRequest;
import com.depromeet.domain.follow.dto.request.FollowDeleteRequest;
import com.depromeet.domain.follow.dto.response.FollowFindMeInfoResponse;
import com.depromeet.domain.follow.dto.response.FollowFindTargetInfoResponse;
import com.depromeet.domain.follow.dto.response.FollowStatus;
import com.depromeet.domain.follow.dto.response.MemberFollowedResponse;
import com.depromeet.domain.follow.dto.response.*;
import com.depromeet.domain.member.dao.MemberRepository;
import com.depromeet.domain.member.domain.Member;
import com.depromeet.domain.member.dto.response.MemberSearchResponse;
import com.depromeet.domain.mission.domain.Mission;
import com.depromeet.domain.missionRecord.domain.ImageUploadStatus;
import com.depromeet.domain.missionRecord.domain.MissionRecord;
Expand Down Expand Up @@ -188,4 +186,78 @@ private Member getTargetMember(Long targetId) {
ErrorCode.FOLLOW_TARGET_MEMBER_NOT_FOUND));
return targetMember;
}

public FollowListResponse findFollowList(Long targetId) {
final Member currentMember = memberUtil.getCurrentMember();
Member targetMember = getTargetMember(targetId);

List<MemberSearchResponse> followingList = new ArrayList<>();
List<MemberSearchResponse> followerList = new ArrayList<>();

List<MemberRelation> targetMemberSources =
memberRelationRepository.findAllBySourceId(targetMember.getId());
List<MemberRelation> targetMemberTargets =
memberRelationRepository.findAllByTargetId(targetMember.getId());

List<MemberRelation> currentMemberSources =
memberRelationRepository.findAllBySourceId(currentMember.getId());
List<MemberRelation> currentMemberTargets =
memberRelationRepository.findAllByTargetId(currentMember.getId());

// target 유저의 팔로잉
List<Member> followingMembers =
targetMemberSources.stream().map(MemberRelation::getTarget).toList();

// target 유저의 팔로워
List<Member> followerMembers =
targetMemberTargets.stream().map(MemberRelation::getSource).toList();

// 팔로잉 리스트 구하기
getFollowStatusIncludeList(
followingMembers, currentMemberSources, followingList, currentMemberTargets);

// 팔로워 리스트 구하기
getFollowStatusIncludeList(
followerMembers, currentMemberSources, followerList, currentMemberTargets);

return FollowListResponse.of(
targetMember.getProfile().getNickname(), followingList, followerList);
}

private static void getFollowStatusIncludeList(
List<Member> targetMembers,
List<MemberRelation> currentMemberSources,
List<MemberSearchResponse> resultList,
List<MemberRelation> currentMemberTargets) {
for (Member member : targetMembers) {
boolean existRelation = false;
for (MemberRelation memberRelation : currentMemberSources) {
if (member.getId().equals(memberRelation.getTarget().getId())) {
existRelation = true;
break;
}
}

if (existRelation) { // 조회 된 애들 중 내가 팔로우한 애라면
resultList.add(MemberSearchResponse.toFollowingResponse(member));
continue;
}

// 내가 팔로우를 하지 않았을 때
Optional<MemberRelation> optionalMemberRelation =
currentMemberTargets.stream()
.filter(
memberRelation ->
member.getId()
.equals(memberRelation.getSource().getId()))
.findFirst();
if (optionalMemberRelation.isPresent()) { // 상대방만 나를 팔로우 하고 있을 때
resultList.add(MemberSearchResponse.toFollowedByMeResponse(member));
continue;
}

// 아니라면 서로 팔로우가 아닌 상태
resultList.add(MemberSearchResponse.toNotFollowingResponse(member));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.depromeet.domain.follow.dto.response;

import com.depromeet.domain.member.dto.response.MemberSearchResponse;
import java.util.List;

public record FollowListResponse(
String targetNickname,
List<MemberSearchResponse> followingList,
List<MemberSearchResponse> followerList) {
public static FollowListResponse of(
String targetNickname,
List<MemberSearchResponse> followingList,
List<MemberSearchResponse> followerList) {
return new FollowListResponse(targetNickname, followingList, followerList);
}
}
Loading