Skip to content
Browse files

added API endpoint to show playlists for a specific user

  • Loading branch information...
1 parent ab627e6 commit 32e71622d147f70e293c075ee7c21cfcc480dc85 @rodnaph committed Jul 23, 2011
View
38 TODO
@@ -1,3 +1,4 @@
+
1 - BUGS
--------
@@ -11,7 +12,6 @@
2 - FEATURES
------------
-* complete streamfinder impl
* user comments
* browse A-Z by artist/album/genre/etc...
* windows installer
@@ -38,44 +38,8 @@
* rescan command line argument (prob mysql only...)
* allow uploading zip files with multiple tracks
- HTML5 player for iPad support
-- API
3 - MANUAL
----------
* improve "artwork" page
-* add page about the "API"
-
-API Ideas
----------
-
-All endpoints return JSON, and will start off being read-only. The endpoints
-that are marked as "paged" return 100 items at a time maximum, but this can
-be adjusted using "limit" and "offset" parameters. Requests should be GET.
-A "limit" of -1 means no limit. Offset can only be specified with a limit.
-
-/api - (server info, same as /json/serverinfo) [DONE]
-/api/artists - array of all artists ordered a-z (paged) [DONE]
-/api/artists/123 - artist info (with albums) [DONE]
-/api/artists/123/tracks - array of tracks for artist [DONE]
-
-/api/albums - array of all albums ordered a-z with artist (paged) [DONE]
-/api/albums/123 - album info (with artist) [DONE]
-/api/albums/123/tracks - array of tracks for album [DONE]
-
-/api/tracks - array of all tracks ordered a-z (paged) [DONE]
-/api/tracks/123 - track info, artist and album [DONE]
-
-/api/playlists - array of all playlists, newest first (paged) [DONE]
-/api/playlists/123 - array of tracks for playlist [DONE]
-/api/playlists/user - array of the current users playlists (paged) [DONE]
-/api/playlists/site - array of site playlists [DONE]
-
-/api/session - returns '1' or '0' depending on if the session is valid [DONE]
-
-API TODO
---------
-
-1) Add artist to /api/albums [DONE]
-2) API errors need to render as JSON [DONE]
-3) Tracks need artist/album info in /api/playlists/123 [DONE]
View
28 src/com/pugh/sockso/web/action/api/PlaylistsAction.java
@@ -5,6 +5,7 @@
import com.pugh.sockso.templates.api.TPlaylists;
import com.pugh.sockso.web.BadRequestException;
import com.pugh.sockso.web.Request;
+import com.pugh.sockso.web.User;
import java.io.IOException;
@@ -33,6 +34,13 @@ public boolean canHandle( final Request req ) {
(
req.getParamCount() == 3
&& ( req.getUrlParam(2).equals("site") || req.getUrlParam(2).equals("user") )
+ )
+ ||
+ (
+ req.getParamCount() == 4
+ && req.getUrlParam( 1 ).equals( "playlists" )
+ && req.getUrlParam( 2 ).equals( "user" )
+ && isInteger(req.getUrlParam(3) )
);
}
@@ -61,7 +69,7 @@ public void handleRequest() throws SQLException, IOException, BadRequestExceptio
else if ( req.getUrlParam(2).equals("user") ) {
showPlaylists(Playlist.findAllForUser(
getDatabase(),
- getUser(),
+ getPlaylistUser(),
getLimit(),
getOffset()
));
@@ -78,6 +86,24 @@ else if ( req.getUrlParam(2).equals("user") ) {
}
/**
+ * Returns the user to get playlists for. This could be the current user
+ * or the user specified in the URL
+ *
+ * @return
+ *
+ */
+
+ protected User getPlaylistUser() {
+
+ final Request req = getRequest();
+
+ return req.getParamCount() == 4
+ ? User.find( getDatabase(), Integer.parseInt(req.getUrlParam(3)) )
+ : getUser();
+
+ }
+
+ /**
* Shows the playlists as JSON
*
* @param playlists
View
10 test/com/pugh/sockso/web/action/api/PlaylistsActionTest.java
@@ -28,6 +28,8 @@ public void testActionHandlesPlaylistsUrl() {
assertTrue( action.canHandle(getRequest("/api/playlists?offset=100")) );
assertTrue( action.canHandle(getRequest("/api/playlists/site")) );
assertTrue( action.canHandle(getRequest("/api/playlists/user")) );
+ assertTrue( action.canHandle(getRequest("/api/playlists/user/123")) );
+ assertTrue( action.canHandle(getRequest("/api/playlists/user/123?limit=123")) );
}
public void testActionDoesntHandleNonPlaylistUrls() {
@@ -73,4 +75,12 @@ public void testNoPlaylistsListedWhenUserUrlRequestedAndNoCurrentUser() throws E
assertNotContains( res.getOutput(), "Bar Bar" );
}
+ public void testPlaylistsOnlyForSpecifiedUserAreListedWhenAUserIsSpecified() throws Exception {
+ action.setRequest( getRequest("/api/playlists/user/1") );
+ action.handleRequest();
+ assertContains( res.getOutput(), "A Playlist" );
+ assertNotContains( res.getOutput(), "Foo Foo" );
+ assertNotContains( res.getOutput(), "Bar Bar" );
+ }
+
}

0 comments on commit 32e7162

Please sign in to comment.
Something went wrong with that request. Please try again.