diff --git a/springSpotifyPlayList/README.md b/springSpotifyPlayList/README.md index 61b820ea8..93b2ee9cd 100644 --- a/springSpotifyPlayList/README.md +++ b/springSpotifyPlayList/README.md @@ -122,8 +122,16 @@ docker rmi -f $(docker images -q -a) + ## TODO +
+TODO 1. code refactor -2. integrate with chatGPT \ No newline at end of file + - dep injection : `spotifyApi` +2. integrate with chatGPT +3. CICD, dockerize project +4. UI redesign + +
\ No newline at end of file diff --git a/springSpotifyPlayList/backend/SpotifyPlayList/src/main/java/com/yen/SpotifyPlayList/controller/UserDataController.java b/springSpotifyPlayList/backend/SpotifyPlayList/src/main/java/com/yen/SpotifyPlayList/controller/UserDataController.java index 71ceb4df6..a722711b2 100644 --- a/springSpotifyPlayList/backend/SpotifyPlayList/src/main/java/com/yen/SpotifyPlayList/controller/UserDataController.java +++ b/springSpotifyPlayList/backend/SpotifyPlayList/src/main/java/com/yen/SpotifyPlayList/controller/UserDataController.java @@ -23,6 +23,6 @@ public class UserDataController { @GetMapping("/playlist") public PlaylistSimplified[] getUserPlayList(){ //String userId = "62kytpy7jswykfjtnjn9zv3ou"; - return userDataService.getUserAllPlayList(userId); + return userDataService.getUserAllPlaylists(userId); } } diff --git a/springSpotifyPlayList/backend/SpotifyPlayList/src/main/java/com/yen/SpotifyPlayList/service/PlayListService.java b/springSpotifyPlayList/backend/SpotifyPlayList/src/main/java/com/yen/SpotifyPlayList/service/PlayListService.java index 5ffd3d619..ca17bd17e 100644 --- a/springSpotifyPlayList/backend/SpotifyPlayList/src/main/java/com/yen/SpotifyPlayList/service/PlayListService.java +++ b/springSpotifyPlayList/backend/SpotifyPlayList/src/main/java/com/yen/SpotifyPlayList/service/PlayListService.java @@ -26,46 +26,54 @@ public class PlayListService { @Autowired private ProfileService profileService; - private SpotifyApi spotifyApi; - - - public PlayListService() { - - } - + /** + * Fetches a playlist by its ID. + * + * @param playlistId The Spotify playlist ID. + * @return Playlist object + * @throws SpotifyWebApiException In case of errors while fetching the playlist. + */ public Playlist getPlaylistById(String playlistId) throws SpotifyWebApiException { Playlist playlist = null; + try { - // TODO : move below to controller / config - this.spotifyApi = authService.getSpotifyClient(); - final GetPlaylistRequest getPlaylistRequest = spotifyApi + // Get SpotifyApi instance from AuthService + SpotifyApi spotifyApi = authService.initializeSpotifyApi(); + + // Build and execute request to get the playlist + GetPlaylistRequest getPlaylistRequest = spotifyApi .getPlaylist(playlistId) .build(); playlist = getPlaylistRequest.execute(); - log.info("playlist = " + playlist); + + log.info("Retrieved playlist: {}", playlist); } catch (IOException | SpotifyWebApiException | ParseException e) { - throw new SpotifyWebApiException("getPlaylistById error: " + e.getMessage()); + throw new SpotifyWebApiException("Error retrieving playlist: " + e.getMessage(), e); } return playlist; } + /** + * Creates a new playlist for the authenticated user. + * + * @param createPlayListDto Data Transfer Object (DTO) containing playlist details. + * @return Playlist object + * @throws SpotifyWebApiException In case of errors while creating the playlist. + */ public Playlist createPlayList(CreatePlayListDto createPlayListDto) throws SpotifyWebApiException { Playlist playlist = null; try { - // TODO : move below to controller / config - this.spotifyApi = authService.authClientWithAuthCode( + // Initialize SpotifyApi with the provided auth code + SpotifyApi spotifyApi = authService.authClientWithAuthCode( authService.getSpotifyClient(), createPlayListDto.getAuthCode() ); - /** NOTE !!! use the same client (spotifyApi) - * for getting current user id and create playlist - */ - // get current user profile - String userId = profileService.getCurrentUserId(this.spotifyApi); //"62kytpy7jswykfjtnjn9zv3ou"; + // Fetch current user ID + String userId = profileService.getCurrentUserId(spotifyApi); /** * NOTE !!! via refresh token, @@ -81,54 +89,56 @@ public Playlist createPlayList(CreatePlayListDto createPlayListDto) throws Spoti * Code ref : * - https://github.com/spotify-web-api-java/spotify-web-api-java/blob/cfd0dae1262bd7f95f90c37b28d27b9c944d471a/examples/authorization/authorization_code/AuthorizationCodeRefreshExample.java#L22 */ - String refreshToken = this.spotifyApi.getRefreshToken(); + // Refresh token to handle multiple requests + String refreshToken = spotifyApi.getRefreshToken(); spotifyApi.setRefreshToken(refreshToken); - final CreatePlaylistRequest createPlaylistRequest = spotifyApi + // Build and execute request to create playlist + CreatePlaylistRequest createPlaylistRequest = spotifyApi .createPlaylist(userId, createPlayListDto.getName()) .build(); - // create new playList playlist = createPlaylistRequest.execute(); - log.info("playlist is created ! " + playlist + " name = " + playlist.getName()); + log.info("Playlist created: {} (Name: {})", playlist.getId(), playlist.getName()); } catch (IOException | SpotifyWebApiException | ParseException e) { - throw new SpotifyWebApiException("createPlayList error: " + e.getMessage()); + throw new SpotifyWebApiException("Error creating playlist: " + e.getMessage(), e); } return playlist; } + /** + * Adds a song to a playlist. + * + * @param addSongToPlayListDto Data Transfer Object (DTO) containing song and playlist details. + * @return SnapshotResult object showing the status of the update. + */ public SnapshotResult addSongToPlayList(AddSongToPlayListDto addSongToPlayListDto) { SnapshotResult snapshotResult = null; - log.info("(addSongToPlayList) addSongToPlayListDto = " + addSongToPlayListDto); - try { + log.info("(addSongToPlayList) DTO: {}", addSongToPlayListDto); - log.info(">>> (addSongToPlayList) authService.getAuthCode() = " + authService.getAuthCode()); - - this.spotifyApi = authService.authClientWithAuthCode( + try { + // Authenticate and initialize SpotifyApi with the provided auth code + SpotifyApi spotifyApi = authService.authClientWithAuthCode( authService.getSpotifyClient(), - authService.getAuthCode()); - - log.info(">>> before refreshSpotifyClient"); - log.info(">>> auth code = " + addSongToPlayListDto.getAuthCode()); + authService.getAuthCode() + ); - String refreshToken = this.spotifyApi.getRefreshToken(); + // Refresh token to handle multiple requests + String refreshToken = spotifyApi.getRefreshToken(); spotifyApi.setRefreshToken(refreshToken); - log.info(">>> end refreshSpotifyClient"); - - final AddItemsToPlaylistRequest addItemsToPlaylistRequest = this.spotifyApi + // Build and execute request to add songs to the playlist + AddItemsToPlaylistRequest addItemsToPlaylistRequest = spotifyApi .addItemsToPlaylist(addSongToPlayListDto.getPlaylistId(), addSongToPlayListDto.getSongUris().split(",")) .build(); snapshotResult = addItemsToPlaylistRequest.execute(); - - log.info("Snapshot ID: " + snapshotResult.getSnapshotId()); - log.info("addSongToPlayList OK"); - }catch (Exception e){ - log.error("addSongToPlayList Error: " + e.getMessage()); + log.info("Snapshot ID: {}", snapshotResult.getSnapshotId()); + } catch (Exception e) { + log.error("Error adding songs to playlist: {}", e.getMessage(), e); } return snapshotResult; } -} +} \ No newline at end of file