Skip to content

Commit b4861b8

Browse files
authored
Refactor : 팔로우, 팔로잉 목록 조회 DTO 수정 (#59)
1 parent 92ea07a commit b4861b8

3 files changed

Lines changed: 51 additions & 15 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.example.cp_main_be.domain.social.follow.dto;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Getter;
6+
import lombok.NoArgsConstructor;
7+
8+
@Getter
9+
@Builder
10+
@AllArgsConstructor
11+
@NoArgsConstructor
12+
public class FollowResponseDTO {
13+
private Long userId;
14+
private String username;
15+
private String userImageUrl;
16+
}

src/main/java/com/example/cp_main_be/domain/social/follow/presentation/FollowController.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.example.cp_main_be.domain.member.user.domain.User;
44
// UserService import 추가
5+
import com.example.cp_main_be.domain.social.follow.dto.FollowResponseDTO;
56
import com.example.cp_main_be.domain.social.follow.service.FollowService;
67
import com.example.cp_main_be.global.common.ApiResponse;
78
import io.swagger.v3.oas.annotations.Operation;
@@ -36,17 +37,17 @@ public ResponseEntity<ApiResponse<Void>> unfollowUser(
3637
return ResponseEntity.ok(ApiResponse.success(null));
3738
}
3839

39-
@Operation(summary = "팔로워 조회", description = "나를 팔로우하는 유저 목록을 조회합니다")
40+
@Operation(summary = "팔로워 조회", description = "팔로우하는 유저 목록을 조회합니다")
4041
@GetMapping("/{userId}/followers")
41-
public ResponseEntity<ApiResponse<List<User>>> getFollowers(@PathVariable Long userId) {
42-
List<User> followers = followService.getFollowers(userId);
43-
return ResponseEntity.ok(ApiResponse.success(followers));
42+
public ResponseEntity<ApiResponse<List<FollowResponseDTO>>> getFollowers(@PathVariable Long userId) {
43+
List<FollowResponseDTO> followResponseDTOS = followService.getFollowers(userId);
44+
return ResponseEntity.ok(ApiResponse.success(followResponseDTOS));
4445
}
4546

46-
@Operation(summary = "팔로잉 조회", description = "내가 팔로우하는 유저 목록을 조회합니다")
47+
@Operation(summary = "팔로잉 조회", description = "팔로우하는 유저 목록을 조회합니다")
4748
@GetMapping("/{userId}/following")
48-
public ResponseEntity<ApiResponse<List<User>>> getFollowing(@PathVariable Long userId) {
49-
List<User> following = followService.getFollowing(userId);
50-
return ResponseEntity.ok(ApiResponse.success(following));
49+
public ResponseEntity<ApiResponse<List<FollowResponseDTO>>> getFollowing(@PathVariable Long userId) {
50+
List<FollowResponseDTO> followResponseDTOS = followService.getFollowing(userId);
51+
return ResponseEntity.ok(ApiResponse.success(followResponseDTOS));
5152
}
5253
}

src/main/java/com/example/cp_main_be/domain/social/follow/service/FollowService.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.example.cp_main_be.domain.member.user.domain.repository.UserRepository;
77
import com.example.cp_main_be.domain.social.follow.domain.Follow;
88
import com.example.cp_main_be.domain.social.follow.domain.repository.FollowRepository;
9+
import com.example.cp_main_be.domain.social.follow.dto.FollowResponseDTO;
910
import com.example.cp_main_be.global.exception.UserNotFoundException;
1011
import java.util.List;
1112
import java.util.stream.Collectors;
@@ -63,24 +64,42 @@ public void unfollowUser(Long followerId, Long followingId) {
6364
}
6465

6566
@Transactional(readOnly = true)
66-
public List<User> getFollowers(Long userId) {
67+
public List<FollowResponseDTO> getFollowers(Long userId) {
6768
User user =
6869
userRepository
6970
.findById(userId)
7071
.orElseThrow(() -> new UserNotFoundException("사용자를 찾을 수 없습니다."));
71-
return followRepository.findByFollowing(user).stream()
72-
.map(Follow::getFollower)
73-
.collect(Collectors.toList());
72+
73+
List<User> userList = followRepository.findByFollowing(user).stream()
74+
.map(Follow::getFollower)
75+
.toList();
76+
77+
78+
return userList.stream()
79+
.map(member -> FollowResponseDTO.builder()
80+
.username(member.getUsername())
81+
.userImageUrl(member.getProfileImageUrl())
82+
.userId(member.getId())
83+
.build())
84+
.toList();
7485
}
7586

7687
@Transactional(readOnly = true)
77-
public List<User> getFollowing(Long userId) {
88+
public List<FollowResponseDTO> getFollowing(Long userId) {
7889
User user =
7990
userRepository
8091
.findById(userId)
8192
.orElseThrow(() -> new UserNotFoundException("사용자를 찾을 수 없습니다."));
82-
return followRepository.findByFollower(user).stream()
93+
List<User> userList = followRepository.findByFollower(user).stream()
8394
.map(Follow::getFollowing)
84-
.collect(Collectors.toList());
95+
.toList();
96+
97+
return userList.stream()
98+
.map(member -> FollowResponseDTO.builder()
99+
.username(member.getUsername())
100+
.userImageUrl(member.getProfileImageUrl())
101+
.userId(member.getId())
102+
.build())
103+
.toList();
85104
}
86105
}

0 commit comments

Comments
 (0)