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

플레이리스트 정보에 음악 개수, 썸네일 링크 추가 #221

Merged
merged 5 commits into from
Nov 29, 2023
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
15 changes: 15 additions & 0 deletions server/src/entity/music_playlist.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,19 @@ export class Music_Playlist extends BaseEntity {
take: 10,
}).then((a: Music_Playlist[]) => a.map((b) => b.music));
}

static async getMusicCountByPlaylistId(playlist_id: number): Promise<number> {
return this.count({ where: { playlist: { playlist_id } } });
}

static async getThumbnailByPlaylistId(
playlist_id: number,
): Promise<Music_Playlist> {
return this.findOne({
relations: { music: true },
select: { music: { cover: true } },
where: { playlist: { playlist_id } },
order: { music_playlist_id: 'DESC' },
});
}
}
Comment on lines +85 to 95
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이렇게 하나씩 떼어서 가져와야 하는 군요 ㅠㅠㅠ 고생하셨어요 👍😊🙇‍♀️
typeorm 쿼리문(?)들은 항상 제가 배워가는 것 같네요

2 changes: 1 addition & 1 deletion server/src/music/music.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class MusicController {
}

@Get('info')
@HttpCode(HTTP_STATUS_CODE.SERVER_ERROR)
@HttpCode(HTTP_STATUS_CODE.SUCCESS)
async getMusicInfo(@Query('music_id') music_id: string): Promise<Music> {
return this.musicService.getMusicInfo(music_id);
}
Expand Down
15 changes: 14 additions & 1 deletion server/src/playlist/playlist.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,20 @@ export class PlaylistService {

async getUserPlaylists(userId: string): Promise<Playlist[]> {
try {
return Playlist.getPlaylistsByUserId(userId);
const playlists: Playlist[] = await Playlist.getPlaylistsByUserId(userId);
const countPromises = playlists.map(async (playlist) => {
playlist['music_count'] =
await Music_Playlist.getMusicCountByPlaylistId(playlist.playlist_id);
});
const thumbnailPromises = playlists.map(async (playlist) => {
const target = await Music_Playlist.getThumbnailByPlaylistId(
playlist.playlist_id,
);
playlist['thumbnail'] = !target ? null : target.music.cover;
});
await Promise.all(countPromises);
await Promise.all(thumbnailPromises);
Comment on lines +180 to +181
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

한번에 묶어서 처리하신게 좋아보입니다!! 👍😊
각각의 플레이리스트마다 모두 썸네일이랑 음악 개수를 순차적으로 넣어줘야 해서 Promise.all를 쓰신거죠 ?!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

각각 플레이리스트마다 쿼리문이 발생하는데, 그 쿼리문들을 for 문으로 모두 동기적으로 처리하면 시간이 오래 걸리니까 Promise.all 로 비동기적으로 병렬 처리를 했습니다!

return playlists;
} catch {
throw new CatchyException(
'SERVER_ERROR',
Expand Down