Skip to content

Commit

Permalink
Add new routes to REST API
Browse files Browse the repository at this point in the history
  • Loading branch information
jypelle committed Jul 12, 2021
1 parent dff40f6 commit f5dc236
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
8 changes: 7 additions & 1 deletion internal/srv/restSrvV1/playlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ import (
func (s *RestServer) readPlaylists(w http.ResponseWriter, r *http.Request) {
s.log.Debugf("Read playlists")

playlists, err := s.store.ReadPlaylists(nil, &restApiV1.PlaylistFilter{})
var playlistFilter restApiV1.PlaylistFilter
err := json.NewDecoder(r.Body).Decode(&playlistFilter)
if err != nil {
s.log.Panicf("Unable to interpret data to read the playlists: %v", err)
}

playlists, err := s.store.ReadPlaylists(nil, &playlistFilter)
if err != nil {
s.log.Panicf("Unable to read playlists: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ var (
AppVersion = restApiV1.Version{
MajorNumber: 0,
MinorNumber: 3,
PatchNumber: 1,
PatchNumber: 2,
}
)
18 changes: 18 additions & 0 deletions restClientV1/playlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ func (c *RestClient) CreatePlaylist(playListMeta *restApiV1.PlaylistMeta) (*rest

}

func (c *RestClient) ReadPlaylist(playlistFilter *restApiV1.PlaylistFilter) ([]restApiV1.Playlist, ClientError) {
var playlistList []restApiV1.Playlist

encodedPlaylistFilter, _ := json.Marshal(playlistFilter)

response, cliErr := c.doGetRequestWithContent("/playlists", JsonContentType, bytes.NewBuffer(encodedPlaylistFilter))
if cliErr != nil {
return nil, cliErr
}
defer response.Body.Close()

if err := json.NewDecoder(response.Body).Decode(&playlistList); err != nil {
return nil, NewClientError(err)
}

return playlistList, nil
}

func (c *RestClient) UpdatePlaylist(playlistId restApiV1.PlaylistId, playlistMeta *restApiV1.PlaylistMeta) (*restApiV1.Playlist, ClientError) {
var playlist *restApiV1.Playlist

Expand Down
3 changes: 3 additions & 0 deletions restClientV1/restClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ func (c *RestClient) doRequest(method, relativeUrl string, contentType string, b
func (c *RestClient) doGetRequest(relativeUrl string) (*http.Response, ClientError) {
return c.doRequest("GET", relativeUrl, "", nil)
}
func (c *RestClient) doGetRequestWithContent(relativeUrl string, contentType string, body io.Reader) (*http.Response, ClientError) {
return c.doRequest("GET", relativeUrl, contentType, body)
}
func (c *RestClient) doDeleteRequest(relativeUrl string) (*http.Response, ClientError) {
return c.doRequest("DELETE", relativeUrl, "", nil)
}
Expand Down
16 changes: 16 additions & 0 deletions restClientV1/song.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@ import (
"io"
)

func (c *RestClient) ReadSong(songId restApiV1.SongId) (*restApiV1.Song, ClientError) {
var song *restApiV1.Song

response, cliErr := c.doGetRequest("/songs/" + string(songId))
if cliErr != nil {
return nil, cliErr
}
defer response.Body.Close()

if err := json.NewDecoder(response.Body).Decode(&song); err != nil {
return nil, NewClientError(err)
}

return song, nil
}

func (c *RestClient) ReadSongContent(songId restApiV1.SongId) (io.ReadCloser, int64, ClientError) {

response, cliErr := c.doGetRequest("/songContents/" + string(songId))
Expand Down

0 comments on commit f5dc236

Please sign in to comment.