Simplify PlaylistHelper#getUserPlaylist

This commit is contained in:
Bnyro 2023-05-29 21:35:56 +02:00
parent 86152a457a
commit 71b13d59c4
4 changed files with 17 additions and 48 deletions

View File

@ -37,8 +37,7 @@ import java.util.concurrent.TimeUnit;
import static io.activej.config.converter.ConfigConverters.ofInetSocketAddress; import static io.activej.config.converter.ConfigConverters.ofInetSocketAddress;
import static io.activej.http.HttpHeaders.*; import static io.activej.http.HttpHeaders.*;
import static io.activej.http.HttpMethod.GET; import static io.activej.http.HttpMethod.*;
import static io.activej.http.HttpMethod.POST;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
import static me.kavin.piped.consts.Constants.mapper; import static me.kavin.piped.consts.Constants.mapper;
@ -402,7 +401,7 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher {
} catch (Exception e) { } catch (Exception e) {
return getErrorResponse(e, request.getPath()); 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 { try {
var json = mapper.readTree(request.loadBody().getResult().asArray()); var json = mapper.readTree(request.loadBody().getResult().asArray());
var playlistId = json.get("playlistId").textValue(); var playlistId = json.get("playlistId").textValue();

View File

@ -23,6 +23,7 @@ import me.kavin.piped.utils.resp.AuthenticationFailureResponse;
import me.kavin.piped.utils.resp.InvalidRequestResponse; import me.kavin.piped.utils.resp.InvalidRequestResponse;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.internal.util.ExceptionHelper;
import org.schabi.newpipe.extractor.Page; import org.schabi.newpipe.extractor.Page;
import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.playlist.PlaylistInfo; import org.schabi.newpipe.extractor.playlist.PlaylistInfo;
@ -148,19 +149,15 @@ public class AuthPlaylistHandlers {
ExceptionHandler.throwErrorResponse(new AuthenticationFailureResponse()); ExceptionHandler.throwErrorResponse(new AuthenticationFailureResponse());
try (Session s = DatabaseSessionFactory.createSession()) { try (Session s = DatabaseSessionFactory.createSession()) {
var playlistResult = PlaylistHelpers.getUserPlaylist(s, user, playlistId); var playlist = PlaylistHelpers.getUserPlaylist(s, user, playlistId);
if (playlistResult.getError() != null) {
return mapper.writeValueAsBytes(mapper.createObjectNode()
.put("error", playlistResult.getError()));
}
var playlist = playlistResult.getPlaylist();
playlist.setName(newName); playlist.setName(newName);
var tr = s.beginTransaction(); var tr = s.beginTransaction();
s.merge(playlist); s.merge(playlist);
tr.commit(); tr.commit();
} catch (IllegalArgumentException e) {
ExceptionHandler.throwErrorResponse(new InvalidRequestResponse(e.getMessage()));
} }
return mapper.writeValueAsBytes(new AcceptedResponse()); return mapper.writeValueAsBytes(new AcceptedResponse());
@ -180,19 +177,15 @@ public class AuthPlaylistHandlers {
ExceptionHandler.throwErrorResponse(new AuthenticationFailureResponse()); ExceptionHandler.throwErrorResponse(new AuthenticationFailureResponse());
try (Session s = DatabaseSessionFactory.createSession()) { try (Session s = DatabaseSessionFactory.createSession()) {
var playlistResult = PlaylistHelpers.getUserPlaylist(s, user, playlistId); var playlist = PlaylistHelpers.getUserPlaylist(s, user, playlistId);
if (playlistResult.getError() != null) {
return mapper.writeValueAsBytes(mapper.createObjectNode()
.put("error", playlistResult.getError()));
}
var playlist = playlistResult.getPlaylist();
playlist.setShortDescription(newDescription); playlist.setShortDescription(newDescription);
var tr = s.beginTransaction(); var tr = s.beginTransaction();
s.merge(playlist); s.merge(playlist);
tr.commit(); tr.commit();
} catch (IllegalArgumentException e) {
ExceptionHandler.throwErrorResponse(new InvalidRequestResponse(e.getMessage()));
} }
return mapper.writeValueAsBytes(new AcceptedResponse()); return mapper.writeValueAsBytes(new AcceptedResponse());
@ -209,17 +202,14 @@ public class AuthPlaylistHandlers {
ExceptionHandler.throwErrorResponse(new AuthenticationFailureResponse()); ExceptionHandler.throwErrorResponse(new AuthenticationFailureResponse());
try (Session s = DatabaseSessionFactory.createSession()) { try (Session s = DatabaseSessionFactory.createSession()) {
var playlistResult = PlaylistHelpers.getUserPlaylist(s, user, playlistId); var playlist = PlaylistHelpers.getUserPlaylist(s, user, playlistId);
if (playlistResult.getError() != null) {
return mapper.writeValueAsBytes(mapper.createObjectNode()
.put("error", playlistResult.getError()));
}
var playlist = playlistResult.getPlaylist();
var tr = s.beginTransaction(); var tr = s.beginTransaction();
s.remove(playlist); s.remove(playlist);
tr.commit(); tr.commit();
} catch (IllegalArgumentException e) {
ExceptionHandler.throwErrorResponse(new InvalidRequestResponse(e.getMessage()));
} }
return mapper.writeValueAsBytes(new AcceptedResponse()); return mapper.writeValueAsBytes(new AcceptedResponse());

View File

@ -1,19 +1,20 @@
package me.kavin.piped.utils; package me.kavin.piped.utils;
import me.kavin.piped.utils.obj.db.Playlist;
import org.hibernate.Session; import org.hibernate.Session;
import me.kavin.piped.utils.obj.db.User; import me.kavin.piped.utils.obj.db.User;
public class PlaylistHelpers { 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); var playlist = DatabaseHelper.getPlaylistFromId(s, playlistId);
if (playlist == null) if (playlist == null)
return new PlaylistResult(null, "Playlist not found"); throw new IllegalArgumentException("Playlist not found");
if (playlist.getOwner().getId() != user.getId()) 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;
} }
} }

View File

@ -1,21 +0,0 @@
package me.kavin.piped.utils;
import me.kavin.piped.utils.obj.db.Playlist;
public final class PlaylistResult {
private final Playlist playlist;
private final String error;
public PlaylistResult(Playlist playlist, String error) {
this.playlist = playlist;
this.error = error;
}
public Playlist getPlaylist() {
return playlist;
}
public String getError() {
return error;
}
}