Commit ab4e2799 authored by Christian Pointner's avatar Christian Pointner
Browse files

Merge branch 'topic/global-playlist-api'

parents 753fdcd5 5334814f
Pipeline #573 failed with stages
in 7 minutes and 41 seconds
......@@ -132,9 +132,36 @@ func (api *API) DeletePlaylistOfShow(c *gin.Context) {
// global
func (api *API) ListPlaylists(c *gin.Context) {
c.JSON(http.StatusNotImplemented, ErrorResponse{Error: "not implemented"})
if authorized, _ := authorizeRequestAllShows(c); !authorized {
return
}
offset, limit, ok := getPaginationParameter(c)
if !ok {
return
}
playlists, err := api.store.ListPlaylistsAllShows(offset, limit)
if err != nil {
sendError(c, err)
return
}
c.JSON(http.StatusOK, PlaylistsListing{playlists})
}
func (api *API) ReadPlaylist(c *gin.Context) {
c.JSON(http.StatusNotImplemented, ErrorResponse{Error: "not implemented"})
if authorized, _ := authorizeRequestAllShows(c); !authorized {
return
}
id, err := idFromString(c.Param("playlist-id"))
if err != nil {
c.JSON(http.StatusBadRequest, ErrorResponse{Error: "invalid playlist-id: " + err.Error()})
return
}
playlist, err := api.store.GetPlaylistAllShows(id)
if err != nil {
sendError(c, err)
return
}
c.JSON(http.StatusOK, playlist)
}
......@@ -139,6 +139,23 @@ func isRequestReadOnly(c *gin.Context) bool {
return false
}
func authorizeRequestAllShows(c *gin.Context) (bool, *auth.Session) {
s := getAuthSession(c.Request)
if s.Privileged {
return true, s
}
if s.ReadOnly && !isRequestReadOnly(c) {
c.JSON(http.StatusForbidden, ErrorResponse{Error: "this session is read-only"})
return false, s
}
if s.AllShows {
return true, s
}
c.JSON(http.StatusForbidden, ErrorResponse{Error: "you are not allowed to access all shows"})
return false, s
}
func authorizeRequestForShow(c *gin.Context, showID string) (bool, *auth.Session) {
s := getAuthSession(c.Request)
if s.Privileged {
......
......@@ -153,3 +153,18 @@ func (st *Store) DeletePlaylist(show string, id uint64) (err error) {
}
return
}
func (st *Store) ListPlaylistsAllShows(offset, limit int) (playlists Playlists, err error) {
err = st.db.Preload("Entries", func(db *gorm.DB) *gorm.DB {
return db.Order("playlist_entries.line_num asc")
}).Preload("Entries.File").Order("id").Offset(offset).Limit(limit).Find(&playlists).Error
return
}
func (st *Store) GetPlaylistAllShows(id uint64) (playlist *Playlist, err error) {
playlist = &Playlist{}
err = st.db.Preload("Entries", func(db *gorm.DB) *gorm.DB {
return db.Order("playlist_entries.line_num ASC")
}).Preload("Entries.File").First(playlist, id).Error
return
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment