-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from RomanMager/issue-11
Add endpoints for moving videos and playlist title update
- Loading branch information
Showing
15 changed files
with
601 additions
and
170 deletions.
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 |
---|---|---|
@@ -1,40 +1,76 @@ | ||
package com.ypm.controller; | ||
|
||
import com.google.api.services.youtube.model.Playlist; | ||
import com.google.api.services.youtube.model.VideoSnippet; | ||
import com.ypm.service.YouTubeService; | ||
import com.google.api.services.youtube.model.PlaylistItem; | ||
import com.google.api.services.youtube.model.PlaylistSnippet; | ||
import com.ypm.service.PlayListService; | ||
import com.ypm.service.VideoService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient; | ||
import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("/playlist") | ||
@RequestMapping("/playlists") | ||
@RequiredArgsConstructor | ||
public class PlayListController { | ||
private final YouTubeService youTubeService; | ||
|
||
@GetMapping("/list") | ||
private final PlayListService playListService; | ||
private final VideoService videosService; | ||
|
||
@GetMapping | ||
public ResponseEntity<List<Playlist>> getPlayLists( | ||
@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient authentication) throws IOException { | ||
|
||
String accessToken = authentication.getAccessToken().getTokenValue(); | ||
return ResponseEntity.ok(youTubeService.getMyPlayLists(accessToken)); | ||
String accessToken = authentication | ||
.getAccessToken() | ||
.getTokenValue(); | ||
|
||
return ResponseEntity.ok(playListService.getPlayLists(accessToken)); | ||
} | ||
|
||
@GetMapping("/{playlistId}/videos") | ||
public ResponseEntity<List<VideoSnippet>> getVideos( | ||
@GetMapping("/{playlistId}") | ||
public ResponseEntity<List<PlaylistItem>> getPlayListVideos( | ||
@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient authentication, | ||
@PathVariable String playlistId) throws IOException { | ||
|
||
String accessToken = authentication.getAccessToken().getTokenValue(); | ||
return ResponseEntity.ok(youTubeService.getPlayListVideos(accessToken, playlistId)); | ||
String accessToken = authentication | ||
.getAccessToken() | ||
.getTokenValue(); | ||
|
||
return ResponseEntity.ok(videosService.getPlayListVideos(accessToken, playlistId)); | ||
} | ||
|
||
@PutMapping("/{playlistId}") | ||
public ResponseEntity<Playlist> updatePlayListTitle( | ||
@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient authentication, | ||
@PathVariable String playlistId, | ||
@RequestBody PlaylistSnippet dataWithUpdatedTitle) throws IOException { | ||
|
||
String accessToken = authentication | ||
.getAccessToken() | ||
.getTokenValue(); | ||
|
||
String newTitle = dataWithUpdatedTitle.getTitle(); | ||
return ResponseEntity.ok(playListService.updatePlayListTitle(accessToken, playlistId, newTitle)); | ||
} | ||
|
||
@PutMapping("/{playlistId}/{targetPlaylistId}") | ||
public ResponseEntity<List<PlaylistItem>> moveVideos( | ||
@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient authentication, | ||
@PathVariable String playlistId, | ||
@PathVariable String targetPlaylistId, | ||
@RequestBody List<String> videosIds) throws IOException { | ||
|
||
String accessToken = authentication | ||
.getAccessToken() | ||
.getTokenValue(); | ||
|
||
return ResponseEntity.ok(videosService.moveVideos(accessToken, playlistId, | ||
targetPlaylistId, videosIds)); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.ypm.controller; | ||
|
||
import com.ypm.service.VideoService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient; | ||
import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.io.IOException; | ||
|
||
@RestController | ||
@RequestMapping("/videos") | ||
@RequiredArgsConstructor | ||
public class VideoController { | ||
|
||
private final VideoService videosService; | ||
|
||
@DeleteMapping("/{videoId}") | ||
public ResponseEntity<?> deleteVideos( | ||
@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient authentication, | ||
@PathVariable String videoId) throws IOException { | ||
|
||
String accessToken = authentication | ||
.getAccessToken() | ||
.getTokenValue(); | ||
|
||
videosService.deleteVideo(accessToken, videoId); | ||
|
||
return ResponseEntity.noContent().build(); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.ypm.service; | ||
|
||
import com.google.api.services.youtube.YouTube; | ||
import com.google.api.services.youtube.model.Playlist; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class PlayListService { | ||
|
||
private final YouTube youTubeClient; | ||
|
||
public List<Playlist> getPlayLists(String accessToken) throws IOException { | ||
return youTubeClient | ||
.playlists() | ||
.list(List.of("snippet")) | ||
.setAccessToken(accessToken) | ||
.setMine(true) | ||
.execute() | ||
.getItems(); | ||
} | ||
|
||
public Playlist updatePlayListTitle(String accessToken, String playListId, | ||
String newTitle) throws IOException { | ||
|
||
Playlist playlistToEdit = getPlayListById(accessToken, playListId); | ||
playlistToEdit.getSnippet().setTitle(newTitle); | ||
|
||
return youTubeClient | ||
.playlists() | ||
.update(List.of("snippet"), playlistToEdit) | ||
.setAccessToken(accessToken) | ||
.execute(); | ||
} | ||
|
||
public Playlist getPlayListById(String accessToken, String playListId) throws IOException { | ||
return youTubeClient | ||
.playlists() | ||
.list(List.of("snippet")) | ||
.setId(List.of(playListId)) | ||
.setAccessToken(accessToken) | ||
.execute() | ||
.getItems() | ||
.stream() | ||
.findFirst() | ||
.orElseThrow(); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package com.ypm.service; | ||
|
||
import com.google.api.services.youtube.YouTube; | ||
import com.google.api.services.youtube.model.PlaylistItem; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class VideoService { | ||
|
||
private final YouTube youTubeClient; | ||
|
||
public List<PlaylistItem> moveVideos(String accessToken, | ||
String playListId, | ||
String targetPlayListId, | ||
List<String> videosIds) throws IOException { | ||
|
||
List<PlaylistItem> videosToMove = getVideos(accessToken, videosIds); | ||
List<PlaylistItem> movedVideos = new ArrayList<>(videosIds.size()); | ||
|
||
for (PlaylistItem video : videosToMove) { | ||
if (isVideoInPlayList(playListId, video)) { | ||
video.getSnippet().setPlaylistId(targetPlayListId); | ||
movedVideos.add(insertVideo(accessToken, video)); | ||
deleteVideo(accessToken, video.getId()); | ||
} | ||
} | ||
|
||
return movedVideos; | ||
} | ||
|
||
public List<PlaylistItem> getVideos(String accessToken, | ||
List<String> videosIds) throws IOException { | ||
|
||
return youTubeClient | ||
.playlistItems() | ||
.list(List.of("snippet")) | ||
.setId(videosIds) | ||
.setAccessToken(accessToken) | ||
.execute() | ||
.getItems(); | ||
} | ||
|
||
public PlaylistItem insertVideo(String accessToken, PlaylistItem playlistItem) throws IOException { | ||
return youTubeClient | ||
.playlistItems() | ||
.insert(List.of("snippet"), playlistItem) | ||
.setAccessToken(accessToken) | ||
.execute(); | ||
} | ||
|
||
public void deleteVideo(String accessToken, String id) throws IOException { | ||
youTubeClient | ||
.playlistItems() | ||
.delete(id) | ||
.setAccessToken(accessToken) | ||
.execute(); | ||
} | ||
|
||
public List<PlaylistItem> getPlayListVideos(String accessToken, | ||
String playListId) throws IOException { | ||
|
||
return youTubeClient | ||
.playlistItems() | ||
.list(List.of("snippet")) | ||
.setPlaylistId(playListId) | ||
.setAccessToken(accessToken) | ||
.execute() | ||
.getItems(); | ||
} | ||
|
||
private boolean isVideoInPlayList(String playListId, PlaylistItem video) { | ||
return video | ||
.getSnippet() | ||
.getPlaylistId() | ||
.equals(playListId); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.