Skip to content

Commit

Permalink
♻️ refactor(api): change response results
Browse files Browse the repository at this point in the history
  • Loading branch information
siyeonSon committed Oct 16, 2024
1 parent 301c194 commit ce0761a
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.depromeet.domains.music.event.CreateSongGenreEvent;
import com.depromeet.domains.music.song.repository.SongRepository;
import com.depromeet.domains.recommend.dto.response.MusicInfoResponseDto;
import com.depromeet.domains.recommend.dto.response.RecommendCategoryDto;
import com.depromeet.domains.recommend.dto.response.SearchRecommendCategoryDto;
import com.depromeet.domains.recommend.constant.RecommendType;
import com.depromeet.music.album.Album;
import com.depromeet.music.album.AlbumCover;
Expand Down Expand Up @@ -125,11 +125,11 @@ public MusicResponseDto getMusic(Long songId) {
}

@Transactional(readOnly = true)
public RecommendCategoryDto getRecentMusic(RecommendType recommendType) {
public SearchRecommendCategoryDto getRecentMusic(RecommendType recommendType) {
var recentSongs = songRepository.findRecentSongs(recommendType.getLimit());
List<MusicInfoResponseDto> musicInfoResponseDtos = recentSongs.stream()
.map(MusicInfoResponseDto::ofSong)
.toList();
return RecommendCategoryDto.ofMusicInfoResponseDto(recommendType, musicInfoResponseDtos);
return SearchRecommendCategoryDto.ofMusicInfoResponseDto(recommendType, musicInfoResponseDtos);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
@Getter
@AllArgsConstructor
public enum RecommendType {
POPULAR_CHART_SONG("지금 인기 있는 음악", 30, true),
RECENT_SONGS("최근 드랍된 음악", 15, true),
CHART_ARTIST("아티스트", 10, false);
POPULAR_CHART_SONG("지금 인기 있는 음악", "basic", "애플 뮤직의 '지금 인기 있는 곡' 리스트를 반영했어요.", 30, true),
RECENT_SONGS("최근 드랍된 음악", "basic", "", 15, true),
CHART_ARTIST("아티스트", "keyword", "", 10, false);

private final String title;
private final String type;
private final String description;
private final int limit;
private final boolean nextPage;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.depromeet.domains.recommend.controller;

import com.depromeet.common.dto.ResponseDto;
import com.depromeet.domains.recommend.dto.response.RecommendResponseDto;
import com.depromeet.domains.recommend.dto.response.SearchRecommendResponseDto;
import com.depromeet.domains.recommend.dto.response.SearchTermRecommendResponseDto;
import com.depromeet.domains.recommend.service.SearchRecommendService;
import io.swagger.v3.oas.annotations.Operation;
Expand Down Expand Up @@ -29,7 +29,7 @@ public ResponseEntity<SearchTermRecommendResponseDto> recommendSearchTerm() {

@Operation(summary = "검색어 추천 v2")
@GetMapping("/v2/search-term/recommend")
public ResponseEntity<RecommendResponseDto> recommendSearchTerm2() {
public ResponseEntity<SearchRecommendResponseDto> recommendSearchTerm2() {
var response = searchRecommendService.recommendSearchSongs();
return ResponseDto.ok(response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,37 @@

@Getter
@AllArgsConstructor
public class RecommendCategoryDto {
public class SearchRecommendCategoryDto {
private String title;
private String type;
private String description;
private List<?> content;
private boolean nextPage;

public static RecommendCategoryDto ofMusicInfoResponseDto(RecommendType recommendType, List<MusicInfoResponseDto> musicInfoResponseDto) {
return new RecommendCategoryDto(recommendType.getTitle(), musicInfoResponseDto, recommendType.isNextPage());
public static SearchRecommendCategoryDto ofMusicInfoResponseDto(RecommendType recommendType, List<MusicInfoResponseDto> musicInfoResponseDto) {
return new SearchRecommendCategoryDto(
recommendType.getTitle(),
recommendType.getType(),
recommendType.getDescription(),
musicInfoResponseDto,
recommendType.isNextPage()
);
}


public static RecommendCategoryDto ofAppleMusicResponseDto(RecommendType recommendType, AppleMusicSongChartResponseDto appleMusicSongChartResponseDto) {
public static SearchRecommendCategoryDto ofAppleMusicResponseDto(RecommendType recommendType, AppleMusicSongChartResponseDto appleMusicSongChartResponseDto) {
List<MusicInfoResponseDto> musicInfoList = Optional.ofNullable(appleMusicSongChartResponseDto.results.songs)
.filter(songs -> !songs.isEmpty())
.map(songs -> songs.get(0).data.stream()
.map(MusicInfoResponseDto::fromAppleMusicResponse)
.toList()
)
.orElse(Collections.emptyList());
return new RecommendCategoryDto(recommendType.getTitle(), musicInfoList, recommendType.isNextPage());
return new SearchRecommendCategoryDto(recommendType.getTitle(), recommendType.getType(), recommendType.getDescription(),
musicInfoList, recommendType.isNextPage());
}

public static RecommendCategoryDto ofAppleMusicResponseDto(RecommendType recommendType, AppleMusicAlbumChartResponseDto appleMusicAlbumChartResponseDto) {
public static SearchRecommendCategoryDto ofAppleMusicResponseDto(RecommendType recommendType, AppleMusicAlbumChartResponseDto appleMusicAlbumChartResponseDto) {
List<ArtistInfoResponseDto> artistInfoList =
Optional.ofNullable(appleMusicAlbumChartResponseDto.results.albums)
.filter(albums -> !albums.isEmpty())
Expand All @@ -42,6 +51,7 @@ public static RecommendCategoryDto ofAppleMusicResponseDto(RecommendType recomme
.toList()
)
.orElse(Collections.emptyList());
return new RecommendCategoryDto(recommendType.getTitle(), artistInfoList, recommendType.isNextPage());
return new SearchRecommendCategoryDto(recommendType.getTitle(), recommendType.getType(), recommendType.getDescription(),
artistInfoList, recommendType.isNextPage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@

@Getter
@AllArgsConstructor
public class RecommendResponseDto {
private List<RecommendCategoryDto> data;
public class SearchRecommendResponseDto {
private List<SearchRecommendCategoryDto> data;
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public SearchTermRecommendResponseDto recommendSearchTerm() {
return new SearchTermRecommendResponseDto(description, termList);
}

public RecommendResponseDto recommendSearchSongs() {
return new RecommendResponseDto(
public SearchRecommendResponseDto recommendSearchSongs() {
return new SearchRecommendResponseDto(
List.of(
appleMusicService.getCategoryChart(RecommendType.POPULAR_CHART_SONG),
musicService.getRecentMusic(RecommendType.RECENT_SONGS),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.depromeet.common.error.dto.CommonErrorCode;
import com.depromeet.common.error.exception.internal.BusinessException;
import com.depromeet.domains.recommend.dto.response.RecommendCategoryDto;
import com.depromeet.domains.recommend.dto.response.SearchRecommendCategoryDto;
import com.depromeet.domains.recommend.constant.RecommendType;
import com.depromeet.external.feign.client.AppleMusicFeignClient;
import lombok.RequiredArgsConstructor;
Expand All @@ -14,15 +14,15 @@ public class AppleMusicService {

private final AppleMusicFeignClient appleMusicFeignClient;

public RecommendCategoryDto getCategoryChart(RecommendType recommendType) {
public SearchRecommendCategoryDto getCategoryChart(RecommendType recommendType) {
return switch (recommendType) {
case POPULAR_CHART_SONG -> {
var response = appleMusicFeignClient.getSongCharts("songs", recommendType.getLimit());
yield RecommendCategoryDto.ofAppleMusicResponseDto(recommendType, response);
yield SearchRecommendCategoryDto.ofAppleMusicResponseDto(recommendType, response);
}
case CHART_ARTIST -> {
var response = appleMusicFeignClient.getAlbumCharts("albums", recommendType.getLimit());
yield RecommendCategoryDto.ofAppleMusicResponseDto(recommendType, response);
yield SearchRecommendCategoryDto.ofAppleMusicResponseDto(recommendType, response);
}
default -> throw new BusinessException(CommonErrorCode.UNSUPPORTED_TYPE);
};
Expand Down

0 comments on commit ce0761a

Please sign in to comment.