Skip to content
Snippets Groups Projects
Verified Commit 855acaee authored by Ernesto Rico Schmidt's avatar Ernesto Rico Schmidt
Browse files

feat: remove playlists routes

parent 5753b065
No related branches found
No related tags found
No related merge requests found
......@@ -88,15 +88,6 @@ func InstallHTTPHandler(r *gin.RouterGroup, st *store.Store, im *importer.Import
imports.GET("", api.ListImportsOfShow)
}
playlists := r.Group("playlists")
{
playlists.GET("", api.ListPlaylists)
playlists.POST("", api.CreatePlaylistForShow)
playlists.GET(":playlist-id", api.ReadPlaylist)
playlists.PUT(":playlist-id", api.UpdatePlaylistOfShow)
playlists.DELETE(":playlist-id", api.DeletePlaylistOfShow)
}
files := r.Group("files")
{
files.GET("", api.ListFilesOfShow)
......
//
// tank, Import and Playlist Daemon for Aura project
// Copyright (C) 2017-2020 Christian Pointner <equinox@helsinki.at>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
package v1
import (
"encoding/json"
"github.com/gin-gonic/gin"
"gitlab.servus.at/autoradio/tank/store"
"net/http"
)
// CreatePlaylistForShow creates a new playlist.
//
// @Summary Create playlist
// @Description Creates a new playlist for the show.
// @Accept json
// @Produce json
// @Param playlist body store.Playlist true "Playlist data"
// @Success 201 {object} store.Playlist
// @Failure 400 {object} ErrorResponse
// @Failure 403 {object} ErrorResponse
// @Failure 500 {object} ErrorResponse
// @Router /api/v1/playlists [post]
func (api *API) CreatePlaylistForShow(c *gin.Context) {
playlist := &store.Playlist{}
err := json.NewDecoder(c.Request.Body).Decode(playlist)
if err != nil {
c.JSON(http.StatusBadRequest, ErrorResponse{Error: "error decoding playlist: " + err.Error()})
return
}
if authorized, _ := authorizeRequestForShow(c, playlist.ShowID); !authorized {
return
}
if playlist, err = api.store.CreatePlaylist(playlist.ShowID, *playlist); err != nil {
sendError(c, err)
return
}
c.JSON(http.StatusCreated, playlist)
}
// UpdatePlaylistOfShow updates a playlist of a show.
//
// @Summary Update playlist
// @Description Updates a playlist of a show.
// @Accept json
// @Produce json
// @Param id path int true "ID of the playlist"
// @Param playlist body store.Playlist true "Playlist data"
// @Success 200 {object} store.Playlist
// @Failure 400 {object} ErrorResponse
// @Failure 403 {object} ErrorResponse
// @Failure 404 {object} ErrorResponse
// @Failure 500 {object} ErrorResponse
// @Router /api/v1/playlists/{id} [put]
func (api *API) UpdatePlaylistOfShow(c *gin.Context) {
playlistID, err := idFromString(c.Param("playlist-id"))
if err != nil {
c.JSON(http.StatusBadRequest, ErrorResponse{Error: "invalid playlist-id: " + err.Error()})
return
}
// with this we are actively subverting the checks performed in api.store.UpdatePlaylist
showID, err := api.store.GetPlaylistShowID(playlistID)
if err != nil {
c.JSON(http.StatusBadRequest, ErrorResponse{Error: "wrong playlist-id: " + err.Error()})
return
}
if authorized, _ := authorizeRequestForShow(c, showID); !authorized {
return
}
playlist := &store.Playlist{}
if err := json.NewDecoder(c.Request.Body).Decode(playlist); err != nil {
c.JSON(http.StatusBadRequest, ErrorResponse{Error: "error decoding playlist: " + err.Error()})
return
}
if playlist, err = api.store.UpdatePlaylist(showID, playlistID, *playlist); err != nil {
sendError(c, err)
return
}
c.JSON(http.StatusOK, playlist)
}
// DeletePlaylistOfShow deletes a playlist of a show.
//
// @Summary Delete playlist
// @Description Deletes a playlist of a show.
// @Param id path int true "ID of the playlist"
// @Success 204
// @Failure 400 {object} ErrorResponse
// @Failure 403 {object} ErrorResponse
// @Failure 404 {object} ErrorResponse
// @Failure 500 {object} ErrorResponse
// @Router /api/v1/playlists/{id} [delete]
func (api *API) DeletePlaylistOfShow(c *gin.Context) {
playlistID, err := idFromString(c.Param("playlist-id"))
if err != nil {
c.JSON(http.StatusBadRequest, ErrorResponse{Error: "invalid playlist-id: " + err.Error()})
return
}
// with this we are actively subverting the checks performed in api.store.DeletePlaylist
showID, err := api.store.GetPlaylistShowID(playlistID)
if err != nil {
c.JSON(http.StatusBadRequest, ErrorResponse{Error: "wrong playlist-id: " + err.Error()})
return
}
if authorized, _ := authorizeRequestForShow(c, showID); !authorized {
return
}
if err = api.store.DeletePlaylist(showID, playlistID); err != nil {
sendError(c, err)
return
}
c.JSON(http.StatusNoContent, nil)
}
// global
// ListPlaylists lists playlists.
//
// @Summary List playlists
// @Description Lists all playlists, the ones of a show or the ones that include a file
// @Produce json
// @Param showId query int false "ID of the show"
// @Param fileId query int false "ID of the file"
// @Param limit query int false "Limit number of results"
// @Param offset query int false "Start listing from offset"
// @Success 200 {object} PlaylistsListing
// @Failure 400 {object} ErrorResponse
// @Failure 403 {object} ErrorResponse
// @Failure 500 {object} ErrorResponse
// @Router /api/v1/playlists [get]
func (api *API) ListPlaylists(c *gin.Context) {
if authorized, _ := authorizeRequest(c, api); !authorized {
return
}
offset, limit, ok := getPaginationParameter(c)
if !ok {
return
}
showID, _ := idFromString(c.Query("showId")) // we ignore error because showId is an optional query parameter
if fileId := c.Query("fileId"); fileId != "" {
fileID, err := idFromString(fileId)
if err != nil {
c.JSON(http.StatusBadRequest, ErrorResponse{Error: "invalid file-id: " + err.Error()})
return
}
// filter the playlists that include the file
if fileID != 0 {
result := FileUsageListing{}
if result.Usage.Playlists, err = api.store.GetFileUsage(showID, fileID); err != nil {
sendError(c, err)
return
}
c.JSON(http.StatusOK, result)
}
}
if showID != 0 {
playlists, err := api.store.ListPlaylists(showID, offset, limit)
if err != nil {
sendError(c, err)
return
}
c.JSON(http.StatusOK, PlaylistsListing{playlists})
} else {
playlists, err := api.store.ListPlaylistsAllShows(offset, limit)
if err != nil {
sendError(c, err)
return
}
c.JSON(http.StatusOK, PlaylistsListing{playlists})
}
}
// ReadPlaylist retrieves a playlist.
//
// @Summary Retrieve playlist
// @Description Retrieves a playlist.
// @Produce json
// @Param id path int true "ID of the playlist"
// @Success 200 {object} store.Playlist
// @Failure 400 {object} ErrorResponse
// @Failure 403 {object} ErrorResponse
// @Failure 404 {object} ErrorResponse
// @Failure 500 {object} ErrorResponse
// @Router /api/v1/playlists/{id} [get]
func (api *API) ReadPlaylist(c *gin.Context) {
if authorized, _ := authorizeRequest(c, api); !authorized {
return
}
id, err := idFromString(c.Param("playlist-id"))
if err != nil {
c.JSON(http.StatusBadRequest, ErrorResponse{Error: "invalid playlist-id: " + err.Error()})
return
}
showID, _ := idFromString(c.Query("showId")) // we ignore error because the showId is an optional query parameter
if showID != 0 {
playlist, err := api.store.GetPlaylist(showID, id)
if err != nil {
sendError(c, err)
return
}
c.JSON(http.StatusOK, playlist)
} else {
playlist, err := api.store.GetPlaylistAllShows(id)
if err != nil {
sendError(c, err)
return
}
c.JSON(http.StatusOK, playlist)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment