2015-12-22 04:07:15 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
"github.com/grafana/grafana/pkg/middleware"
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ValidateOrgPlaylist(c *middleware.Context) {
|
|
|
|
id := c.ParamsInt64(":id")
|
|
|
|
query := m.GetPlaylistByIdQuery{Id: id}
|
|
|
|
err := bus.Dispatch(&query)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.JsonApiErr(404, "Playlist not found", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if query.Result.OrgId != c.OrgId {
|
|
|
|
c.JsonApiErr(403, "You are not allowed to edit/view playlist", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-08 07:21:30 -06:00
|
|
|
func SearchPlaylists(c *middleware.Context) Response {
|
2015-12-22 04:07:15 -06:00
|
|
|
query := c.Query("query")
|
|
|
|
limit := c.QueryInt("limit")
|
|
|
|
|
|
|
|
if limit == 0 {
|
|
|
|
limit = 1000
|
|
|
|
}
|
|
|
|
|
|
|
|
searchQuery := m.PlaylistQuery{
|
|
|
|
Title: query,
|
|
|
|
Limit: limit,
|
|
|
|
OrgId: c.OrgId,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := bus.Dispatch(&searchQuery)
|
|
|
|
if err != nil {
|
2016-01-08 07:21:30 -06:00
|
|
|
return ApiError(500, "Search failed", err)
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2016-01-08 07:21:30 -06:00
|
|
|
return Json(200, searchQuery.Result)
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2016-01-08 07:21:30 -06:00
|
|
|
func GetPlaylist(c *middleware.Context) Response {
|
2015-12-22 04:07:15 -06:00
|
|
|
id := c.ParamsInt64(":id")
|
|
|
|
cmd := m.GetPlaylistByIdQuery{Id: id}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
2016-01-08 07:21:30 -06:00
|
|
|
return ApiError(500, "Playlist not found", err)
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2016-01-08 07:21:30 -06:00
|
|
|
return Json(200, cmd.Result)
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2016-01-08 07:21:30 -06:00
|
|
|
func GetPlaylistDashboards(c *middleware.Context) Response {
|
2015-12-22 04:07:15 -06:00
|
|
|
id := c.ParamsInt64(":id")
|
|
|
|
|
|
|
|
query := m.GetPlaylistDashboardsQuery{Id: id}
|
|
|
|
if err := bus.Dispatch(&query); err != nil {
|
2016-01-08 07:21:30 -06:00
|
|
|
return ApiError(500, "Playlist not found", err)
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2016-01-08 09:13:49 -06:00
|
|
|
dtos := make([]m.PlaylistDashboardDto, 0)
|
|
|
|
for _, item := range *query.Result {
|
|
|
|
dtos = append(dtos, m.PlaylistDashboardDto{
|
|
|
|
Id: item.Id,
|
|
|
|
Slug: item.Slug,
|
|
|
|
Title: item.Title,
|
|
|
|
Uri: "db/" + item.Slug,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return Json(200, dtos)
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2016-01-08 07:21:30 -06:00
|
|
|
func DeletePlaylist(c *middleware.Context) Response {
|
2015-12-22 04:07:15 -06:00
|
|
|
id := c.ParamsInt64(":id")
|
|
|
|
|
|
|
|
cmd := m.DeletePlaylistQuery{Id: id}
|
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
2016-01-08 07:21:30 -06:00
|
|
|
return ApiError(500, "Failed to delete playlist", err)
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2016-01-08 07:21:30 -06:00
|
|
|
return Json(200, "")
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2016-01-08 07:21:30 -06:00
|
|
|
func CreatePlaylist(c *middleware.Context, query m.CreatePlaylistQuery) Response {
|
2015-12-22 04:07:15 -06:00
|
|
|
query.OrgId = c.OrgId
|
|
|
|
err := bus.Dispatch(&query)
|
|
|
|
if err != nil {
|
2016-01-08 07:21:30 -06:00
|
|
|
return ApiError(500, "Failed to create playlist", err)
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2016-01-08 07:21:30 -06:00
|
|
|
return Json(200, query.Result)
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2016-01-08 07:21:30 -06:00
|
|
|
func UpdatePlaylist(c *middleware.Context, query m.UpdatePlaylistQuery) Response {
|
2015-12-22 04:07:15 -06:00
|
|
|
err := bus.Dispatch(&query)
|
|
|
|
if err != nil {
|
2016-01-08 07:21:30 -06:00
|
|
|
return ApiError(500, "Failed to save playlist", err)
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2016-01-08 07:21:30 -06:00
|
|
|
return Json(200, query.Result)
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|