From 178dbefb52dd65f0c6658cff7aa19e4074f10937 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sat, 27 Aug 2022 19:09:21 +0200 Subject: [PATCH] route for renaming playlists --- .../java/me/kavin/piped/ServerLauncher.java | 9 ++++++ .../me/kavin/piped/utils/ResponseHelper.java | 32 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/main/java/me/kavin/piped/ServerLauncher.java b/src/main/java/me/kavin/piped/ServerLauncher.java index 6af8613..c2200cf 100644 --- a/src/main/java/me/kavin/piped/ServerLauncher.java +++ b/src/main/java/me/kavin/piped/ServerLauncher.java @@ -345,6 +345,15 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher { } 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()); + var playlistId = json.get("playlistId").textValue(); + var newName = json.get("newName").textValue(); + return getJsonResponse(ResponseHelper.renamePlaylistResponse(request.getHeader(AUTHORIZATION), playlistId, newName), "private"); + } catch (Exception e) { + return getErrorResponse(e, request.getPath()); + } })).map(POST, "/user/playlists/delete", AsyncServlet.ofBlocking(executor, request -> { try { var json = Constants.mapper.readTree(request.loadBody().getResult().asArray()); diff --git a/src/main/java/me/kavin/piped/utils/ResponseHelper.java b/src/main/java/me/kavin/piped/utils/ResponseHelper.java index 7f76311..5d5e12a 100644 --- a/src/main/java/me/kavin/piped/utils/ResponseHelper.java +++ b/src/main/java/me/kavin/piped/utils/ResponseHelper.java @@ -1260,6 +1260,38 @@ public class ResponseHelper { } } + public static byte[] renamePlaylistResponse(String session, String playlistId, String newName) throws IOException { + + if (StringUtils.isBlank(playlistId)) + return mapper.writeValueAsBytes(new InvalidRequestResponse()); + + User user = DatabaseHelper.getUserFromSession(session); + + if (user == null) + return mapper.writeValueAsBytes(new AuthenticationFailureResponse()); + + try (Session s = DatabaseSessionFactory.createSession()) { + var playlist = DatabaseHelper.getPlaylistFromId(s, playlistId); + + if (playlist == null) + return mapper.writeValueAsBytes(mapper.createObjectNode() + .put("error", "Playlist not found")); + + if (playlist.getOwner().getId() != user.getId()) + return mapper.writeValueAsBytes(mapper.createObjectNode() + .put("error", "You do not own this playlist")); + + playlist.setName(newName); + + var tr = s.beginTransaction(); + s.merge(playlist); + tr.commit(); + + } + + return mapper.writeValueAsBytes(new AcceptedResponse()); + } + public static byte[] deletePlaylistResponse(String session, String playlistId) throws IOException { if (StringUtils.isBlank(playlistId))