diff --git a/src/main/java/org/sopt/confeti/global/util/artistsearcher/ArtistResolver.java b/src/main/java/org/sopt/confeti/global/util/artistsearcher/ArtistResolver.java index 87fd309..3b48476 100644 --- a/src/main/java/org/sopt/confeti/global/util/artistsearcher/ArtistResolver.java +++ b/src/main/java/org/sopt/confeti/global/util/artistsearcher/ArtistResolver.java @@ -2,9 +2,12 @@ import java.util.ArrayList; import java.util.HashMap; +import java.util.LinkedList; import java.util.List; import java.util.Objects; +import java.util.Queue; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentLinkedQueue; import lombok.AccessLevel; import lombok.RequiredArgsConstructor; import org.sopt.confeti.annotation.Resolver; @@ -12,6 +15,8 @@ import org.sopt.confeti.domain.concert.Concert; import org.sopt.confeti.domain.festival.Festival; import org.sopt.confeti.domain.festivaldate.FestivalDate; +import org.sopt.confeti.global.exception.ConfetiException; +import org.sopt.confeti.global.message.ErrorMessage; import org.sopt.confeti.global.util.IntegrateFunction; @Resolver @@ -39,7 +44,7 @@ public class ArtistResolver { }}; private List artistIds; - private HashMap artistMapper; + private HashMap> artistMapper; private final SpotifyAPIHandler spotifyAPIHandler; @@ -97,17 +102,14 @@ private void collectArtistFavorite(final Object target) { ArtistFavorite artistFavorite = (ArtistFavorite) target; ConfetiArtist confetiArtist = artistFavorite.getArtist(); artistIds.add(confetiArtist.getArtistId()); - artistMapper.put(confetiArtist.getArtistId(), artistFavorite.getArtist()); + addArtistToMapper(confetiArtist.getArtistId(), artistFavorite.getArtist()); } private void collectConcert(final Object target) { Concert concert = (Concert) target; concert.getArtists().forEach(artist -> { artistIds.add(artist.getArtist().getArtistId()); - artistMapper.put( - artist.getArtist().getArtistId(), - artist.getArtist() - ); + addArtistToMapper(artist.getArtist().getArtistId(), artist.getArtist()); }); } @@ -119,7 +121,7 @@ private void collectFestival(final Object target) { .flatMap(time -> time.getArtists().stream()) .forEach(artist -> { artistIds.add(artist.getArtist().getArtistId()); - artistMapper.put(artist.getArtist().getArtistId(), artist.getArtist()); + addArtistToMapper(artist.getArtist().getArtistId(), artist.getArtist()); }); } @@ -130,13 +132,18 @@ private void collectFestivalDate(final Object target) { .flatMap(time -> time.getArtists().stream()) .forEach(artist -> { artistIds.add(artist.getArtist().getArtistId()); - artistMapper.put(artist.getArtist().getArtistId(), artist.getArtist()); + addArtistToMapper(artist.getArtist().getArtistId(), artist.getArtist()); }); } private void injection(final List confetiArtists) { confetiArtists.forEach((confetiArtist -> { - ConfetiArtist mappedConfetiArtist = artistMapper.get(confetiArtist.getArtistId()); + ConfetiArtist mappedConfetiArtist = artistMapper.get(confetiArtist.getArtistId()).poll(); + + if (mappedConfetiArtist == null) { + throw new ConfetiException(ErrorMessage.INTERNAL_SERVER_ERROR); + } + mappedConfetiArtist.setName(confetiArtist.getName()); mappedConfetiArtist.setProfileUrl(confetiArtist.getProfileUrl()); })); @@ -145,4 +152,16 @@ private void injection(final List confetiArtists) { private List searchByArtistIds(final List artistIds) { return spotifyAPIHandler.findArtistsByArtistIdsEntry(artistIds); } + + private void addArtistToMapper(final String artistId, final ConfetiArtist artist) { + if (!artistMapper.containsKey(artistId)) { + createQueueToMapper(artistId); + } + + artistMapper.get(artistId).add(artist); + } + + private void createQueueToMapper(final String artistId) { + artistMapper.put(artistId, new LinkedList<>()); + } } diff --git a/src/main/java/org/sopt/confeti/global/util/artistsearcher/SpotifyAPIHandler.java b/src/main/java/org/sopt/confeti/global/util/artistsearcher/SpotifyAPIHandler.java index adda83c..0ca98f4 100644 --- a/src/main/java/org/sopt/confeti/global/util/artistsearcher/SpotifyAPIHandler.java +++ b/src/main/java/org/sopt/confeti/global/util/artistsearcher/SpotifyAPIHandler.java @@ -130,6 +130,8 @@ public List findArtistsByArtistIdsEntry(final List artist } public List findArtistsByArtistIds(final List artistIds) { + artistIds.forEach(artistId -> System.out.print(artistId + ",")); + System.out.println(); if (artistIds.isEmpty()) { return Collections.emptyList(); }