feat: ability to clear a playlist from its videos (#1952)

This commit is contained in:
frajibe 2023-01-07 11:07:14 +01:00
parent 09d538d355
commit fef6022104
2 changed files with 17 additions and 6 deletions

View file

@ -412,6 +412,14 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher {
} catch (Exception e) {
return getErrorResponse(e, request.getPath());
}
})).map(POST, "/user/playlists/clear", AsyncServlet.ofBlocking(executor, request -> {
try {
var json = Constants.mapper.readTree(request.loadBody().getResult().asArray());
var playlistId = json.get("playlistId").textValue();
return getJsonResponse(AuthPlaylistHandlers.removeFromPlaylistResponse(request.getHeader(AUTHORIZATION), playlistId, null), "private");
} catch (Exception e) {
return getErrorResponse(e, request.getPath());
}
})).map(POST, "/user/playlists/rename", AsyncServlet.ofBlocking(executor, request -> {
try {
var json = Constants.mapper.readTree(request.loadBody().getResult().asArray());

View file

@ -283,8 +283,7 @@ public class AuthPlaylistHandlers {
}
}
public static byte[] removeFromPlaylistResponse(String session, String playlistId, int index) throws IOException {
public static byte[] removeFromPlaylistResponse(String session, String playlistId, Integer index) throws IOException {
if (StringUtils.isBlank(session) || StringUtils.isBlank(playlistId))
ExceptionHandler.throwErrorResponse(new InvalidRequestResponse("session and playlistId are required parameters"));
@ -304,11 +303,15 @@ public class AuthPlaylistHandlers {
return mapper.writeValueAsBytes(mapper.createObjectNode()
.put("error", "You are not the owner this playlist"));
if (index < 0 || index >= playlist.getVideos().size())
return mapper.writeValueAsBytes(mapper.createObjectNode()
.put("error", "Video Index out of bounds"));
if (index != null) {
if (index < 0 || index >= playlist.getVideos().size())
return mapper.writeValueAsBytes(mapper.createObjectNode()
.put("error", "Video Index out of bounds"));
playlist.getVideos().remove(index);
playlist.getVideos().remove((int) index);
} else {
playlist.getVideos().clear();
}
var tr = s.beginTransaction();
s.merge(playlist);