route for renaming playlists

This commit is contained in:
Bnyro 2022-08-27 19:09:21 +02:00
parent 1261adeb31
commit 178dbefb52
2 changed files with 41 additions and 0 deletions

View file

@ -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());

View file

@ -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))