Add route to remove from playlist.

This commit is contained in:
Kavin 2022-04-06 14:21:46 +01:00
parent 00e32dc992
commit 3d6aefdd0e
No known key found for this signature in database
GPG key ID: 49451E4482CC5BCD
4 changed files with 53 additions and 2 deletions

View file

@ -300,6 +300,15 @@ 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/remove", AsyncServlet.ofBlocking(executor, request -> {
try {
var json = Constants.mapper.readTree(request.loadBody().getResult().asArray());
var playlistId = json.get("playlistId").asText();
var index = json.get("index").asInt();
return getJsonResponse(ResponseHelper.removeFromPlaylistResponse(request.getHeader(AUTHORIZATION), playlistId, index), "private");
} catch (Exception e) {
return getErrorResponse(e, request.getPath());
}
})).map(GET, "/registered/badge", AsyncServlet.ofBlocking(executor, request -> { })).map(GET, "/registered/badge", AsyncServlet.ofBlocking(executor, request -> {
try { try {
return HttpResponse.ofCode(302).withHeader(LOCATION, ResponseHelper.registeredBadgeRedirect()) return HttpResponse.ofCode(302).withHeader(LOCATION, ResponseHelper.registeredBadgeRedirect())

View file

@ -296,7 +296,8 @@ public class ResponseHelper {
var cb = s.getCriteriaBuilder(); var cb = s.getCriteriaBuilder();
var cq = cb.createQuery(me.kavin.piped.utils.obj.db.Playlist.class); var cq = cb.createQuery(me.kavin.piped.utils.obj.db.Playlist.class);
var root = cq.from(me.kavin.piped.utils.obj.db.Playlist.class); var root = cq.from(me.kavin.piped.utils.obj.db.Playlist.class);
root.fetch("videos", JoinType.LEFT); root.fetch("videos")
.fetch("channel", JoinType.LEFT);
root.fetch("owner", JoinType.LEFT); root.fetch("owner", JoinType.LEFT);
cq.select(root); cq.select(root);
cq.where(cb.equal(root.get("playlist_id"), UUID.fromString(playlistId))); cq.where(cb.equal(root.get("playlist_id"), UUID.fromString(playlistId)));
@ -1072,6 +1073,44 @@ public class ResponseHelper {
} }
} }
public static byte[] removeFromPlaylistResponse(String session, String playlistId, int index) throws IOException {
if (StringUtils.isBlank(playlistId))
return Constants.mapper.writeValueAsBytes(new InvalidRequestResponse());
try (Session s = DatabaseSessionFactory.createSession()) {
var cb = s.getCriteriaBuilder();
var query = cb.createQuery(me.kavin.piped.utils.obj.db.Playlist.class);
var root = query.from(me.kavin.piped.utils.obj.db.Playlist.class);
root.fetch("videos", JoinType.LEFT);
root.fetch("owner", JoinType.LEFT);
query.where(cb.equal(root.get("playlist_id"), UUID.fromString(playlistId)));
var playlist = s.createQuery(query).uniqueResult();
if (playlist == null)
return Constants.mapper.writeValueAsBytes(Constants.mapper.createObjectNode()
.put("error", "Playlist not found"));
if (playlist.getOwner().getId() != DatabaseHelper.getUserFromSession(session).getId())
return Constants.mapper.writeValueAsBytes(Constants.mapper.createObjectNode()
.put("error", "You are not the owner this playlist"));
if (index < 0 || index >= playlist.getVideos().size())
return Constants.mapper.writeValueAsBytes(Constants.mapper.createObjectNode()
.put("error", "Video Index out of bounds"));
playlist.getVideos().remove(index);
s.update(playlist);
if (!s.getTransaction().isActive())
s.getTransaction().begin();
s.getTransaction().commit();
return Constants.mapper.writeValueAsBytes(new AcceptedResponse());
}
}
public static String registeredBadgeRedirect() { public static String registeredBadgeRedirect() {
try (Session s = DatabaseSessionFactory.createSession()) { try (Session s = DatabaseSessionFactory.createSession()) {
long registered = (Long) s.createQuery("select count(*) from User").uniqueResult(); long registered = (Long) s.createQuery("select count(*) from User").uniqueResult();

View file

@ -43,8 +43,8 @@ public class Playlist {
private User owner; private User owner;
@ElementCollection(fetch = FetchType.LAZY) @ElementCollection(fetch = FetchType.LAZY)
@OneToMany(targetEntity = PlaylistVideo.class)
@Column(name = "videos") @Column(name = "videos")
@OrderColumn(name = "videos_order")
private List<PlaylistVideo> videos; private List<PlaylistVideo> videos;
public long getId() { public long getId() {

View file

@ -101,3 +101,6 @@ curl ${CURLOPTS[@]} $HOST/playlists/$PLAYLIST_ID || exit 1
# Add to Playlist Test # Add to Playlist Test
curl ${CURLOPTS[@]} $HOST/user/playlists/add -X POST -H "Content-Type: application/json" -H "Authorization: $AUTH_TOKEN" -d $(jq -n --compact-output --arg videoId "BtN-goy9VOY" --arg playlistId $PLAYLIST_ID '{"videoId": $videoId, "playlistId": $playlistId}') || exit 1 curl ${CURLOPTS[@]} $HOST/user/playlists/add -X POST -H "Content-Type: application/json" -H "Authorization: $AUTH_TOKEN" -d $(jq -n --compact-output --arg videoId "BtN-goy9VOY" --arg playlistId $PLAYLIST_ID '{"videoId": $videoId, "playlistId": $playlistId}') || exit 1
# Remove from Playlist Test
curl ${CURLOPTS[@]} $HOST/user/playlists/remove -X POST -H "Content-Type: application/json" -H "Authorization: $AUTH_TOKEN" -d $(jq -n --compact-output --arg index "0" --arg playlistId $PLAYLIST_ID '{"index": $index, "playlistId": $playlistId}') || exit 1