Skip to content

Commit

Permalink
merge: 유사 음악 조회 요청 변경
Browse files Browse the repository at this point in the history
Feature/#73 유사 음악 조회 요청변경
  • Loading branch information
hong-sile authored Aug 9, 2024
2 parents 792c40f + 8a66d15 commit b4d698c
Show file tree
Hide file tree
Showing 16 changed files with 73 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import play.pluv.member.domain.Member;
import play.pluv.playlist.domain.MusicStreaming;
import play.pluv.oauth.application.SocialLoginClientComposite;
import play.pluv.oauth.domain.OAuthMemberInfo;
import play.pluv.playlist.domain.MusicStreaming;

@Service
@RequiredArgsConstructor
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/play/pluv/music/application/MusicExplorer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import play.pluv.music.domain.DestinationMusic;
import play.pluv.music.domain.MusicId;
import play.pluv.playlist.domain.MusicStreaming;
import play.pluv.music.domain.SourceMusic;
import play.pluv.playlist.domain.PlayListMusic;

public interface MusicExplorer {

Optional<DestinationMusic> searchMusic(final String accessToken, final SourceMusic query);
Optional<DestinationMusic> searchMusic(final String accessToken, final PlayListMusic source);

void transferMusics(
final String accessToken, final List<MusicId> musicIds, final String playlistName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import org.springframework.stereotype.Component;
import play.pluv.music.domain.DestinationMusic;
import play.pluv.music.domain.MusicId;
import play.pluv.music.domain.SourceMusic;
import play.pluv.music.exception.MusicException;
import play.pluv.playlist.domain.MusicStreaming;
import play.pluv.playlist.domain.PlayListMusic;

@Component
public class MusicExplorerComposite {
Expand All @@ -26,9 +26,9 @@ public MusicExplorerComposite(final Set<MusicExplorer> musicExplorers) {
}

public Optional<DestinationMusic> searchMusic(
final MusicStreaming musicStreaming, final String accessToken, final SourceMusic query
final MusicStreaming musicStreaming, final String accessToken, final PlayListMusic source
) {
return getClient(musicStreaming).searchMusic(accessToken, query);
return getClient(musicStreaming).searchMusic(accessToken, source);
}

public void transferMusics(
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/play/pluv/music/application/MusicService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import play.pluv.music.domain.DestinationMusic;
import play.pluv.music.domain.MusicId;
import play.pluv.playlist.domain.MusicStreaming;
import play.pluv.music.domain.SourceMusic;
import play.pluv.playlist.domain.PlayListMusic;

@Service
@RequiredArgsConstructor
Expand All @@ -26,7 +26,7 @@ public List<MusicSearchResponse> searchMusics(

return request.musics().parallelStream()
.map(MusicQuery::toDomain)
.map(sourceMusic -> searchMusic(musicStreaming, sourceMusic, accessToken))
.map(playListMusic -> searchMusic(musicStreaming, playListMusic, accessToken))
.toList();
}

Expand All @@ -39,14 +39,14 @@ public void transferMusics(final MusicAddRequest request, final MusicStreaming d
}

private MusicSearchResponse searchMusic(
final MusicStreaming musicStreaming, final SourceMusic sourceMusic, final String accessToken
final MusicStreaming musicStreaming, final PlayListMusic playlistMusic, final String accessToken
) {
final Optional<DestinationMusic> result
= musicExplorerComposite.searchMusic(musicStreaming, accessToken, sourceMusic);
= musicExplorerComposite.searchMusic(musicStreaming, accessToken, playlistMusic);
return result
.map(
destination -> MusicSearchResponse.createFound(sourceMusic, destination)
destination -> MusicSearchResponse.createFound(playlistMusic, destination)
)
.orElseGet(() -> MusicSearchResponse.createNotFound(sourceMusic));
.orElseGet(() -> MusicSearchResponse.createNotFound(playlistMusic));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@

import java.util.Arrays;
import java.util.List;
import play.pluv.music.domain.SourceMusic;
import play.pluv.playlist.domain.PlayListMusic;

public record MusicSearchRequest(
String destinationAccessToken, List<MusicQuery> musics
) {

public record MusicQuery(
String title, String artistName, String isrcCode
String title, String artistName, String isrcCode, String imageUrl
) {

public SourceMusic toDomain() {
public PlayListMusic toDomain() {
final List<String> names = Arrays.stream(artistName.split(","))
.map(String::trim)
.toList();

return SourceMusic.builder()
return PlayListMusic.builder()
.title(title)
.artistNames(names)
.imageUrl(imageUrl)
.isrcCode(isrcCode)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.Builder;
import play.pluv.music.domain.DestinationMusic;
import play.pluv.music.domain.SourceMusic;
import play.pluv.playlist.domain.PlayListMusic;

@Builder
public record MusicSearchResponse(
Expand All @@ -13,7 +14,7 @@ public record MusicSearchResponse(
) {

public static MusicSearchResponse createFound(
final SourceMusic sourceMusic, final DestinationMusic destinationMusic
final PlayListMusic sourceMusic, final DestinationMusic destinationMusic
) {
final SourceMusicResponse sourceMusicResponse = SourceMusicResponse.from(sourceMusic);
final DestinationMusicResponse destinationMusicResponse
Expand All @@ -27,8 +28,8 @@ public static MusicSearchResponse createFound(
.build();
}

public static MusicSearchResponse createNotFound(final SourceMusic sourceMusic) {
final SourceMusicResponse response = SourceMusicResponse.from(sourceMusic);
public static MusicSearchResponse createNotFound(final PlayListMusic playListMusic) {
final SourceMusicResponse response = SourceMusicResponse.from(playListMusic);
return MusicSearchResponse.builder()
.sourceMusic(response)
.isEqual(false)
Expand All @@ -38,13 +39,15 @@ public static MusicSearchResponse createNotFound(final SourceMusic sourceMusic)

public record SourceMusicResponse(
String title,
String artistName
String artistName,
String imageUrl
) {

public static SourceMusicResponse from(final SourceMusic sourceMusic) {
public static SourceMusicResponse from(final PlayListMusic playListMusic) {
return new SourceMusicResponse(
sourceMusic.getTitle(),
String.join(",", sourceMusic.getArtistNames())
playListMusic.getTitle(),
String.join(",", playListMusic.getArtistNames()),
playListMusic.getImageUrl()
);
}
}
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/play/pluv/music/domain/DestinationMusic.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.Builder;
import lombok.Getter;
import lombok.ToString;
import play.pluv.playlist.domain.PlayListMusic;

@ToString
@Getter
Expand All @@ -20,19 +21,19 @@ public class DestinationMusic {
//nullable한 값
private final String isrcCode;

public Boolean isSame(final SourceMusic sourceMusic) {
public Boolean isSame(final PlayListMusic playListMusic) {
if (isrcCode != null) {
return sourceMusic.getIsrcCode()
return playListMusic.getIsrcCode()
.map(isrc -> isrc.equals(isrcCode))
.orElseGet(() -> compareName(sourceMusic));
.orElseGet(() -> compareName(playListMusic));
}
return compareName(sourceMusic);
return compareName(playListMusic);
}

private Boolean compareName(final SourceMusic sourceMusic) {
private Boolean compareName(final PlayListMusic playListMusic) {
final Set<String> set1 = new HashSet<>(artistNames);
final Set<String> set2 = new HashSet<>(sourceMusic.getArtistNames());
final Set<String> set2 = new HashSet<>(playListMusic.getArtistNames());

return Objects.equals(sourceMusic.getTitle(), title) && set1.equals(set2);
return Objects.equals(playListMusic.getTitle(), title) && set1.equals(set2);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package play.pluv.oauth.application;

import play.pluv.playlist.domain.MusicStreaming;
import play.pluv.oauth.domain.OAuthMemberInfo;
import play.pluv.playlist.domain.MusicStreaming;

public interface SocialLoginClient {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import java.util.Optional;
import java.util.Set;
import org.springframework.stereotype.Component;
import play.pluv.playlist.domain.MusicStreaming;
import play.pluv.oauth.domain.OAuthMemberInfo;
import play.pluv.oauth.exception.OAuthException;
import play.pluv.playlist.domain.MusicStreaming;

@Component
public class SocialLoginClientComposite {
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/play/pluv/oauth/google/GoogleConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@
import play.pluv.music.application.MusicExplorer;
import play.pluv.music.domain.DestinationMusic;
import play.pluv.music.domain.MusicId;
import play.pluv.playlist.domain.MusicStreaming;
import play.pluv.music.domain.SourceMusic;
import play.pluv.oauth.application.SocialLoginClient;
import play.pluv.oauth.domain.OAuthMemberInfo;
import play.pluv.oauth.google.dto.GoogleOAuthResponse;
import play.pluv.oauth.google.dto.YoutubeAddMusicRequest;
import play.pluv.oauth.google.dto.YoutubeCreatePlayListRequest;
import play.pluv.oauth.google.dto.YoutubeMusicResponses;
import play.pluv.playlist.application.PlayListConnector;
import play.pluv.playlist.domain.MusicStreaming;
import play.pluv.playlist.domain.PlayList;
import play.pluv.playlist.domain.PlayListId;
import play.pluv.playlist.domain.PlayListMusic;
Expand Down Expand Up @@ -73,8 +72,10 @@ public PlayListId createPlayList(final String accessToken, final String title) {
}

@Override
public Optional<DestinationMusic> searchMusic(final String accessToken, final SourceMusic query) {
final String q = query.getTitle() + String.join(",", query.getArtistNames());
public Optional<DestinationMusic> searchMusic(
final String accessToken, final PlayListMusic source
) {
final String q = source.getTitle() + String.join(",", source.getArtistNames());
return googleApiClient.searchMusic(CREATE_AUTH_HEADER.apply(accessToken), q)
.toDestinationMusic();
}
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/play/pluv/oauth/spotify/SpotifyConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import play.pluv.music.application.MusicExplorer;
import play.pluv.music.domain.DestinationMusic;
import play.pluv.music.domain.MusicId;
import play.pluv.music.domain.SourceMusic;
import play.pluv.oauth.application.SocialLoginClient;
import play.pluv.oauth.domain.OAuthMemberInfo;
import play.pluv.oauth.spotify.dto.SpotifyAddMusicRequest;
Expand Down Expand Up @@ -62,8 +61,10 @@ public MusicStreaming supportedType() {
}

@Override
public Optional<DestinationMusic> searchMusic(final String accessToken, final SourceMusic query) {
final MultiValueMap<String, String> param = createRequestParamForSearchMusic(query);
public Optional<DestinationMusic> searchMusic(
final String accessToken, final PlayListMusic source
) {
final MultiValueMap<String, String> param = createRequestParamForSearchMusic(source);
return spotifyApiClient.searchMusic(CREATE_AUTH_HEADER.apply(accessToken), param)
.toMusic();
}
Expand Down Expand Up @@ -126,7 +127,9 @@ private MultiValueMap<String, String> createRequestParamForAccessToken(final Str
return param;
}

private MultiValueMap<String, String> createRequestParamForSearchMusic(final SourceMusic music) {
private MultiValueMap<String, String> createRequestParamForSearchMusic(
final PlayListMusic music
) {
final MultiValueMap<String, String> param = new LinkedMultiValueMap<>();

final String artistName = String.join(",", music.getArtistNames());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package play.pluv.playlist.exception;

import play.pluv.base.BaseException;
import play.pluv.base.BaseExceptionType;

public class PlayListException extends BaseException {

Expand Down
2 changes: 2 additions & 0 deletions src/test/java/play/pluv/api/MusicApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class MusicApiTest extends ApiTest {
.description("플레이리스트 제공자의 accessToken"),
fieldWithPath("musics[].title").type(STRING).description("음악 이름"),
fieldWithPath("musics[].artistName").type(STRING).description("가수 이름들"),
fieldWithPath("musics[].imageUrl").type(STRING).description("음악 image url"),
fieldWithPath("musics[].isrcCode").type(STRING).description("음악의 isrc코드").optional()
),
responseFields(
Expand All @@ -43,6 +44,7 @@ public class MusicApiTest extends ApiTest {
fieldWithPath("data[].isEqual").type(BOOLEAN).description("동일한 음악인지"),
fieldWithPath("data[].isFound").type(BOOLEAN).description("찾았는지"),
fieldWithPath("data[].sourceMusic.title").type(STRING).description("검색하려는 음악의 이름"),
fieldWithPath("data[].sourceMusic.imageUrl").type(STRING).description("음악 imageUrl"),
fieldWithPath("data[].sourceMusic.artistName").type(STRING)
.description("검색하려는 음악의 가수 이름"),
fieldWithPath("data[].destinationMusic").type(OBJECT).description("조회된 음악 정보")
Expand Down
18 changes: 9 additions & 9 deletions src/test/java/play/pluv/api/fixture/MusicFixture.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,40 @@ public class MusicFixture {
public static List<MusicSearchResponse> 스포티파이_음악_검색_결과() {
return List.of(
new MusicSearchResponse(
true, true, new SourceMusicResponse("좋은 날", "아이유"),
true, true, new SourceMusicResponse("좋은 날", "아이유", "imageUrl"),
new DestinationMusicResponse("124nkd3fh", "Good Day", "IU",
"https://i.scdn.co/image/ab67616d00001e0215cf3110f19687b1a24943d1")
),
new MusicSearchResponse(true, true, new SourceMusicResponse("ㅈㅣㅂ", "hanro"),
new MusicSearchResponse(true, true, new SourceMusicResponse("ㅈㅣㅂ", "hanro", "imageUrl"),
new DestinationMusicResponse("uo890df1", "SPOT!", "제니,지코",
"https://i.scdn.co/image/ab67616d00001e024930dc9d8cdc7f5f33282538")),
new MusicSearchResponse(false, false,
new SourceMusicResponse("세상에 존재하지 않는 음악", "세상에 존재하지 않는 가수"), null
new SourceMusicResponse("세상에 존재하지 않는 음악", "세상에 존재하지 않는 가수", "imageUrl"), null
)
);
}

public static List<MusicSearchResponse> 유튜브_음악_검색_결과() {
return List.of(
new MusicSearchResponse(
true, true, new SourceMusicResponse("좋은 날", "아이유"),
true, true, new SourceMusicResponse("좋은 날", "아이유", "imageUrl"),
new DestinationMusicResponse("124nkd3fh", "Good Day - MV", "",
"https://i.scdn.co/image/ab67616d00001e0215cf3110f19687b1a24943d1")
),
new MusicSearchResponse(true, true, new SourceMusicResponse("ㅈㅣㅂ", "hanro"),
new MusicSearchResponse(true, true, new SourceMusicResponse("ㅈㅣㅂ", "hanro", "imageUrl"),
new DestinationMusicResponse("uo890df1", "SPOT! - MV (제니,지코)", "",
"https://i.scdn.co/image/ab67616d00001e024930dc9d8cdc7f5f33282538")),
new MusicSearchResponse(false, false,
new SourceMusicResponse("세상에 존재하지 않는 음악", "세상에 존재하지 않는 가수"), null
new SourceMusicResponse("세상에 존재하지 않는 음악", "세상에 존재하지 않는 가수", "imageUrl"), null
)
);
}

public static MusicSearchRequest 음악_검색_요청() {
final List<MusicQuery> musicQueries = List.of(
new MusicQuery("좋은 날", "아이유", "KRA381001057"),
new MusicQuery("SPOT!", "지코,제니", null),
new MusicQuery("세상에 존재하지 않는 음악", "세상에 존재하지 않는 가수", null)
new MusicQuery("좋은 날", "아이유", "KRA381001057", "imageUrl"),
new MusicQuery("SPOT!", "지코,제니", null, "imageUrl"),
new MusicQuery("세상에 존재하지 않는 음악", "세상에 존재하지 않는 가수", null, "imageUrl")
);

return new MusicSearchRequest("dfj51lk5", musicQueries);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ class MusicServiceTest extends ApplicationTest {
@Test
void 음악을_조회한다() {
final MusicSearchRequest request = new MusicSearchRequest("accessToken",
List.of(new MusicQuery("좋은 날", "아이유", "KRA381001057"))
List.of(new MusicQuery("좋은 날", "아이유", "KRA381001057", "imageUrl"))
);

final List<MusicSearchResponse> actual = musicService.searchMusics(SPOTIFY, request);
final List<MusicSearchResponse> expected = List.of(
new MusicSearchResponse(true, true,
new SourceMusicResponse("좋은 날", "아이유"),
new SourceMusicResponse("좋은 날", "아이유","imageUrl"),
new DestinationMusicResponse("goodDayId", "Good Day", "IU", "href")
)
);
Expand Down
Loading

0 comments on commit b4d698c

Please sign in to comment.