Skip to content

Commit

Permalink
Simplify PlaylistHelper#getUserPlaylist
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed May 29, 2023
1 parent 86152a4 commit 71b13d5
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 48 deletions.
5 changes: 2 additions & 3 deletions src/main/java/me/kavin/piped/server/ServerLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@

import static io.activej.config.converter.ConfigConverters.ofInetSocketAddress;
import static io.activej.http.HttpHeaders.*;
import static io.activej.http.HttpMethod.GET;
import static io.activej.http.HttpMethod.POST;
import static io.activej.http.HttpMethod.*;
import static java.nio.charset.StandardCharsets.UTF_8;
import static me.kavin.piped.consts.Constants.mapper;

Expand Down Expand Up @@ -402,7 +401,7 @@ AsyncServlet mainServlet(Executor executor) {
} catch (Exception e) {
return getErrorResponse(e, request.getPath());
}
})).map(POST, "/user/playlists/description/change", AsyncServlet.ofBlocking(executor, request -> {
})).map(PATCH, "/user/playlists/description", AsyncServlet.ofBlocking(executor, request -> {
try {
var json = mapper.readTree(request.loadBody().getResult().asArray());
var playlistId = json.get("playlistId").textValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import me.kavin.piped.utils.resp.InvalidRequestResponse;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Session;
import org.hibernate.internal.util.ExceptionHelper;
import org.schabi.newpipe.extractor.Page;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.playlist.PlaylistInfo;
Expand Down Expand Up @@ -148,19 +149,15 @@ public static byte[] renamePlaylistResponse(String session, String playlistId, S
ExceptionHandler.throwErrorResponse(new AuthenticationFailureResponse());

try (Session s = DatabaseSessionFactory.createSession()) {
var playlistResult = PlaylistHelpers.getUserPlaylist(s, user, playlistId);
if (playlistResult.getError() != null) {
return mapper.writeValueAsBytes(mapper.createObjectNode()
.put("error", playlistResult.getError()));
}
var playlist = playlistResult.getPlaylist();

var playlist = PlaylistHelpers.getUserPlaylist(s, user, playlistId);
playlist.setName(newName);

var tr = s.beginTransaction();
s.merge(playlist);
tr.commit();

} catch (IllegalArgumentException e) {
ExceptionHandler.throwErrorResponse(new InvalidRequestResponse(e.getMessage()));
}

return mapper.writeValueAsBytes(new AcceptedResponse());
Expand All @@ -180,19 +177,15 @@ public static byte[] editPlaylistDescriptionResponse(String session, String play
ExceptionHandler.throwErrorResponse(new AuthenticationFailureResponse());

try (Session s = DatabaseSessionFactory.createSession()) {
var playlistResult = PlaylistHelpers.getUserPlaylist(s, user, playlistId);
if (playlistResult.getError() != null) {
return mapper.writeValueAsBytes(mapper.createObjectNode()
.put("error", playlistResult.getError()));
}
var playlist = playlistResult.getPlaylist();

var playlist = PlaylistHelpers.getUserPlaylist(s, user, playlistId);
playlist.setShortDescription(newDescription);

var tr = s.beginTransaction();
s.merge(playlist);
tr.commit();

} catch (IllegalArgumentException e) {
ExceptionHandler.throwErrorResponse(new InvalidRequestResponse(e.getMessage()));
}

return mapper.writeValueAsBytes(new AcceptedResponse());
Expand All @@ -209,17 +202,14 @@ public static byte[] deletePlaylistResponse(String session, String playlistId) t
ExceptionHandler.throwErrorResponse(new AuthenticationFailureResponse());

try (Session s = DatabaseSessionFactory.createSession()) {
var playlistResult = PlaylistHelpers.getUserPlaylist(s, user, playlistId);
if (playlistResult.getError() != null) {
return mapper.writeValueAsBytes(mapper.createObjectNode()
.put("error", playlistResult.getError()));
}
var playlist = playlistResult.getPlaylist();
var playlist = PlaylistHelpers.getUserPlaylist(s, user, playlistId);

var tr = s.beginTransaction();
s.remove(playlist);
tr.commit();

} catch (IllegalArgumentException e) {
ExceptionHandler.throwErrorResponse(new InvalidRequestResponse(e.getMessage()));
}

return mapper.writeValueAsBytes(new AcceptedResponse());
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/me/kavin/piped/utils/PlaylistHelpers.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package me.kavin.piped.utils;

import me.kavin.piped.utils.obj.db.Playlist;
import org.hibernate.Session;

import me.kavin.piped.utils.obj.db.User;

public class PlaylistHelpers {
public static PlaylistResult getUserPlaylist(Session s, User user, String playlistId) {
public static Playlist getUserPlaylist(Session s, User user, String playlistId) throws IllegalArgumentException {
var playlist = DatabaseHelper.getPlaylistFromId(s, playlistId);

if (playlist == null)
return new PlaylistResult(null, "Playlist not found");
throw new IllegalArgumentException("Playlist not found");

if (playlist.getOwner().getId() != user.getId())
return new PlaylistResult(null, "You do not own this playlist");
throw new IllegalArgumentException("You do not own this playlist");

return new PlaylistResult(playlist, null);
return playlist;
}
}
21 changes: 0 additions & 21 deletions src/main/java/me/kavin/piped/utils/PlaylistResult.java

This file was deleted.

0 comments on commit 71b13d5

Please sign in to comment.