Something went wrong on our end
-
Christian Pointner authoredChristian Pointner authored
shows.go 2.90 KiB
//
// tank
//
// Import and Playlist Daemon for autoradio project
//
//
// Copyright (C) 2017-2019 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 (
"net/http"
"sort"
"github.com/gin-gonic/gin"
"gitlab.servus.at/autoradio/tank/store"
)
func (api *API) ListShows(c *gin.Context) {
offset, limit, ok := getPaginationParameter(c)
if !ok {
return
}
storeShows, err := api.store.ListShows()
if err != nil {
c.JSON(http.StatusInternalServerError, ErrorResponse{Error: err.Error()})
return
}
s := getAuthSession(c.Request) // this will panic if there is no session
showsMap := make(map[string]store.Show)
for _, showName := range s.Shows {
showsMap[showName] = store.Show{Name: showName}
}
for _, showName := range s.PublicShows {
showsMap[showName] = store.Show{Name: showName}
}
for _, show := range storeShows {
if _, ok := showsMap[show.Name]; ok || s.AllShows {
showsMap[show.Name] = show
}
}
shows := store.Shows{}
for _, show := range showsMap {
shows = append(shows, show)
}
sort.Slice(shows, func(i, j int) bool { return shows[i].Name < shows[j].Name })
if offset > 0 {
if offset >= len(shows) {
c.JSON(http.StatusOK, ShowsListing{Shows: store.Shows{}})
return
}
shows = shows[offset:]
}
if limit > 0 && limit < len(shows) {
shows = shows[:limit]
}
c.JSON(http.StatusOK, ShowsListing{Shows: shows})
}
func (api *API) CreateShow(c *gin.Context) {
showID := c.Param("show-id")
if authorized, _ := authorizeRequest(c, showID); !authorized {
return
}
srcID := c.Query("clone-from")
var show *store.Show
var err error
if srcID != "" {
if authorized, _ := authorizeRequest(c, srcID); !authorized {
return
}
show, err = api.store.CloneShow(showID, srcID)
} else {
show, err = api.store.CreateShow(showID)
}
if err != nil {
sendError(c, err)
return
}
c.JSON(http.StatusCreated, show)
}
func (api *API) DeleteShow(c *gin.Context) {
showID := c.Param("show-id")
if authorized, s := authorizeRequest(c, showID); !authorized {
return
} else if !s.Privileged {
c.JSON(http.StatusForbidden, ErrorResponse{Error: "only privileged users are allowed to delete shows"})
return
}
if err := api.store.DeleteShow(showID); err != nil {
sendError(c, err)
return
}
c.JSON(http.StatusNoContent, nil)
}