Skip to content
This repository
Browse code

added API endpoint to show playlists for a specific user

  • Loading branch information...
commit 32e71622d147f70e293c075ee7c21cfcc480dc85 1 parent ab627e6
rodnaph authored
38 TODO
... ... @@ -1,3 +1,4 @@
  1 +
1 2 1 - BUGS
2 3 --------
3 4
@@ -11,7 +12,6 @@
11 12 2 - FEATURES
12 13 ------------
13 14
14   -* complete streamfinder impl
15 15 * user comments
16 16 * browse A-Z by artist/album/genre/etc...
17 17 * windows installer
@@ -38,44 +38,8 @@
38 38 * rescan command line argument (prob mysql only...)
39 39 * allow uploading zip files with multiple tracks
40 40 - HTML5 player for iPad support
41   -- API
42 41
43 42 3 - MANUAL
44 43 ----------
45 44
46 45 * improve "artwork" page
47   -* add page about the "API"
48   -
49   -API Ideas
50   ----------
51   -
52   -All endpoints return JSON, and will start off being read-only. The endpoints
53   -that are marked as "paged" return 100 items at a time maximum, but this can
54   -be adjusted using "limit" and "offset" parameters. Requests should be GET.
55   -A "limit" of -1 means no limit. Offset can only be specified with a limit.
56   -
57   -/api - (server info, same as /json/serverinfo) [DONE]
58   -/api/artists - array of all artists ordered a-z (paged) [DONE]
59   -/api/artists/123 - artist info (with albums) [DONE]
60   -/api/artists/123/tracks - array of tracks for artist [DONE]
61   -
62   -/api/albums - array of all albums ordered a-z with artist (paged) [DONE]
63   -/api/albums/123 - album info (with artist) [DONE]
64   -/api/albums/123/tracks - array of tracks for album [DONE]
65   -
66   -/api/tracks - array of all tracks ordered a-z (paged) [DONE]
67   -/api/tracks/123 - track info, artist and album [DONE]
68   -
69   -/api/playlists - array of all playlists, newest first (paged) [DONE]
70   -/api/playlists/123 - array of tracks for playlist [DONE]
71   -/api/playlists/user - array of the current users playlists (paged) [DONE]
72   -/api/playlists/site - array of site playlists [DONE]
73   -
74   -/api/session - returns '1' or '0' depending on if the session is valid [DONE]
75   -
76   -API TODO
77   ---------
78   -
79   -1) Add artist to /api/albums [DONE]
80   -2) API errors need to render as JSON [DONE]
81   -3) Tracks need artist/album info in /api/playlists/123 [DONE]
28 src/com/pugh/sockso/web/action/api/PlaylistsAction.java
@@ -5,6 +5,7 @@
5 5 import com.pugh.sockso.templates.api.TPlaylists;
6 6 import com.pugh.sockso.web.BadRequestException;
7 7 import com.pugh.sockso.web.Request;
  8 +import com.pugh.sockso.web.User;
8 9
9 10 import java.io.IOException;
10 11
@@ -33,6 +34,13 @@ public boolean canHandle( final Request req ) {
33 34 (
34 35 req.getParamCount() == 3
35 36 && ( req.getUrlParam(2).equals("site") || req.getUrlParam(2).equals("user") )
  37 + )
  38 + ||
  39 + (
  40 + req.getParamCount() == 4
  41 + && req.getUrlParam( 1 ).equals( "playlists" )
  42 + && req.getUrlParam( 2 ).equals( "user" )
  43 + && isInteger(req.getUrlParam(3) )
36 44 );
37 45
38 46 }
@@ -61,7 +69,7 @@ public void handleRequest() throws SQLException, IOException, BadRequestExceptio
61 69 else if ( req.getUrlParam(2).equals("user") ) {
62 70 showPlaylists(Playlist.findAllForUser(
63 71 getDatabase(),
64   - getUser(),
  72 + getPlaylistUser(),
65 73 getLimit(),
66 74 getOffset()
67 75 ));
@@ -78,6 +86,24 @@ else if ( req.getUrlParam(2).equals("user") ) {
78 86 }
79 87
80 88 /**
  89 + * Returns the user to get playlists for. This could be the current user
  90 + * or the user specified in the URL
  91 + *
  92 + * @return
  93 + *
  94 + */
  95 +
  96 + protected User getPlaylistUser() {
  97 +
  98 + final Request req = getRequest();
  99 +
  100 + return req.getParamCount() == 4
  101 + ? User.find( getDatabase(), Integer.parseInt(req.getUrlParam(3)) )
  102 + : getUser();
  103 +
  104 + }
  105 +
  106 + /**
81 107 * Shows the playlists as JSON
82 108 *
83 109 * @param playlists
10 test/com/pugh/sockso/web/action/api/PlaylistsActionTest.java
@@ -28,6 +28,8 @@ public void testActionHandlesPlaylistsUrl() {
28 28 assertTrue( action.canHandle(getRequest("/api/playlists?offset=100")) );
29 29 assertTrue( action.canHandle(getRequest("/api/playlists/site")) );
30 30 assertTrue( action.canHandle(getRequest("/api/playlists/user")) );
  31 + assertTrue( action.canHandle(getRequest("/api/playlists/user/123")) );
  32 + assertTrue( action.canHandle(getRequest("/api/playlists/user/123?limit=123")) );
31 33 }
32 34
33 35 public void testActionDoesntHandleNonPlaylistUrls() {
@@ -73,4 +75,12 @@ public void testNoPlaylistsListedWhenUserUrlRequestedAndNoCurrentUser() throws E
73 75 assertNotContains( res.getOutput(), "Bar Bar" );
74 76 }
75 77
  78 + public void testPlaylistsOnlyForSpecifiedUserAreListedWhenAUserIsSpecified() throws Exception {
  79 + action.setRequest( getRequest("/api/playlists/user/1") );
  80 + action.handleRequest();
  81 + assertContains( res.getOutput(), "A Playlist" );
  82 + assertNotContains( res.getOutput(), "Foo Foo" );
  83 + assertNotContains( res.getOutput(), "Bar Bar" );
  84 + }
  85 +
76 86 }

0 comments on commit 32e7162

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