Add route to delete playlist.

This commit is contained in:
Kavin 2022-04-06 16:11:28 +01:00
parent 1135e20c3d
commit f7a5f5051a
No known key found for this signature in database
GPG key ID: 49451E4482CC5BCD
4 changed files with 53 additions and 2 deletions

View file

@ -309,6 +309,14 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher {
} 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());
var playlistId = json.get("playlistId").asText();
return getJsonResponse(ResponseHelper.deletePlaylistResponse(request.getHeader(AUTHORIZATION), playlistId), "private");
} catch (Exception e) {
return getErrorResponse(e, request.getPath());
}
})).map(GET, "/registered/badge", AsyncServlet.ofBlocking(executor, request -> {
try {
return HttpResponse.ofCode(302).withHeader(LOCATION, ResponseHelper.registeredBadgeRedirect())

View file

@ -7,6 +7,7 @@ import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Root;
import java.util.UUID;
public class DatabaseHelper {
@ -60,6 +61,15 @@ public class DatabaseHelper {
return s.createQuery(cr).uniqueResult();
}
public static Playlist getPlaylistFromId(Session s, String id) {
CriteriaBuilder cb = s.getCriteriaBuilder();
CriteriaQuery<Playlist> cr = cb.createQuery(Playlist.class);
Root<Playlist> root = cr.from(Playlist.class);
cr.select(root).where(cb.equal(root.get("playlist_id"), UUID.fromString(id)));
return s.createQuery(cr).uniqueResult();
}
public static PubSub getPubSubFromId(Session s, String id) {
CriteriaBuilder cb = s.getCriteriaBuilder();
CriteriaQuery<PubSub> cr = cb.createQuery(PubSub.class);

View file

@ -296,9 +296,9 @@ public class ResponseHelper {
var cb = s.getCriteriaBuilder();
var cq = cb.createQuery(me.kavin.piped.utils.obj.db.Playlist.class);
var root = cq.from(me.kavin.piped.utils.obj.db.Playlist.class);
root.fetch("videos")
root.fetch("videos", JoinType.LEFT)
.fetch("channel", JoinType.LEFT);
root.fetch("owner", JoinType.LEFT);
root.fetch("owner", JoinType.INNER);
cq.select(root);
cq.where(cb.equal(root.get("playlist_id"), UUID.fromString(playlistId)));
var query = s.createQuery(cq);
@ -1030,6 +1030,36 @@ public class ResponseHelper {
}
}
public static byte[] deletePlaylistResponse(String session, String playlistId) throws IOException {
if (StringUtils.isBlank(playlistId))
return Constants.mapper.writeValueAsBytes(new InvalidRequestResponse());
User user = DatabaseHelper.getUserFromSession(session);
if (user == null)
return Constants.mapper.writeValueAsBytes(new AuthenticationFailureResponse());
try (Session s = DatabaseSessionFactory.createSession()) {
var playlist = DatabaseHelper.getPlaylistFromId(s, playlistId);
if (playlist == null)
return Constants.mapper.writeValueAsBytes(Constants.mapper.createObjectNode()
.put("error", "Playlist not found"));
if (playlist.getOwner().getId() != user.getId())
return Constants.mapper.writeValueAsBytes(Constants.mapper.createObjectNode()
.put("error", "You do not own this playlist"));
s.delete(playlist);
s.getTransaction().begin();
s.getTransaction().commit();
}
return Constants.mapper.writeValueAsBytes(new AcceptedResponse());
}
public static byte[] playlistsResponse(String session) throws IOException {
User user = DatabaseHelper.getUserFromSession(session);

View file

@ -104,3 +104,6 @@ curl ${CURLOPTS[@]} $HOST/user/playlists/add -X POST -H "Content-Type: applicati
# 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
# Delete Playlist Test
curl ${CURLOPTS[@]} $HOST/user/playlists/delete -X POST -H "Content-Type: application/json" -H "Authorization: $AUTH_TOKEN" -d $(jq -n --compact-output --arg playlistId $PLAYLIST_ID '{"playlistId": $playlistId}') || exit 1