Skip to content

Commit

Permalink
feat: 내 팔로워 조회 시, 팔로잉 여부 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
jang-namu committed May 5, 2024
1 parent d093a29 commit 11a72f4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ public ResponseEntity<Void> addFollow(@Valid @RequestBody FollowRequest followRe
*/
@GetMapping("/v1/follows/mine/follower")
@Operation(summary = "follower 조회", description = "현재 사용자를 follow하는 follower 목록을 조회합니다.")
public ResponseEntity<List<FollowResponse>> searchFollowing(@AuthenticationPrincipal UserDetails userDetails) {

public ResponseEntity<List<FollowResponse.MyFollowResponse>> searchFollowing(@AuthenticationPrincipal UserDetails userDetails) {
return ResponseEntity.ok(followService.searchFollowers(userDetails));
}

Expand Down
23 changes: 21 additions & 2 deletions src/main/java/com/bugflix/weblog/follow/dto/FollowResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,39 @@
import com.bugflix.weblog.profile.domain.Profile;
import com.bugflix.weblog.user.domain.User;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "Follower 혹은 Following 중인 사용자의 정보 DTO")
public class FollowResponse {
@Schema(description = "nickname", example = "namu")
private String nickname;
@Schema(description = "Follower 혹은 Following 중인 사용자의 profile image url", example = "http://~")
private String profileImageUrl;

@Getter
@NoArgsConstructor
public static class MyFollowResponse extends FollowResponse {
@Schema(description = "팔로잉 상태", example = "false")
private Boolean followed;

private MyFollowResponse(User user, Profile profile, Boolean followed) {
super(user.getNickname(), profile.getImageUrl());
this.followed = followed;
}

public static MyFollowResponse of(User user, Profile profile, Boolean followed) {
return new MyFollowResponse(user, profile, followed);
}
}

private FollowResponse(String nickname, String profileImageUrl) {
this.nickname = nickname;
this.profileImageUrl = profileImageUrl;
}

private FollowResponse(User user, Profile profile) {
this.nickname = user.getNickname();
this.profileImageUrl = profile.getImageUrl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import java.util.Set;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
Expand All @@ -33,19 +33,22 @@ public void addFollow(FollowRequest followRequest, UserDetails userDetails) {
}


public List<FollowResponse> searchFollowers(UserDetails userDetails) {
public List<FollowResponse.MyFollowResponse> searchFollowers(UserDetails userDetails) {
User user = ((CustomUserDetails) userDetails).getUser();
List<Follow> follows = followRepository.findByFollowing(user);
return follows.stream()

List<Follow> toFollows = followRepository.findByFollowing(user);
List<Follow> fromFollows = followRepository.findByFollower(user);
Set<Long> followings = fromFollows.stream().map(Follow::getFollowing).map(User::getUserId).collect(Collectors.toSet());
return toFollows.stream()
.map(Follow::getFollower)
.map(follower -> FollowResponse.of(follower, follower.getProfile()))
// User - Profile 1대1 매핑 (Default=EAGER)
.map(follower -> FollowResponse.MyFollowResponse.of(follower, follower.getProfile(), followings.contains(follower.getUserId())))
.toList();
}

public List<FollowResponse> searchFollwings(UserDetails userDetails) {
User user = ((CustomUserDetails) userDetails).getUser();
List<Follow> follows = followRepository.findByFollower(user);
ArrayList<FollowResponse> followResponses = new ArrayList<>();

return follows.stream()
.map(Follow::getFollowing)
Expand All @@ -67,7 +70,6 @@ public List<FollowResponse> searchFollwings(String nickname) {
User user = userRepository.findByNickname(nickname)
.orElseThrow(() -> new ResourceNotFoundException(Errors.USER_NOT_FOUND));
List<Follow> follows = followRepository.findByFollower(user);
ArrayList<FollowResponse> followResponses = new ArrayList<>();

return follows.stream()
.map(Follow::getFollowing)
Expand Down

0 comments on commit 11a72f4

Please sign in to comment.