2015-12-22 04:07:15 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-01-15 07:43:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
2015-12-22 04:07:15 -06:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2020-03-04 05:57:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2015-12-22 04:07:15 -06:00
|
|
|
)
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
func ValidateOrgPlaylist(c *models.ReqContext) {
|
2015-12-22 04:07:15 -06:00
|
|
|
id := c.ParamsInt64(":id")
|
2020-03-04 05:57:20 -06:00
|
|
|
query := models.GetPlaylistByIdQuery{Id: id}
|
2015-12-22 04:07:15 -06:00
|
|
|
err := bus.Dispatch(&query)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.JsonApiErr(404, "Playlist not found", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-01 06:50:55 -06:00
|
|
|
if query.Result.OrgId == 0 {
|
|
|
|
c.JsonApiErr(404, "Playlist not found", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-22 04:07:15 -06:00
|
|
|
if query.Result.OrgId != c.OrgId {
|
|
|
|
c.JsonApiErr(403, "You are not allowed to edit/view playlist", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
func SearchPlaylists(c *models.ReqContext) response.Response {
|
2015-12-22 04:07:15 -06:00
|
|
|
query := c.Query("query")
|
|
|
|
limit := c.QueryInt("limit")
|
|
|
|
|
|
|
|
if limit == 0 {
|
|
|
|
limit = 1000
|
|
|
|
}
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
searchQuery := models.GetPlaylistsQuery{
|
2016-01-18 09:00:11 -06:00
|
|
|
Name: query,
|
2015-12-22 04:07:15 -06:00
|
|
|
Limit: limit,
|
|
|
|
OrgId: c.OrgId,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := bus.Dispatch(&searchQuery)
|
|
|
|
if err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Search failed", err)
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, searchQuery.Result)
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
func GetPlaylist(c *models.ReqContext) response.Response {
|
2015-12-22 04:07:15 -06:00
|
|
|
id := c.ParamsInt64(":id")
|
2020-03-04 05:57:20 -06:00
|
|
|
cmd := models.GetPlaylistByIdQuery{Id: id}
|
2015-12-22 04:07:15 -06:00
|
|
|
|
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Playlist not found", err)
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2016-01-12 01:36:25 -06:00
|
|
|
playlistDTOs, _ := LoadPlaylistItemDTOs(id)
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
dto := &models.PlaylistDTO{
|
2016-01-12 01:36:25 -06:00
|
|
|
Id: cmd.Result.Id,
|
2016-01-18 09:00:11 -06:00
|
|
|
Name: cmd.Result.Name,
|
2016-01-12 06:56:47 -06:00
|
|
|
Interval: cmd.Result.Interval,
|
2016-01-12 01:36:25 -06:00
|
|
|
OrgId: cmd.Result.OrgId,
|
|
|
|
Items: playlistDTOs,
|
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, dto)
|
2016-01-12 01:36:25 -06:00
|
|
|
}
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
func LoadPlaylistItemDTOs(id int64) ([]models.PlaylistItemDTO, error) {
|
2016-01-12 01:36:25 -06:00
|
|
|
playlistitems, err := LoadPlaylistItems(id)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-01-08 10:32:55 -06:00
|
|
|
}
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
playlistDTOs := make([]models.PlaylistItemDTO, 0)
|
2016-01-08 10:32:55 -06:00
|
|
|
|
2016-01-12 01:36:25 -06:00
|
|
|
for _, item := range playlistitems {
|
2020-03-04 05:57:20 -06:00
|
|
|
playlistDTOs = append(playlistDTOs, models.PlaylistItemDTO{
|
2016-01-08 10:32:55 -06:00
|
|
|
Id: item.Id,
|
|
|
|
PlaylistId: item.PlaylistId,
|
|
|
|
Type: item.Type,
|
|
|
|
Value: item.Value,
|
|
|
|
Order: item.Order,
|
2016-01-12 01:36:25 -06:00
|
|
|
Title: item.Title,
|
2016-01-08 10:32:55 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-01-12 01:36:25 -06:00
|
|
|
return playlistDTOs, nil
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
func LoadPlaylistItems(id int64) ([]models.PlaylistItem, error) {
|
|
|
|
itemQuery := models.GetPlaylistItemsByIdQuery{PlaylistId: id}
|
2016-01-08 10:32:55 -06:00
|
|
|
if err := bus.Dispatch(&itemQuery); err != nil {
|
2016-01-12 07:28:19 -06:00
|
|
|
return nil, err
|
2016-01-08 10:32:55 -06:00
|
|
|
}
|
2015-12-22 04:07:15 -06:00
|
|
|
|
2016-01-08 10:32:55 -06:00
|
|
|
return *itemQuery.Result, nil
|
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
func GetPlaylistItems(c *models.ReqContext) response.Response {
|
2016-01-08 10:32:55 -06:00
|
|
|
id := c.ParamsInt64(":id")
|
|
|
|
|
2016-01-12 01:36:25 -06:00
|
|
|
playlistDTOs, err := LoadPlaylistItemDTOs(id)
|
2016-01-08 10:32:55 -06:00
|
|
|
|
|
|
|
if err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Could not load playlist items", err)
|
2016-01-08 10:32:55 -06:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, playlistDTOs)
|
2016-01-08 10:32:55 -06:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
func GetPlaylistDashboards(c *models.ReqContext) response.Response {
|
2018-03-22 06:37:35 -05:00
|
|
|
playlistID := c.ParamsInt64(":id")
|
2016-01-08 10:32:55 -06:00
|
|
|
|
2018-03-22 06:37:35 -05:00
|
|
|
playlists, err := LoadPlaylistDashboards(c.OrgId, c.SignedInUser, playlistID)
|
2016-01-08 10:32:55 -06:00
|
|
|
if err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Could not load dashboards", err)
|
2016-01-08 10:32:55 -06:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, playlists)
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
func DeletePlaylist(c *models.ReqContext) response.Response {
|
2015-12-22 04:07:15 -06:00
|
|
|
id := c.ParamsInt64(":id")
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
cmd := models.DeletePlaylistCommand{Id: id, OrgId: c.OrgId}
|
2015-12-22 04:07:15 -06:00
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to delete playlist", err)
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, "")
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
func CreatePlaylist(c *models.ReqContext, cmd models.CreatePlaylistCommand) response.Response {
|
2016-01-18 09:00:11 -06:00
|
|
|
cmd.OrgId = c.OrgId
|
|
|
|
|
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to create playlist", err)
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, cmd.Result)
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
func UpdatePlaylist(c *models.ReqContext, cmd models.UpdatePlaylistCommand) response.Response {
|
2016-01-18 09:00:11 -06:00
|
|
|
cmd.OrgId = c.OrgId
|
2018-07-17 09:45:39 -05:00
|
|
|
cmd.Id = c.ParamsInt64(":id")
|
2016-01-18 09:05:38 -06:00
|
|
|
|
2016-01-18 09:00:11 -06:00
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to save playlist", err)
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2016-01-18 09:00:11 -06:00
|
|
|
playlistDTOs, err := LoadPlaylistItemDTOs(cmd.Id)
|
2016-01-08 10:32:55 -06:00
|
|
|
if err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to save playlist", err)
|
2016-01-08 10:32:55 -06:00
|
|
|
}
|
|
|
|
|
2016-01-18 09:00:11 -06:00
|
|
|
cmd.Result.Items = playlistDTOs
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, cmd.Result)
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|