Skip to content

Commit

Permalink
Merge pull request #22 from RomanMager/issue-18
Browse files Browse the repository at this point in the history
Add ability to set Playlist visibility
  • Loading branch information
daverbk authored Apr 17, 2024
2 parents db0480a + b7bb765 commit 45f3338
Show file tree
Hide file tree
Showing 16 changed files with 394 additions and 280 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ trim_trailing_whitespace = true
insert_final_newline = true
max_line_length = 100

[*.java]
ij_java_align_multiline_chained_methods = true
ij_java_align_multiline_extends_list = true

[*.yml]
indent_size = 2

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

public enum Part {
SNIPPET, STATUS;

@Override
public String toString() {
return this.name().toLowerCase();
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/ypm/constant/PrivacyStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.ypm.constant;

public enum PrivacyStatus {
PRIVATE, PUBLIC, UNLISTED;

@Override
public String toString() {
return this.name().toLowerCase();
}
}
89 changes: 37 additions & 52 deletions src/main/java/com/ypm/controller/PlayListController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
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.PlaylistDto;
import com.ypm.dto.request.MergePlayListsRequest;
import com.ypm.service.PlayListService;
import com.ypm.service.TokenService;
import com.ypm.service.VideoService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
Expand All @@ -22,94 +24,77 @@ public class PlayListController {

private final PlayListService playListService;
private final VideoService videosService;
private final TokenService tokenService;

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

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

@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient authClient
) throws IOException {
var accessToken = tokenService.getToken(authClient);
return ResponseEntity.ok(playListService.getPlayLists(accessToken));
}

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

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

return ResponseEntity.ok(videosService.getPlayListVideos(accessToken, playlistId));
@PathVariable String playlistId
) throws IOException {
var accessToken = tokenService.getToken(authClient);
var playlistVideos = videosService.getPlayListVideos(accessToken, playlistId);
return ResponseEntity.ok(playlistVideos);
}

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

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

return ResponseEntity.ok(playListService.createPlayList(accessToken, playlistSnippet));
@RequestBody PlaylistDto playlistDto
) throws IOException {
var accessToken = tokenService.getToken(authClient);
var createdPlayList = playListService.createPlayList(accessToken, playlistDto);
return ResponseEntity.ok(createdPlayList);
}

@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()));
@RequestBody MergePlayListsRequest request
) throws IOException {
var accessToken = tokenService.getToken(authClient);
var mergedPlaylist = playListService.mergePlayLists(accessToken, request.playlistDto(),
request.playListsIds(), request.deleteAfterMerge());
return ResponseEntity.ok(mergedPlaylist);
}

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

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

String newTitle = dataWithUpdatedTitle.getTitle();
return ResponseEntity.ok(playListService.updatePlayListTitle(accessToken, playlistId,
newTitle));
@RequestBody PlaylistSnippet dataWithUpdatedTitle
) throws IOException {
var accessToken = tokenService.getToken(authClient);
var newTitle = dataWithUpdatedTitle.getTitle();
var updatedPlaylist = playListService.updatePlayListTitle(accessToken, playlistId, newTitle);
return ResponseEntity.ok(updatedPlaylist);
}

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

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

return ResponseEntity.ok(videosService.moveVideos(accessToken, playlistId,
targetPlaylistId, videosIds));
@RequestBody List<String> videosIds
) throws IOException {
var accessToken = tokenService.getToken(authClient);
var movedVideos = videosService.moveVideos(accessToken, playlistId, targetPlaylistId, videosIds);
return ResponseEntity.ok(movedVideos);
}

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

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

@PathVariable String playlistId
) throws IOException {
var accessToken = tokenService.getToken(authClient);
playListService.deletePlayList(accessToken, playlistId);
return ResponseEntity.noContent().build();
}
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/com/ypm/controller/VideoController.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.ypm.controller;

import com.ypm.service.TokenService;
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 org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

Expand All @@ -15,18 +19,15 @@
public class VideoController {

private final VideoService videosService;
private final TokenService tokenService;

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

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

@PathVariable String videoId
) throws IOException {
var accessToken = tokenService.getToken(authClient);
videosService.deleteVideo(accessToken, videoId);

return ResponseEntity.noContent().build();
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/ypm/dto/PlaylistDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.ypm.dto;

import java.util.Optional;

public record PlaylistDto(String title,
Optional<String> description,
String status) {
}
5 changes: 4 additions & 1 deletion src/main/java/com/ypm/dto/request/MergePlayListsRequest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.ypm.dto.request;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.ypm.dto.PlaylistDto;

import java.util.List;

public record MergePlayListsRequest(String mergedPlayListTitle,
public record MergePlayListsRequest(@JsonProperty(value = "playlistDetails") PlaylistDto playlistDto,
List<String> playListsIds,
boolean deleteAfterMerge) { }
98 changes: 11 additions & 87 deletions src/main/java/com/ypm/service/PlayListService.java
Original file line number Diff line number Diff line change
@@ -1,101 +1,25 @@
package com.ypm.service;

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 com.ypm.exception.PlayListNotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ypm.dto.PlaylistDto;

import java.io.IOException;
import java.util.List;

@Service
@RequiredArgsConstructor
public class PlayListService {
public interface PlayListService {

private final YouTube youTubeClient;
private final VideoService videoService;
List<Playlist> getPlayLists(String accessToken) throws IOException;

public List<Playlist> getPlayLists(String accessToken) throws IOException {
return youTubeClient
.playlists()
.list(List.of("snippet"))
.setAccessToken(accessToken)
.setMine(true)
.execute()
.getItems();
}
Playlist createPlayList(String accessToken, PlaylistDto playlistDto) throws IOException;

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

Playlist mergedPlayList = createPlayList(accessToken, mergedPlayListName);
void deletePlayList(String accessToken, String playListId) throws IOException;

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);
}
Playlist mergePlayLists(String accessToken, PlaylistDto playlistDto,
List<String> playListsIds,
boolean deleteAfterMerge) throws IOException;

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 {

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(() ->
new PlayListNotFoundException(playListId,
"At some stage we were not able to find one of the request-related playlists."));
}

public void deletePlayList(String accessToken, String playListId) throws IOException {
youTubeClient
.playlists()
.delete(playListId)
.setAccessToken(accessToken)
.execute();
}
Playlist updatePlayListTitle(String accessToken, String playListId,
String newTitle) throws IOException;
}
Loading

0 comments on commit 45f3338

Please sign in to comment.