Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ refactor(api): change response results #516

Merged
merged 3 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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", null, 15, true),
CHART_ARTIST("아티스트", "keyword", null, 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
@@ -0,0 +1,23 @@
package com.depromeet.domains.recommend.dto.response;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.List;

@Getter
@AllArgsConstructor
public class ContentTypeResponseDto {

@Getter
@AllArgsConstructor
public static class BasicTypeResponseDto {
List<MusicInfoResponseDto> basic;
}

@Getter
@AllArgsConstructor
public static class KeywordTypeResponseDto {
List<ArtistInfoResponseDto> keyword;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.depromeet.domains.recommend.dto.response;

import com.depromeet.domains.recommend.constant.RecommendType;
import com.depromeet.external.applemusic.dto.response.catalogchart.AppleMusicAlbumChartResponseDto;
import com.depromeet.external.applemusic.dto.response.catalogchart.AppleMusicSongChartResponseDto;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Getter
@AllArgsConstructor
public class SearchRecommendCategoryDto {
private String title;
private String type;
private String description;
private Object content;
private boolean nextPage;

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


public static SearchRecommendCategoryDto ofAppleMusicResponseDto(RecommendType recommendType, AppleMusicSongChartResponseDto appleMusicSongChartResponseDto) {
return new SearchRecommendCategoryDto(
recommendType.getTitle(),
recommendType.getType(),
recommendType.getDescription(),
getContentTypeResponseDto(appleMusicSongChartResponseDto),
recommendType.isNextPage()
);
}

public static SearchRecommendCategoryDto ofAppleMusicResponseDto(RecommendType recommendType, AppleMusicAlbumChartResponseDto appleMusicAlbumChartResponseDto) {
return new SearchRecommendCategoryDto(
recommendType.getTitle(),
recommendType.getType(),
recommendType.getDescription(),
getContentTypeResponseDto(appleMusicAlbumChartResponseDto),
recommendType.isNextPage()
);
}

private static ContentTypeResponseDto.BasicTypeResponseDto getContentTypeResponseDto(List<MusicInfoResponseDto> musicInfoResponseDto) {
return new ContentTypeResponseDto.BasicTypeResponseDto(musicInfoResponseDto);
}

private static ContentTypeResponseDto.BasicTypeResponseDto getContentTypeResponseDto(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 ContentTypeResponseDto.BasicTypeResponseDto(musicInfoList);
}


private static ContentTypeResponseDto.KeywordTypeResponseDto getContentTypeResponseDto(AppleMusicAlbumChartResponseDto appleMusicAlbumChartResponseDto) {
List<ArtistInfoResponseDto> artistInfoList =
Optional.ofNullable(appleMusicAlbumChartResponseDto.results.albums)
.filter(albums -> !albums.isEmpty())
.map(albums -> albums.get(0).data.stream()
.map(ArtistInfoResponseDto::fromAppleMusicResponse)
.toList()
)
.orElse(Collections.emptyList());
return new ContentTypeResponseDto.KeywordTypeResponseDto(artistInfoList);
}
}
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
Loading