Skip to content

Commit

Permalink
refactor: 아티스트 검색 한국 가수 필터링 추가 (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
GaBaljaintheroom authored Nov 11, 2024
1 parent 0e723ce commit 91e52d4
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

public record PaginationServiceResponse<T>(
boolean hasNext,
List<T> data
List<T> data,
CursorApiResponse cursor
) {

public static <T> PaginationServiceResponse<T> of(
Expand All @@ -15,6 +16,18 @@ public static <T> PaginationServiceResponse<T> of(
data = List.of();
}

return new PaginationServiceResponse<>(hasNext, data);
return new PaginationServiceResponse<>(hasNext, data, CursorApiResponse.noneCursor());
}

public static <T> PaginationServiceResponse<T> of(
List<T> data,
boolean hasNext,
CursorApiResponse cursor
) {
if (data == null || data.isEmpty()) {
data = List.of();
}

return new PaginationServiceResponse<>(hasNext, data, cursor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public ResponseEntity<PaginationApiResponse<ArtistSearchPaginationApiParam>> sea
PaginationApiResponse.<ArtistSearchPaginationApiParam>builder()
.hasNext(response.hasNext())
.data(data)
.cursor(CursorApiResponse.toCursorId(request.cursorId() + 1))
.cursor(response.cursor())
.build()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.List;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.example.dto.response.CursorApiResponse;
import org.example.dto.response.PaginationServiceResponse;
import org.example.entity.artist.Artist;
import org.example.entity.usershow.ArtistSubscription;
Expand Down Expand Up @@ -55,7 +56,9 @@ public PaginationServiceResponse<ArtistSearchPaginationServiceParam> searchArtis
)
.toList();

return PaginationServiceResponse.of(data, response.hasNext());
return PaginationServiceResponse.of(data, response.hasNext(),
CursorApiResponse.toCursorId(response.offset())
);
}

public ArtistSubscriptionServiceResponse subscribe(ArtistSubscriptionServiceRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public record ArtistSearchPaginationDomainResponse(
boolean hasNext
) {



}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.example.usecase;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
Expand Down Expand Up @@ -31,6 +32,7 @@
import org.example.repository.artist.artistgenre.ArtistGenreRepository;
import org.example.repository.genre.GenreRepository;
import org.example.repository.show.showartist.ShowArtistRepository;
import org.example.vo.ArtistFilterType;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -119,37 +121,64 @@ public ArtistPaginationDomainResponse findAllArtistInCursorPagination(
public ArtistSearchPaginationDomainResponse searchArtist(
ArtistSearchPaginationDomainRequest request
) {
ArtistSearchPortResponse response = artistSearchPort.searchArtist(
ArtistSearchPortRequest.builder()
.accessToken(artistSearchPort.getAccessToken())
.search(request.search())
.limit(request.limit())
.offset(request.offset())
.build()
);
int requiredLimit = request.limit();
int offset = request.offset();
boolean hasNext = false;
String accessToken = artistSearchPort.getAccessToken();
List<ArtistSearchPortParam> filteredArtists = new ArrayList<>();

for (int attempt = 0; attempt < 5 && filteredArtists.size() < requiredLimit; attempt++) {
ArtistSearchPortResponse response = artistSearchPort.searchArtist(
ArtistSearchPortRequest.builder()
.accessToken(accessToken)
.search(request.search())
.limit(requiredLimit - filteredArtists.size())
.offset(offset)
.build()
);

Map<String, Artist> artistBySpotifyId = getArtistBySpotifyId(
response.getSpotifyArtistIds()
);
filteredArtists.addAll(filterKoreanArtistSearch(response));
offset += response.artists().size();
hasNext = response.hasNext();

if (filteredArtists.size() >= requiredLimit) {
break;
}
}

Map<String, Artist> artistBySpotifyId = getArtistBySpotifyId(filteredArtists);

return ArtistSearchPaginationDomainResponse.builder()
.data(
response.artists().stream()
filteredArtists.stream()
.limit(requiredLimit)
.map(it -> it.toDomainResponse(
artistBySpotifyId.getOrDefault(
artistBySpotifyId.getOrDefault(
it.id(),
null
)
)
).toList()
)
.limit(response.limit())
.offset(response.offset())
.hasNext(response.hasNext())
.limit(request.limit())
.offset(offset)
.hasNext(hasNext)
.build();
}

private Map<String, Artist> getArtistBySpotifyId(List<String> spotifyIds) {
private List<ArtistSearchPortParam> filterKoreanArtistSearch(
ArtistSearchPortResponse response
) {
return response.artists().stream()
.filter(artist -> artist.genres().stream()
.noneMatch(ArtistFilterType::isKoreanArtist))
.toList();
}

//Key : spotifyId
private Map<String, Artist> getArtistBySpotifyId(List<ArtistSearchPortParam> filteredArtists) {
List<String> spotifyIds = filteredArtists.stream().map(ArtistSearchPortParam::id).toList();

return artistRepository.findArtistsBySpotifyIdIn(spotifyIds)
.stream()
.collect(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.example.vo;

public enum ArtistFilterType {

K_START("k-"), KOREAN("korean");

private final String value;

ArtistFilterType(String value) {
this.value = value;
}

public static boolean isKoreanArtist(String genre) {
return genre.contains(K_START.value) || genre.contains(KOREAN.value);
}
}

0 comments on commit 91e52d4

Please sign in to comment.