feat(playlist): changes to relation table
Also introduces an abstraction between playlist and dashboard. This will make it possible to att search, and tag filtering to playlists without any major refactoring
This commit is contained in:
+1
-1
@@ -176,7 +176,7 @@ func Register(r *macaron.Macaron) {
|
||||
r.Group("/playlists", func() {
|
||||
r.Get("/", wrap(SearchPlaylists))
|
||||
r.Get("/:id", ValidateOrgPlaylist, wrap(GetPlaylist))
|
||||
r.Get("/:id/dashboards", ValidateOrgPlaylist, wrap(GetPlaylistDashboards))
|
||||
r.Get("/:id/playlistitems", ValidateOrgPlaylist, wrap(GetPlaylistItems))
|
||||
r.Delete("/:id", reqEditorRole, ValidateOrgPlaylist, wrap(DeletePlaylist))
|
||||
r.Put("/:id", reqEditorRole, bind(m.UpdatePlaylistQuery{}), ValidateOrgPlaylist, wrap(UpdatePlaylist))
|
||||
r.Post("/", reqEditorRole, bind(m.CreatePlaylistQuery{}), wrap(CreatePlaylist))
|
||||
|
||||
+115
-8
@@ -1,9 +1,12 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
"github.com/grafana/grafana/pkg/log"
|
||||
"github.com/grafana/grafana/pkg/middleware"
|
||||
m "github.com/grafana/grafana/pkg/models"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func ValidateOrgPlaylist(c *middleware.Context) {
|
||||
@@ -52,19 +55,67 @@ func GetPlaylist(c *middleware.Context) Response {
|
||||
return ApiError(500, "Playlist not found", err)
|
||||
}
|
||||
|
||||
return Json(200, cmd.Result)
|
||||
itemQuery := m.GetPlaylistItemsByIdQuery{PlaylistId: id}
|
||||
if err := bus.Dispatch(&itemQuery); err != nil {
|
||||
log.Warn("itemQuery failed: %v", err)
|
||||
return ApiError(500, "Playlist items not found", err)
|
||||
}
|
||||
|
||||
playlistDTOs := make([]m.PlaylistItemDTO, 0)
|
||||
|
||||
for _, item := range *itemQuery.Result {
|
||||
playlistDTOs = append(playlistDTOs, m.PlaylistItemDTO{
|
||||
Id: item.Id,
|
||||
PlaylistId: item.PlaylistId,
|
||||
Type: item.Type,
|
||||
Value: item.Value,
|
||||
Order: item.Order,
|
||||
})
|
||||
}
|
||||
|
||||
dto := &m.PlaylistDTO{
|
||||
Id: cmd.Result.Id,
|
||||
Title: cmd.Result.Title,
|
||||
Timespan: cmd.Result.Timespan,
|
||||
OrgId: cmd.Result.OrgId,
|
||||
Items: playlistDTOs,
|
||||
}
|
||||
|
||||
return Json(200, dto)
|
||||
}
|
||||
|
||||
func GetPlaylistDashboards(c *middleware.Context) Response {
|
||||
id := c.ParamsInt64(":id")
|
||||
func LoadPlaylistItems(id int64) ([]m.PlaylistItem, error) {
|
||||
itemQuery := m.GetPlaylistItemsByIdQuery{PlaylistId: id}
|
||||
if err := bus.Dispatch(&itemQuery); err != nil {
|
||||
log.Warn("itemQuery failed: %v", err)
|
||||
return nil, errors.New("Playlist not found")
|
||||
}
|
||||
|
||||
query := m.GetPlaylistDashboardsQuery{Id: id}
|
||||
if err := bus.Dispatch(&query); err != nil {
|
||||
return ApiError(500, "Playlist not found", err)
|
||||
return *itemQuery.Result, nil
|
||||
}
|
||||
|
||||
func LoadPlaylistDashboards(id int64) ([]m.PlaylistDashboardDto, error) {
|
||||
playlistItems, _ := LoadPlaylistItems(id)
|
||||
|
||||
dashboardIds := make([]int64, 0)
|
||||
|
||||
for _, i := range playlistItems {
|
||||
dashboardId, _ := strconv.ParseInt(i.Value, 10, 64)
|
||||
dashboardIds = append(dashboardIds, dashboardId)
|
||||
}
|
||||
|
||||
if len(dashboardIds) == 0 {
|
||||
return make([]m.PlaylistDashboardDto, 0), nil
|
||||
}
|
||||
|
||||
dashboardQuery := m.GetPlaylistDashboardsQuery{DashboardIds: dashboardIds}
|
||||
if err := bus.Dispatch(&dashboardQuery); err != nil {
|
||||
log.Warn("dashboardquery failed: %v", err)
|
||||
return nil, errors.New("Playlist not found")
|
||||
}
|
||||
|
||||
dtos := make([]m.PlaylistDashboardDto, 0)
|
||||
for _, item := range *query.Result {
|
||||
for _, item := range *dashboardQuery.Result {
|
||||
dtos = append(dtos, m.PlaylistDashboardDto{
|
||||
Id: item.Id,
|
||||
Slug: item.Slug,
|
||||
@@ -73,7 +124,43 @@ func GetPlaylistDashboards(c *middleware.Context) Response {
|
||||
})
|
||||
}
|
||||
|
||||
return Json(200, dtos)
|
||||
return dtos, nil
|
||||
}
|
||||
|
||||
func GetPlaylistItems(c *middleware.Context) Response {
|
||||
id := c.ParamsInt64(":id")
|
||||
|
||||
items, err := LoadPlaylistItems(id)
|
||||
|
||||
if err != nil {
|
||||
return ApiError(500, "Could not load playlist items", err)
|
||||
}
|
||||
|
||||
playlistDTOs := make([]m.PlaylistItemDTO, 0)
|
||||
|
||||
for _, item := range items {
|
||||
playlistDTOs = append(playlistDTOs, m.PlaylistItemDTO{
|
||||
Id: item.Id,
|
||||
PlaylistId: item.PlaylistId,
|
||||
Type: item.Type,
|
||||
Value: item.Value,
|
||||
Order: item.Order,
|
||||
Title: item.Title,
|
||||
})
|
||||
}
|
||||
|
||||
return Json(200, playlistDTOs)
|
||||
}
|
||||
|
||||
func GetPlaylistDashboards(c *middleware.Context) Response {
|
||||
id := c.ParamsInt64(":id")
|
||||
|
||||
playlists, err := LoadPlaylistDashboards(id)
|
||||
if err != nil {
|
||||
return ApiError(500, "Could not load dashboards", err)
|
||||
}
|
||||
|
||||
return Json(200, playlists)
|
||||
}
|
||||
|
||||
func DeletePlaylist(c *middleware.Context) Response {
|
||||
@@ -103,5 +190,25 @@ func UpdatePlaylist(c *middleware.Context, query m.UpdatePlaylistQuery) Response
|
||||
return ApiError(500, "Failed to save playlist", err)
|
||||
}
|
||||
|
||||
items, err := LoadPlaylistItems(query.Id)
|
||||
|
||||
playlistDTOs := make([]m.PlaylistItemDTO, 0)
|
||||
|
||||
for _, item := range items {
|
||||
playlistDTOs = append(playlistDTOs, m.PlaylistItemDTO{
|
||||
Id: item.Id,
|
||||
PlaylistId: item.PlaylistId,
|
||||
Type: item.Type,
|
||||
Value: item.Value,
|
||||
Order: item.Order,
|
||||
})
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return ApiError(500, "Failed to save playlist", err)
|
||||
}
|
||||
|
||||
query.Result.Items = playlistDTOs
|
||||
|
||||
return Json(200, query.Result)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user