Skip to content

Commit 0aeb250

Browse files
authored
[refactor] 팔로워 목록 조회 코드 수정 (#341)
1 parent f3d9fc8 commit 0aeb250

File tree

5 files changed

+83
-17
lines changed

5 files changed

+83
-17
lines changed

src/main/java/com/back/domain/follow/service/FollowService.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,11 @@ public List<FollowingListResponse> getMyFollowingList(Long userId) {
8686
/**
8787
* 작가의 팔로워 목록 조회 (작가 본인만 조회 가능)
8888
*/
89-
public List<FollowerListResponse> getMyFollowerList(Long artistId) {
90-
ArtistProfile artist = getArtistById(artistId);
89+
public List<FollowerListResponse> getMyFollowerList(Long userId) {
90+
ArtistProfile artist = artistProfileRepository.findByUserId(userId)
91+
.orElseThrow(() -> new ServiceException("404", "작가 본인만 팔로워 목록을 조회할 수 있습니다."));
9192

92-
List<Follow> follows = followRepository.findFollowersByArtistId(artistId);
93+
List<Follow> follows = followRepository.findFollowersByArtistId(artist.getId());
9394

9495
return follows.stream()
9596
.map(FollowerListResponse::from)

src/main/java/com/back/domain/user/entity/User.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ public boolean isArtist() {
151151
*/
152152
public void becomeArtist() {
153153
this.role = Role.ARTIST;
154+
this.grade = Grade.GUARDIAN;
154155
this.isArtistVerified = true;
155156
this.artistVerifiedAt = LocalDateTime.now();
156157
}
@@ -164,6 +165,7 @@ public void revokeArtistRole() {
164165
}
165166

166167
this.role = Role.USER;
168+
this.grade = Grade.SPROUT;
167169
this.isArtistVerified = false;
168170
this.artistVerifiedAt = null;
169171
}

src/main/java/com/back/global/initData/DevInitData.java

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.back.global.initData;
22

3+
import com.back.domain.artist.entity.ArtistApplication;
4+
import com.back.domain.artist.entity.ArtistProfile;
5+
import com.back.domain.artist.repository.ArtistApplicationRepository;
6+
import com.back.domain.artist.repository.ArtistProfileRepository;
37
import com.back.domain.user.entity.Role;
48
import com.back.domain.user.entity.User;
59
import com.back.domain.user.repository.UserRepository;
@@ -25,6 +29,8 @@ public class DevInitData {
2529
private DevInitData self;
2630

2731
private final UserRepository userRepository;
32+
private final ArtistApplicationRepository artistApplicationRepository;
33+
private final ArtistProfileRepository artistProfileRepository;
2834
private final PasswordEncoder passwordEncoder;
2935

3036
@Bean
@@ -47,7 +53,10 @@ public void createUsers() {
4753
String email = String.format("artist%d@artist.com", i);
4854
String name = String.format("아티스트%d", i);
4955
String phone = String.format("010-1%03d-%04d", i, 1000 + i);
50-
createUser(email, "1234qwer!", name, phone, Role.ARTIST);
56+
User artist = createUser(email, "1234qwer!", name, phone, Role.ARTIST);
57+
58+
// 아티스트 프로필 생성
59+
createArtistProfile(artist, name);
5160
}
5261
log.info("아티스트 계정 3개 생성 완료");
5362

@@ -64,10 +73,10 @@ public void createUsers() {
6473
log.info("총 생성된 계정: 9개 (관리자 1개, 아티스트 3개, 일반 사용자 5개)");
6574
}
6675

67-
private void createUser(String email, String password, String name, String phone, Role role) {
76+
private User createUser(String email, String password, String name, String phone, Role role) {
6877
if (userRepository.existsByEmail(email)) {
6978
log.debug("계정 이미 존재 ({}): 스킵", email);
70-
return;
79+
return userRepository.findByEmail(email).orElseThrow();
7180
}
7281

7382
String encodedPassword = passwordEncoder.encode(password);
@@ -81,7 +90,46 @@ private void createUser(String email, String password, String name, String phone
8190
default -> throw new IllegalArgumentException("지원하지 않는 Role: " + role);
8291
}
8392

84-
userRepository.save(user);
93+
User savedUser = userRepository.save(user);
8594
log.debug("{} 계정 생성: {}", role, email);
95+
96+
return savedUser;
97+
}
98+
99+
private void createArtistProfile(User artist, String artistName) {
100+
// 이미 프로필이 있으면 스킵
101+
if (artistProfileRepository.findByUserId(artist.getId()).isPresent()) {
102+
log.debug("아티스트 프로필 이미 존재 (userId: {}): 스킵", artist.getId());
103+
return;
104+
}
105+
106+
// 더미 ArtistApplication 생성
107+
ArtistApplication application = ArtistApplication.builder()
108+
.user(artist)
109+
.ownerName(artist.getName())
110+
.email(artist.getEmail())
111+
.phone(artist.getPhone())
112+
.artistName(artistName)
113+
.businessNumber("123-45-67890")
114+
.businessName("테스트 사업자")
115+
.businessAddress("서울시 테스트구 테스트로 123")
116+
.businessAddressDetail("테스트빌딩 101호")
117+
.businessZipCode("12345")
118+
.telecomSalesNumber("2024-서울-0001")
119+
.snsAccount("@test_artist")
120+
.mainProducts("일러스트, 디지털 아트")
121+
.managerPhone(artist.getPhone())
122+
.bankName("테스트은행")
123+
.bankAccount("1234567890")
124+
.accountName(artist.getName())
125+
.build();
126+
127+
ArtistApplication savedApplication = artistApplicationRepository.save(application);
128+
129+
// ArtistProfile 생성
130+
ArtistProfile profile = ArtistProfile.fromApplication(artist, savedApplication);
131+
artistProfileRepository.save(profile);
132+
133+
log.debug("아티스트 프로필 생성 완료: {}", artistName);
86134
}
87135
}

src/main/java/com/back/global/initData/ProdInitData.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ private void createArtistAccount() {
111111
String encodedPassword = passwordEncoder.encode(artistPassword);
112112
User artist = User.createLocalUser(artistEmail, encodedPassword, name, phone);
113113
userRepository.save(artist);
114-
log.info("✓ 작가 유저 생성 완료: {} ({})", name, artistEmail);
115114

116115
// 2. ArtistApplication 생성 (신청서)
117116
ArtistApplication application = ArtistApplication.builder()
@@ -135,15 +134,14 @@ private void createArtistAccount() {
135134
.build();
136135

137136
artistApplicationRepository.save(application);
138-
log.info("✓ 작가 신청서 생성 완료 (PENDING 상태)");
139137

140138
// 3. 신청서 자동 승인 및 작가 권한 부여
141139
application.approve(1L, "시스템");
142140
artist.becomeArtist();
143141

144142
userRepository.save(artist);
145143
artistApplicationRepository.save(application);
146-
log.info("✓ 작가 신청서 승인 완료 및 작가 권한 부여 완료");
144+
log.info("✓ 작가 유저 생성 완료: {} ({})", name, artistEmail);
147145
}
148146

149147
/**

src/test/java/com/back/domain/follow/service/FollowServiceTest.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2929
import static org.mockito.ArgumentMatchers.any;
3030
import static org.mockito.BDDMockito.given;
31+
import static org.mockito.Mockito.never;
3132
import static org.mockito.Mockito.verify;
3233

3334
@ActiveProfiles("test")
@@ -327,32 +328,48 @@ class GetFollowerListTest {
327328
@DisplayName("팔로워 목록 조회 성공")
328329
void getFollowerList_Success() {
329330
// given
330-
given(artistProfileRepository.findById(1L)).willReturn(Optional.of(testArtist));
331-
given(followRepository.findFollowersByArtistId(1L))
331+
Long userId = 5L;
332+
Long artistProfileId = 1L;
333+
334+
given(testArtist.getId()).willReturn(artistProfileId);
335+
336+
given(artistProfileRepository.findByUserId(userId))
337+
.willReturn(Optional.of(testArtist));
338+
339+
given(followRepository.findFollowersByArtistId(artistProfileId))
332340
.willReturn(List.of(testFollow));
333341

334342
// when
335-
List<FollowerListResponse> response = followService.getMyFollowerList(1L);
343+
List<FollowerListResponse> response = followService.getMyFollowerList(userId);
336344

337345
// then
338346
assertThat(response).hasSize(1);
339347
assertThat(response.get(0).userName()).isEqualTo("테스트유저");
340-
verify(followRepository).findFollowersByArtistId(1L);
348+
349+
verify(artistProfileRepository).findByUserId(userId);
350+
verify(followRepository).findFollowersByArtistId(artistProfileId);
341351
}
342352

343353
@Test
344-
@DisplayName("팔로워 목록 조회 실패 - 작가 없음")
354+
@DisplayName("팔로워 목록 조회 실패 - 작가 프로필 없음")
345355
void getFollowerList_ArtistNotFound() {
346356
// given
347-
given(artistProfileRepository.findById(999L)).willReturn(Optional.empty());
357+
Long userId = 999L;
358+
given(artistProfileRepository.findByUserId(userId))
359+
.willReturn(Optional.empty());
348360

349361
// when & then
350-
assertThatThrownBy(() -> followService.getMyFollowerList(999L))
362+
assertThatThrownBy(() -> followService.getMyFollowerList(userId))
351363
.isInstanceOf(ServiceException.class)
352364
.satisfies(ex -> {
353365
ServiceException serviceEx = (ServiceException) ex;
354366
assertThat(serviceEx.getResultCode()).isEqualTo("404");
367+
assertThat(serviceEx.getMessage())
368+
.contains("작가 본인만 팔로워 목록을 조회할 수 있습니다");
355369
});
370+
371+
verify(artistProfileRepository).findByUserId(userId);
372+
verify(followRepository, never()).findFollowersByArtistId(any());
356373
}
357374
}
358375

0 commit comments

Comments
 (0)