19
19
import project .closet .follower .entity .Follow ;
20
20
import project .closet .follower .repository .FollowRepository ;
21
21
import project .closet .follower .service .FollowService ;
22
+ import project .closet .storage .S3ContentStorage ;
22
23
import project .closet .user .entity .User ;
23
24
import project .closet .user .repository .UserRepository ;
24
25
@@ -30,6 +31,7 @@ public class BasicFollowService implements FollowService {
30
31
private final FollowRepository followRepository ;
31
32
private final UserRepository userRepository ;
32
33
private final ApplicationEventPublisher eventPublisher ;
34
+ private final S3ContentStorage s3ContentStorage ;
33
35
34
36
@ Transactional
35
37
@ Override
@@ -38,64 +40,62 @@ public FollowDto createFollow(FollowCreateRequest followCreateRequest) {
38
40
// TODO 중복 검사 로직 추가 or 데이터베이스 제약조건 추가
39
41
UUID followerId = followCreateRequest .followerId ();
40
42
User follower = userRepository .findByIdWithProfile (followerId )
41
- .orElseThrow (() -> UserNotFoundException .withId (followerId ));
43
+ .orElseThrow (() -> UserNotFoundException .withId (followerId ));
42
44
43
45
UUID followeeId = followCreateRequest .followeeId ();
44
46
User followee = userRepository .findByIdWithProfile (followeeId )
45
- .orElseThrow (() -> UserNotFoundException .withId (followeeId ));
47
+ .orElseThrow (() -> UserNotFoundException .withId (followeeId ));
46
48
47
49
Follow follow = Follow .builder ()
48
- .follower (follower )
49
- .followee (followee )
50
- .build ();
50
+ .follower (follower )
51
+ .followee (followee )
52
+ .build ();
51
53
52
54
// 알림 생성 이벤트
53
55
eventPublisher .publishEvent (new FollowCreateEvent (followeeId , follower .getName ()));
54
- return FollowDto . from (followRepository .save (follow ));
56
+ return toFollowDto (followRepository .save (follow ));
55
57
}
56
58
57
59
@ Transactional (readOnly = true )
58
60
@ Override
59
61
public FollowSummaryDto getFollowSummary (UUID userId , UUID currentUserId ) {
60
62
log .debug ("Getting follow summary for userId: {}, currentUserId: {}" , userId ,
61
- currentUserId );
63
+ currentUserId );
62
64
63
65
long followerCount = followRepository .countByFolloweeId (userId );
64
66
long followingCount = followRepository .countByFollowerId (userId );
65
67
66
68
Optional <Follow > myFollow = followRepository .findByFollowerIdAndFolloweeId (currentUserId ,
67
- userId );
69
+ userId );
68
70
boolean followedByMe = myFollow .isPresent ();
69
71
UUID followedByMeId = myFollow .map (Follow ::getId ).orElse (null );
70
72
71
73
boolean followingMe = followRepository .existsByFollowerIdAndFolloweeId (userId , currentUserId );
72
74
73
75
return new FollowSummaryDto (
74
- userId ,
75
- followerCount ,
76
- followingCount ,
77
- followedByMe ,
78
- followedByMeId ,
79
- followingMe
76
+ userId ,
77
+ followerCount ,
78
+ followingCount ,
79
+ followedByMe ,
80
+ followedByMeId ,
81
+ followingMe
80
82
);
81
83
}
82
84
83
85
@ Transactional (readOnly = true )
84
86
@ Override
85
87
public FollowListResponse getFollowingList (UUID followerId , String cursor , UUID idAfter ,
86
- int limit , String nameLike ) {
88
+ int limit , String nameLike ) {
87
89
List <Follow > follows = followRepository .findFollowingsWithCursor (
88
- followerId ,
89
- cursor != null ? Instant .parse (cursor ) : null ,
90
- idAfter ,
91
- nameLike ,
92
- limit
90
+ followerId ,
91
+ cursor != null ? Instant .parse (cursor ) : null ,
92
+ idAfter ,
93
+ nameLike ,
94
+ limit
93
95
);
94
96
boolean hasNext = follows .size () > limit ;
95
97
List <Follow > pageItems = hasNext ? follows .subList (0 , limit ) : follows ;
96
- List <FollowDto > followDtos = pageItems .stream ()
97
- .map (FollowDto ::from )
98
- .toList ();
98
+ List <FollowDto > followDtos = toFollowerDto (pageItems );
99
99
100
100
String nextCursor = null ;
101
101
UUID nextIdAfter = null ;
@@ -107,31 +107,29 @@ public FollowListResponse getFollowingList(UUID followerId, String cursor, UUID
107
107
108
108
long totalCount = followRepository .countByFollowerId (followerId );
109
109
return new FollowListResponse (
110
- followDtos ,
111
- nextCursor ,
112
- nextIdAfter ,
113
- hasNext ,
114
- totalCount ,
115
- "createdAt" ,
116
- "DESCENDING"
110
+ followDtos ,
111
+ nextCursor ,
112
+ nextIdAfter ,
113
+ hasNext ,
114
+ totalCount ,
115
+ "createdAt" ,
116
+ "DESCENDING"
117
117
);
118
118
}
119
119
120
120
@ Transactional (readOnly = true )
121
121
@ Override
122
122
public FollowListResponse getFollowerList (UUID followeeId , String cursor , UUID idAfter ,
123
- int limit , String nameLike ) {
123
+ int limit , String nameLike ) {
124
124
Instant parsedCursor = (cursor != null ) ? Instant .parse (cursor ) : null ;
125
125
126
126
List <Follow > follows = followRepository .findFollowersWithCursor (
127
- followeeId , parsedCursor , idAfter , nameLike , limit );
127
+ followeeId , parsedCursor , idAfter , nameLike , limit );
128
128
129
129
boolean hasNext = follows .size () > limit ;
130
130
List <Follow > pageItems = hasNext ? follows .subList (0 , limit ) : follows ;
131
131
132
- List <FollowDto > followDtos = pageItems .stream ()
133
- .map (FollowDto ::from )
134
- .toList ();
132
+ List <FollowDto > followDtos = toFollowerDto (pageItems );
135
133
136
134
String nextCursor = null ;
137
135
UUID nextIdAfter = null ;
@@ -144,16 +142,30 @@ public FollowListResponse getFollowerList(UUID followeeId, String cursor, UUID i
144
142
long totalCount = followRepository .countByFolloweeId (followeeId );
145
143
146
144
return new FollowListResponse (
147
- followDtos ,
148
- nextCursor ,
149
- nextIdAfter ,
150
- hasNext ,
151
- totalCount ,
152
- "createdAt" ,
153
- "DESC"
145
+ followDtos ,
146
+ nextCursor ,
147
+ nextIdAfter ,
148
+ hasNext ,
149
+ totalCount ,
150
+ "createdAt" ,
151
+ "DESC"
154
152
);
155
153
}
156
154
155
+ private FollowDto toFollowDto (Follow follow ) {
156
+ String followerImageUrl =
157
+ s3ContentStorage .getPresignedUrl (follow .getFollower ().getProfile ().getProfileImageKey ());
158
+ String followeeImageUrl =
159
+ s3ContentStorage .getPresignedUrl (follow .getFollowee ().getProfile ().getProfileImageKey ());
160
+ return FollowDto .fromWithImageUrl (follow , followeeImageUrl , followerImageUrl );
161
+ }
162
+
163
+ private List <FollowDto > toFollowerDto (List <Follow > followList ) {
164
+ return followList .stream ()
165
+ .map (this ::toFollowDto )
166
+ .toList ();
167
+ }
168
+
157
169
@ Transactional
158
170
@ Override
159
171
public void cancelFollowById (UUID followId ) {
0 commit comments