Skip to content

Commit

Permalink
feat : 사용자의 플레이리스트 삭제 API 추가
Browse files Browse the repository at this point in the history
* DB에서 playlist 열 삭제
* DB에서 music_playlist에 있는 음악 중 해당 playlist에 담긴 음악 열 모두 삭제
  • Loading branch information
sk000801 committed Jan 9, 2024
1 parent cfe5056 commit f827250
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
16 changes: 16 additions & 0 deletions server/src/playlist/playlist.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,22 @@ export class PlaylistController {
return await this.playlistService.getPlaylistMusics(userId, playlistId);
}

@Delete()
@UseGuards(AuthGuard())
@HttpCode(HTTP_STATUS_CODE.SUCCESS)
async deletePlaylist(
@Req() req,
@Body('playlistId') playlistId: number,
): Promise<{ playlistId: number }> {
this.logger.log(
`DELETE /playlists - nickname=${req.user.nickname}, playlistId=${playlistId}`,
);
const userId: string = req.user.user_id;
const deletedPlaylistId: number =
await this.playlistService.deleteSinglePlaylist(userId, playlistId);
return { playlistId: deletedPlaylistId };
}

@Delete(':playlistId')
@UseGuards(AuthGuard())
@HttpCode(HTTP_STATUS_CODE.SUCCESS)
Expand Down
63 changes: 63 additions & 0 deletions server/src/playlist/playlist.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,69 @@ export class PlaylistService {
}
}

async deleteSinglePlaylist(
userId: string,
playlistId: number,
): Promise<number> {
// 사용자 플리가 있는지 확인
if (!(await this.isExistPlaylistOnUser(playlistId, userId))) {
this.logger.error(
`playlist.service - deleteSinglePlaylist : NOT_EXIST_PLAYLIST_ON_USER`,
);
throw new CatchyException(
'NOT_EXIST_PLAYLIST_ON_USER',
HTTP_STATUS_CODE.BAD_REQUEST,
ERROR_CODE.NOT_EXIST_PLAYLIST_ON_USER,
);
}

const queryRunner = this.dataSource.createQueryRunner();
await queryRunner.startTransaction();

try {
const target: Playlist = await this.playlistRepository.findOne({
where: {
playlist_id: playlistId,
user: {
user_id: userId,
},
},
});

const targetMusics: Music_Playlist[] =
await this.music_playlistRepository.find({
where: {
playlist: {
playlist_id: playlistId,
},
},
});

await queryRunner.manager.remove(targetMusics);
await queryRunner.manager.remove(target);

await queryRunner.commitTransaction();
return playlistId;
} catch (error) {
await queryRunner.rollbackTransaction();

if (error instanceof CatchyException) {
throw error;
}

this.logger.error(
`playlist.service - deleteSinglePlaylist : SERVICE_ERROR`,
);
throw new CatchyException(
'SERVICE_ERROR',
HTTP_STATUS_CODE.BAD_REQUEST,
ERROR_CODE.SERVICE_ERROR,
);
} finally {
await queryRunner.release();
}
}

async deleteMusicInPlaylist(
userId: string,
playlistId: number,
Expand Down

0 comments on commit f827250

Please sign in to comment.