File tree Expand file tree Collapse file tree
src/main/java/sopt/comfit/company Expand file tree Collapse file tree Original file line number Diff line number Diff line change 33import lombok .Getter ;
44import lombok .RequiredArgsConstructor ;
55
6+ import java .util .ArrayList ;
7+ import java .util .Collections ;
8+ import java .util .List ;
69import java .util .concurrent .ThreadLocalRandom ;
710
811import 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}
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 2020import java .util .Collections ;
2121import java .util .List ;
2222import 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}
You can’t perform that action at this time.
0 commit comments