-
Notifications
You must be signed in to change notification settings - Fork 0
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
플레이리스트 조회 API #153
Merged
Merged
플레이리스트 조회 API #153
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7cb2444
feat : 플레이리스트 목록 조회 API 구현
khw3754 0b65866
feat : 플레이리스트 음악 추가 로직 수정
khw3754 5c0a5f6
refactor : 음악 추가 API 예외처리 추가
khw3754 c176318
feat : 플레이리스트 음악 목록 조회 API 구현
khw3754 d21ce84
refactor : Active Record 패턴 적용
khw3754 d2deb71
refactor : playlist.service 예외처리 추가
khw3754 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,17 +22,21 @@ export class PlaylistService { | |
userId: string, | ||
playlistCreateDto: PlaylistCreateDto, | ||
): Promise<number> { | ||
const title: string = playlistCreateDto.title; | ||
const newPlaylist: Playlist = this.playlistRepository.create({ | ||
playlist_title: title, | ||
created_at: new Date(), | ||
updated_at: new Date(), | ||
user: { user_id: userId }, | ||
}); | ||
try { | ||
const title: string = playlistCreateDto.title; | ||
const newPlaylist: Playlist = this.playlistRepository.create({ | ||
playlist_title: title, | ||
created_at: new Date(), | ||
updated_at: new Date(), | ||
user: { user_id: userId }, | ||
}); | ||
|
||
const result: Playlist = await this.playlistRepository.save(newPlaylist); | ||
const playlistId: number = result.playlist_Id; | ||
return playlistId; | ||
const result: Playlist = await this.playlistRepository.save(newPlaylist); | ||
const playlistId: number = result.playlist_Id; | ||
return playlistId; | ||
} catch { | ||
throw new HttpException('SERVER_ERROR', HTTP_STATUS_CODE.SERVER_ERROR); | ||
} | ||
} | ||
|
||
async addMusicToPlaylist( | ||
|
@@ -52,34 +56,101 @@ export class PlaylistService { | |
throw new HttpException('NOT_EXIST_MUSIC', HTTP_STATUS_CODE.BAD_REQUEST); | ||
} | ||
|
||
// 이미 추가된 음악인지 확인 | ||
if (await this.isAlreadyAdded(playlistId, musicId)) { | ||
throw new HttpException('ALREADY_ADDED', HTTP_STATUS_CODE.BAD_REQUEST); | ||
} | ||
|
||
// 관계테이블에 추가 | ||
const new_music_playlist: Music_Playlist = | ||
this.music_playlistRepository.create({ | ||
try { | ||
const new_music_playlist: Music_Playlist = | ||
this.music_playlistRepository.create({ | ||
music: { musicId: musicId }, | ||
playlist: { playlist_Id: playlistId }, | ||
}); | ||
|
||
const result: Music_Playlist = | ||
await this.music_playlistRepository.save(new_music_playlist); | ||
this.setUpdatedAtNow(playlistId); | ||
return result.music_playlist_id; | ||
} catch { | ||
throw new HttpException('SERVER_ERROR', HTTP_STATUS_CODE.SERVER_ERROR); | ||
} | ||
} | ||
|
||
async isAlreadyAdded(playlistId: number, musicId: number): Promise<boolean> { | ||
try { | ||
const count: number = await this.music_playlistRepository.countBy({ | ||
music: { musicId: musicId }, | ||
playlist: { playlist_Id: playlistId }, | ||
}); | ||
|
||
const result: Music_Playlist = | ||
await this.music_playlistRepository.save(new_music_playlist); | ||
return result.music_playlist_id; | ||
return count !== 0; | ||
} catch { | ||
throw new HttpException('SERVER_ERROR', HTTP_STATUS_CODE.SERVER_ERROR); | ||
} | ||
} | ||
|
||
async isExistPlaylistOnUser( | ||
playlistId: number, | ||
userId: string, | ||
): Promise<boolean> { | ||
const playlistCount: number = await this.playlistRepository.countBy({ | ||
playlist_Id: playlistId, | ||
user: { user_id: userId }, | ||
}); | ||
return playlistCount !== 0; | ||
try { | ||
const playlistCount: number = await this.playlistRepository.countBy({ | ||
playlist_Id: playlistId, | ||
user: { user_id: userId }, | ||
}); | ||
return playlistCount !== 0; | ||
} catch { | ||
throw new HttpException('SERVER_ERROR', HTTP_STATUS_CODE.SERVER_ERROR); | ||
} | ||
} | ||
|
||
async isExistMusic(musicId: number): Promise<boolean> { | ||
const musicCount: number = await this.MusicRepository.countBy({ | ||
musicId: musicId, | ||
}); | ||
try { | ||
const musicCount: number = await this.MusicRepository.countBy({ | ||
musicId: musicId, | ||
}); | ||
|
||
return musicCount !== 0; | ||
} catch { | ||
throw new HttpException('SERVER_ERROR', HTTP_STATUS_CODE.SERVER_ERROR); | ||
} | ||
} | ||
|
||
return musicCount !== 0; | ||
async setUpdatedAtNow(playlistId: number): Promise<void> { | ||
try { | ||
const targetPlaylist: Playlist = await this.playlistRepository.findOne({ | ||
where: { playlist_Id: playlistId }, | ||
}); | ||
targetPlaylist.updated_at = new Date(); | ||
this.playlistRepository.save(targetPlaylist); | ||
} catch { | ||
throw new HttpException('SERVER_ERROR', HTTP_STATUS_CODE.SERVER_ERROR); | ||
} | ||
} | ||
|
||
async getUserPlaylists(userId: string): Promise<Playlist[]> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DB에 접근하는 코드 불러오는 거니까 예외 처리 (혹시나 DB로의 접근이 실패할 경우?) try-catch 넣어주시면 좋겠습니당 😊 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 반영하고 merge 하겠습니다! |
||
try { | ||
return Playlist.getPlaylistsByUserId(userId); | ||
} catch { | ||
throw new HttpException('SERVER_ERROR', HTTP_STATUS_CODE.SERVER_ERROR); | ||
} | ||
} | ||
|
||
async getPlaylistMusics( | ||
userId: string, | ||
playlistId: number, | ||
): Promise<Music[]> { | ||
if (!(await this.isExistPlaylistOnUser(playlistId, userId))) { | ||
throw new HttpException( | ||
'NOT_EXIST_PLAYLIST_ON_USER', | ||
HTTP_STATUS_CODE.BAD_REQUEST, | ||
); | ||
} | ||
try { | ||
return Music_Playlist.getMusicListByPlaylistId(playlistId); | ||
} catch { | ||
throw new HttpException('SERVER_ERROR', HTTP_STATUS_CODE.SERVER_ERROR); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이게 엔티티에 들어간다는게 생소하지만 Custom Repository 만드는 것보다 현재는 이게 나을 것 같아요 👍