Skip to content

Commit

Permalink
Add endpoint for playlists merge
Browse files Browse the repository at this point in the history
  • Loading branch information
daverbk committed Apr 10, 2024
1 parent a4070b8 commit d65ab93
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
.authorizeHttpRequests(auth -> auth.anyRequest().authenticated())
.oauth2Login(config -> {
config.defaultSuccessUrl("/auth/success", true);
config.failureHandler(new SimpleUrlAuthenticationFailureHandler("/auth/error"));
config.failureHandler(
new SimpleUrlAuthenticationFailureHandler("/auth/error"));
})
.build();
}
Expand Down
49 changes: 38 additions & 11 deletions src/main/java/com/ypm/controller/PlayListController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.api.services.youtube.model.Playlist;
import com.google.api.services.youtube.model.PlaylistItem;
import com.google.api.services.youtube.model.PlaylistSnippet;
import com.ypm.dto.request.MergePlayListsRequest;
import com.ypm.service.PlayListService;
import com.ypm.service.VideoService;
import lombok.RequiredArgsConstructor;
Expand All @@ -24,9 +25,9 @@ public class PlayListController {

@GetMapping
public ResponseEntity<List<Playlist>> getPlayLists(
@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient authentication) throws IOException {
@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient authClient) throws IOException {

String accessToken = authentication
String accessToken = authClient
.getAccessToken()
.getTokenValue();

Expand All @@ -35,38 +36,64 @@ public ResponseEntity<List<Playlist>> getPlayLists(

@GetMapping("/{playlistId}")
public ResponseEntity<List<PlaylistItem>> getPlayListVideos(
@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient authentication,
@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient authClient,
@PathVariable String playlistId) throws IOException {

String accessToken = authentication
String accessToken = authClient
.getAccessToken()
.getTokenValue();

return ResponseEntity.ok(videosService.getPlayListVideos(accessToken, playlistId));
}

@PostMapping
public ResponseEntity<Playlist> createPlayList(
@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient authClient,
@RequestBody PlaylistSnippet playlistSnippet) throws IOException {

String accessToken = authClient
.getAccessToken()
.getTokenValue();

return ResponseEntity.ok(playListService.createPlayList(accessToken, playlistSnippet));
}

@PutMapping()
public ResponseEntity<Playlist> mergePlayLists(
@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient authClient,
@RequestBody MergePlayListsRequest request) throws IOException {

String accessToken = authClient
.getAccessToken()
.getTokenValue();

return ResponseEntity.ok(playListService.mergePlayLists(accessToken,
request.mergedPlayListTitle(), request.playListsIds(), request.deleteAfterMerge()));
}

@PutMapping("/{playlistId}")
public ResponseEntity<Playlist> updatePlayListTitle(
@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient authentication,
@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient authClient,
@PathVariable String playlistId,
@RequestBody PlaylistSnippet dataWithUpdatedTitle) throws IOException {

String accessToken = authentication
String accessToken = authClient
.getAccessToken()
.getTokenValue();

String newTitle = dataWithUpdatedTitle.getTitle();
return ResponseEntity.ok(playListService.updatePlayListTitle(accessToken, playlistId, newTitle));
return ResponseEntity.ok(playListService.updatePlayListTitle(accessToken, playlistId,
newTitle));
}

@PutMapping("/{playlistId}/{targetPlaylistId}")
public ResponseEntity<List<PlaylistItem>> moveVideos(
@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient authentication,
@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient authClient,
@PathVariable String playlistId,
@PathVariable String targetPlaylistId,
@RequestBody List<String> videosIds) throws IOException {

String accessToken = authentication
String accessToken = authClient
.getAccessToken()
.getTokenValue();

Expand All @@ -76,10 +103,10 @@ public ResponseEntity<List<PlaylistItem>> moveVideos(

@DeleteMapping("/{playlistId}")
public ResponseEntity<Void> deletePlayList(
@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient authentication,
@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient authClient,
@PathVariable String playlistId) throws IOException {

String accessToken = authentication
String accessToken = authClient
.getAccessToken()
.getTokenValue();

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/ypm/controller/VideoController.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ public class VideoController {

@DeleteMapping("/{videoId}")
public ResponseEntity<Void> deleteVideos(
@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient authentication,
@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient authClient,
@PathVariable String videoId) throws IOException {

String accessToken = authentication
String accessToken = authClient
.getAccessToken()
.getTokenValue();

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/ypm/dto/request/MergePlayListsRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.ypm.dto.request;

import java.util.List;

public record MergePlayListsRequest(String mergedPlayListTitle,
List<String> playListsIds,
boolean deleteAfterMerge) {
}
38 changes: 38 additions & 0 deletions src/main/java/com/ypm/service/PlayListService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.google.api.services.youtube.YouTube;
import com.google.api.services.youtube.model.Playlist;
import com.google.api.services.youtube.model.PlaylistItem;
import com.google.api.services.youtube.model.PlaylistSnippet;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

Expand All @@ -13,6 +15,7 @@
public class PlayListService {

private final YouTube youTubeClient;
private final VideoService videoService;

public List<Playlist> getPlayLists(String accessToken) throws IOException {
return youTubeClient
Expand All @@ -24,6 +27,41 @@ public List<Playlist> getPlayLists(String accessToken) throws IOException {
.getItems();
}

public Playlist mergePlayLists(String accessToken, String mergedPlayListName,
List<String> playListsIds,
boolean deleteAfterMerge) throws IOException {

Playlist mergedPlayList = createPlayList(accessToken, mergedPlayListName);

for (String playListId : playListsIds) {
List<PlaylistItem> videos = videoService.getPlayListVideos(accessToken, playListId);
List<String> videosIds = videos.stream().map(PlaylistItem::getId).toList();
videoService.moveVideos(accessToken, playListId, mergedPlayList.getId(), videosIds);
if (deleteAfterMerge) deletePlayList(accessToken, playListId);
}

return mergedPlayList;
}

public Playlist createPlayList(String accessToken,
String title) throws IOException {
PlaylistSnippet playList = new PlaylistSnippet();
playList.setTitle(title);
return createPlayList(accessToken, playList);
}

public Playlist createPlayList(String accessToken,
PlaylistSnippet playlistSnippet) throws IOException {
Playlist playlist = new Playlist();
playlist.setSnippet(playlistSnippet);

return youTubeClient
.playlists()
.insert(List.of("snippet"), playlist)
.setAccessToken(accessToken)
.execute();
}

public Playlist updatePlayListTitle(String accessToken, String playListId,
String newTitle) throws IOException {

Expand Down

0 comments on commit d65ab93

Please sign in to comment.