playlist.go 3.72 KB
Newer Older
1 2 3 4
package api

import (
	"github.com/grafana/grafana/pkg/bus"
5
	_ "github.com/grafana/grafana/pkg/log"
6 7 8
	m "github.com/grafana/grafana/pkg/models"
)

9
func ValidateOrgPlaylist(c *m.ReqContext) {
10 11 12 13 14 15 16 17 18
	id := c.ParamsInt64(":id")
	query := m.GetPlaylistByIdQuery{Id: id}
	err := bus.Dispatch(&query)

	if err != nil {
		c.JsonApiErr(404, "Playlist not found", err)
		return
	}

19 20 21 22 23
	if query.Result.OrgId == 0 {
		c.JsonApiErr(404, "Playlist not found", err)
		return
	}

24 25 26 27
	if query.Result.OrgId != c.OrgId {
		c.JsonApiErr(403, "You are not allowed to edit/view playlist", nil)
		return
	}
28 29 30 31 32 33 34 35

	items, itemsErr := LoadPlaylistItemDTOs(id)

	if itemsErr != nil {
		c.JsonApiErr(404, "Playlist items not found", err)
		return
	}

36
	if len(items) == 0 && c.Context.Req.Method != "DELETE" {
37 38 39
		c.JsonApiErr(404, "Playlist is empty", itemsErr)
		return
	}
40 41
}

42
func SearchPlaylists(c *m.ReqContext) Response {
43 44 45 46 47 48 49
	query := c.Query("query")
	limit := c.QueryInt("limit")

	if limit == 0 {
		limit = 1000
	}

50 51
	searchQuery := m.GetPlaylistsQuery{
		Name:  query,
52 53 54 55 56 57
		Limit: limit,
		OrgId: c.OrgId,
	}

	err := bus.Dispatch(&searchQuery)
	if err != nil {
58
		return Error(500, "Search failed", err)
59 60
	}

61
	return JSON(200, searchQuery.Result)
62 63
}

64
func GetPlaylist(c *m.ReqContext) Response {
65 66 67 68
	id := c.ParamsInt64(":id")
	cmd := m.GetPlaylistByIdQuery{Id: id}

	if err := bus.Dispatch(&cmd); err != nil {
69
		return Error(500, "Playlist not found", err)
70 71
	}

72 73 74 75
	playlistDTOs, _ := LoadPlaylistItemDTOs(id)

	dto := &m.PlaylistDTO{
		Id:       cmd.Result.Id,
76
		Name:     cmd.Result.Name,
77
		Interval: cmd.Result.Interval,
78 79 80 81
		OrgId:    cmd.Result.OrgId,
		Items:    playlistDTOs,
	}

82
	return JSON(200, dto)
83 84 85 86 87 88 89
}

func LoadPlaylistItemDTOs(id int64) ([]m.PlaylistItemDTO, error) {
	playlistitems, err := LoadPlaylistItems(id)

	if err != nil {
		return nil, err
90 91 92 93
	}

	playlistDTOs := make([]m.PlaylistItemDTO, 0)

94
	for _, item := range playlistitems {
95 96 97 98 99 100
		playlistDTOs = append(playlistDTOs, m.PlaylistItemDTO{
			Id:         item.Id,
			PlaylistId: item.PlaylistId,
			Type:       item.Type,
			Value:      item.Value,
			Order:      item.Order,
101
			Title:      item.Title,
102 103 104
		})
	}

105
	return playlistDTOs, nil
106 107
}

108 109 110
func LoadPlaylistItems(id int64) ([]m.PlaylistItem, error) {
	itemQuery := m.GetPlaylistItemsByIdQuery{PlaylistId: id}
	if err := bus.Dispatch(&itemQuery); err != nil {
111
		return nil, err
112
	}
113

114 115 116
	return *itemQuery.Result, nil
}

117
func GetPlaylistItems(c *m.ReqContext) Response {
118 119
	id := c.ParamsInt64(":id")

120
	playlistDTOs, err := LoadPlaylistItemDTOs(id)
121 122

	if err != nil {
123
		return Error(500, "Could not load playlist items", err)
124 125
	}

126
	return JSON(200, playlistDTOs)
127 128
}

129
func GetPlaylistDashboards(c *m.ReqContext) Response {
130
	playlistID := c.ParamsInt64(":id")
131

132
	playlists, err := LoadPlaylistDashboards(c.OrgId, c.SignedInUser, playlistID)
133
	if err != nil {
134
		return Error(500, "Could not load dashboards", err)
135 136
	}

137
	return JSON(200, playlists)
138 139
}

140
func DeletePlaylist(c *m.ReqContext) Response {
141 142
	id := c.ParamsInt64(":id")

143
	cmd := m.DeletePlaylistCommand{Id: id, OrgId: c.OrgId}
144
	if err := bus.Dispatch(&cmd); err != nil {
145
		return Error(500, "Failed to delete playlist", err)
146 147
	}

148
	return JSON(200, "")
149 150
}

151
func CreatePlaylist(c *m.ReqContext, cmd m.CreatePlaylistCommand) Response {
152 153 154
	cmd.OrgId = c.OrgId

	if err := bus.Dispatch(&cmd); err != nil {
155
		return Error(500, "Failed to create playlist", err)
156 157
	}

158
	return JSON(200, cmd.Result)
159 160
}

161
func UpdatePlaylist(c *m.ReqContext, cmd m.UpdatePlaylistCommand) Response {
162
	cmd.OrgId = c.OrgId
163
	cmd.Id = c.ParamsInt64(":id")
164

165
	if err := bus.Dispatch(&cmd); err != nil {
166
		return Error(500, "Failed to save playlist", err)
167 168
	}

169
	playlistDTOs, err := LoadPlaylistItemDTOs(cmd.Id)
170
	if err != nil {
171
		return Error(500, "Failed to save playlist", err)
172 173
	}

174
	cmd.Result.Items = playlistDTOs
175
	return JSON(200, cmd.Result)
176
}