Skip to content

Commit 5c99ebf

Browse files
committed
refactor: Random Photo URL, Not Duplicated #71
1 parent d82b6b0 commit 5c99ebf

3 files changed

Lines changed: 26 additions & 7 deletions

File tree

src/main/java/sopt/comfit/company/domain/ERandomPhoto.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import lombok.Getter;
44
import lombok.RequiredArgsConstructor;
55

6+
import java.util.ArrayList;
7+
import java.util.Collections;
8+
import java.util.List;
69
import java.util.concurrent.ThreadLocalRandom;
710

811
import static sopt.comfit.global.constants.Constants.BASE_URL;
@@ -26,7 +29,15 @@ public enum ERandomPhoto {
2629

2730
private static final ERandomPhoto[] VALUES = values();
2831

29-
public static ERandomPhoto random() {
30-
return VALUES[ThreadLocalRandom.current().nextInt(VALUES.length)];
32+
public static List<ERandomPhoto> randomDistinct(int count) {
33+
if (count > VALUES.length) {
34+
throw new IllegalArgumentException("count is greater than total photos");
35+
}
36+
37+
List<ERandomPhoto> shuffled = new ArrayList<>(List.of(VALUES));
38+
Collections.shuffle(shuffled);
39+
40+
return shuffled.subList(0, count);
3141
}
42+
3243
}

src/main/java/sopt/comfit/company/dto/response/FeaturedCompanyResponseDto.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ public record FeaturedCompanyResponseDto(
1010
String scale,
1111
String photoUrl
1212
) {
13-
public static FeaturedCompanyResponseDto from(Company company) {
13+
public static FeaturedCompanyResponseDto of(Company company, String randomPhotoUrl) {
1414
return new FeaturedCompanyResponseDto(
1515
company.getId(),
1616
company.getName(),
1717
company.getScale().name(),
18-
ERandomPhoto.random().getPhotoUrl()
18+
randomPhotoUrl
1919
);
2020
}
2121
}

src/main/java/sopt/comfit/company/service/CompanyService.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Collections;
2121
import java.util.List;
2222
import java.util.stream.Collectors;
23+
import java.util.stream.IntStream;
2324

2425
@Service
2526
@RequiredArgsConstructor
@@ -111,9 +112,16 @@ public List<GetSuggestionCompanyResponseDto> getSuggestionCompany(Long companyId
111112
private List<FeaturedCompanyResponseDto> getFeaturedCompany(List<Long> ids){
112113
Collections.shuffle(ids);
113114
List<Long> randomIds = ids.subList(0, Math.min(3, ids.size()));
115+
List<Company> companies = companyRepository.findAllById(randomIds);
114116

115-
return companyRepository.findAllById(randomIds).stream()
116-
.map(FeaturedCompanyResponseDto::from)
117-
.collect(Collectors.toList());
117+
List<ERandomPhoto> randomPhotos =
118+
ERandomPhoto.randomDistinct(companies.size());
119+
120+
return IntStream.range(0, companies.size())
121+
.mapToObj(i -> FeaturedCompanyResponseDto.of(
122+
companies.get(i),
123+
randomPhotos.get(i).getPhotoUrl()
124+
))
125+
.toList();
118126
}
119127
}

0 commit comments

Comments
 (0)