Skip to content

Commit

Permalink
refactor playListService, rename method in controller, update todo
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Sep 28, 2024
1 parent 5209782 commit 44706a0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 45 deletions.
10 changes: 9 additions & 1 deletion springSpotifyPlayList/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,16 @@ docker rmi -f $(docker images -q -a)

</details>


## TODO

<details>
<summary>TODO</summary>

1. code refactor
2. integrate with chatGPT
- dep injection : `spotifyApi`
2. integrate with chatGPT
3. CICD, dockerize project
4. UI redesign

</details>
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ public class UserDataController {
@GetMapping("/playlist")
public PlaylistSimplified[] getUserPlayList(){
//String userId = "62kytpy7jswykfjtnjn9zv3ou";
return userDataService.getUserAllPlayList(userId);
return userDataService.getUserAllPlaylists(userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
}

}
}

0 comments on commit 44706a0

Please sign in to comment.