Newer
Older
//
// tank
//
// Import and Playlist Daemon for autoradio project
//
//
// Copyright (C) 2017-2018 Christian Pointner <equinox@helsinki.at>
//
// This file is part of tank.
//
// tank is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// tank is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with tank. If not, see <http://www.gnu.org/licenses/>.
//
package v1
import (
"github.com/gorilla/mux"
"gitlab.servus.at/autoradio/tank/store"
)
func (api *API) ListPlaylistsOfGroup() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
// TODO: implement pagination
playlists, err := api.store.ListPlaylists(vars["group-id"])
if err != nil {
sendStoreError(w, err)
return
}
sendWebResponse(w, http.StatusOK, PlaylistsListing{playlists})
})
}
func (api *API) CreatePlaylistForGroup() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
playlist := &store.Playlist{}
err := json.NewDecoder(r.Body).Decode(playlist)
if err != nil {
sendWebResponse(w, http.StatusBadRequest, ErrorResponse{Error: "error decoding playlist: " + err.Error()})
return
}
if playlist, err = api.store.CreatePlaylist(vars["group-id"], *playlist); err != nil {
sendStoreError(w, err)
return
}
sendWebResponse(w, http.StatusCreated, playlist)
})
}
func (api *API) ReadPlaylistOfGroup() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id, err := idFromString(vars["playlist-id"])
if err != nil {
sendWebResponse(w, http.StatusBadRequest, ErrorResponse{Error: "invalid playlist-id: " + err.Error()})
return
}
playlist, err := api.store.GetPlaylist(vars["group-id"], id)
if err != nil {
sendStoreError(w, err)
return
}
sendWebResponse(w, http.StatusOK, playlist)
})
}
func (api *API) UpdatePlaylistOfGroup() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id, err := idFromString(vars["playlist-id"])
if err != nil {
sendWebResponse(w, http.StatusBadRequest, ErrorResponse{Error: "invalid playlist-id: " + err.Error()})
return
}
playlist := &store.Playlist{}
if err = json.NewDecoder(r.Body).Decode(playlist); err != nil {
sendWebResponse(w, http.StatusBadRequest, ErrorResponse{Error: "error decoding playlist: " + err.Error()})
return
}
if playlist, err = api.store.UpdatePlaylist(vars["group-id"], id, *playlist); err != nil {
sendStoreError(w, err)
return
}
sendWebResponse(w, http.StatusOK, playlist)
})
}
func (api *API) DeletePlaylistOfGroup() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id, err := idFromString(vars["playlist-id"])
if err != nil {
sendWebResponse(w, http.StatusBadRequest, ErrorResponse{Error: "invalid playlist-id: " + err.Error()})
return
}
if err = api.store.DeletePlaylist(vars["group-id"], id); err != nil {
sendStoreError(w, err)
return
}
sendWebResponse(w, http.StatusNoContent, nil)