| 
									
										
										
										
											2015-12-22 18:07:15 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/bus" | 
					
						
							| 
									
										
										
										
											2016-01-29 06:56:56 +08:00
										 |  |  | 	_ "github.com/grafana/grafana/pkg/log" | 
					
						
							| 
									
										
										
										
											2015-12-22 18:07:15 +08:00
										 |  |  | 	"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 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-01 20:50:55 +08:00
										 |  |  | 	if query.Result.OrgId == 0 { | 
					
						
							|  |  |  | 		c.JsonApiErr(404, "Playlist not found", err) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-22 18:07:15 +08:00
										 |  |  | 	if query.Result.OrgId != c.OrgId { | 
					
						
							|  |  |  | 		c.JsonApiErr(403, "You are not allowed to edit/view playlist", nil) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-11-06 17:49:43 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	items, itemsErr := LoadPlaylistItemDTOs(id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if itemsErr != nil { | 
					
						
							|  |  |  | 		c.JsonApiErr(404, "Playlist items not found", err) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if len(items) == 0 { | 
					
						
							|  |  |  | 		c.JsonApiErr(404, "Playlist is empty", itemsErr) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-12-22 18:07:15 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-08 21:21:30 +08:00
										 |  |  | func SearchPlaylists(c *middleware.Context) Response { | 
					
						
							| 
									
										
										
										
											2015-12-22 18:07:15 +08:00
										 |  |  | 	query := c.Query("query") | 
					
						
							|  |  |  | 	limit := c.QueryInt("limit") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if limit == 0 { | 
					
						
							|  |  |  | 		limit = 1000 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-18 23:00:11 +08:00
										 |  |  | 	searchQuery := m.GetPlaylistsQuery{ | 
					
						
							|  |  |  | 		Name:  query, | 
					
						
							| 
									
										
										
										
											2015-12-22 18:07:15 +08:00
										 |  |  | 		Limit: limit, | 
					
						
							|  |  |  | 		OrgId: c.OrgId, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	err := bus.Dispatch(&searchQuery) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-01-08 21:21:30 +08:00
										 |  |  | 		return ApiError(500, "Search failed", err) | 
					
						
							| 
									
										
										
										
											2015-12-22 18:07:15 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-08 21:21:30 +08:00
										 |  |  | 	return Json(200, searchQuery.Result) | 
					
						
							| 
									
										
										
										
											2015-12-22 18:07:15 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-08 21:21:30 +08:00
										 |  |  | func GetPlaylist(c *middleware.Context) Response { | 
					
						
							| 
									
										
										
										
											2015-12-22 18:07:15 +08:00
										 |  |  | 	id := c.ParamsInt64(":id") | 
					
						
							|  |  |  | 	cmd := m.GetPlaylistByIdQuery{Id: id} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&cmd); err != nil { | 
					
						
							| 
									
										
										
										
											2016-01-08 21:21:30 +08:00
										 |  |  | 		return ApiError(500, "Playlist not found", err) | 
					
						
							| 
									
										
										
										
											2015-12-22 18:07:15 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-12 15:36:25 +08:00
										 |  |  | 	playlistDTOs, _ := LoadPlaylistItemDTOs(id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	dto := &m.PlaylistDTO{ | 
					
						
							|  |  |  | 		Id:       cmd.Result.Id, | 
					
						
							| 
									
										
										
										
											2016-01-18 23:00:11 +08:00
										 |  |  | 		Name:     cmd.Result.Name, | 
					
						
							| 
									
										
										
										
											2016-01-12 20:56:47 +08:00
										 |  |  | 		Interval: cmd.Result.Interval, | 
					
						
							| 
									
										
										
										
											2016-01-12 15:36:25 +08:00
										 |  |  | 		OrgId:    cmd.Result.OrgId, | 
					
						
							|  |  |  | 		Items:    playlistDTOs, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return Json(200, dto) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func LoadPlaylistItemDTOs(id int64) ([]m.PlaylistItemDTO, error) { | 
					
						
							|  |  |  | 	playlistitems, err := LoadPlaylistItems(id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							| 
									
										
										
										
											2016-01-09 00:32:55 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	playlistDTOs := make([]m.PlaylistItemDTO, 0) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-12 15:36:25 +08:00
										 |  |  | 	for _, item := range playlistitems { | 
					
						
							| 
									
										
										
										
											2016-01-09 00:32:55 +08:00
										 |  |  | 		playlistDTOs = append(playlistDTOs, m.PlaylistItemDTO{ | 
					
						
							|  |  |  | 			Id:         item.Id, | 
					
						
							|  |  |  | 			PlaylistId: item.PlaylistId, | 
					
						
							|  |  |  | 			Type:       item.Type, | 
					
						
							|  |  |  | 			Value:      item.Value, | 
					
						
							|  |  |  | 			Order:      item.Order, | 
					
						
							| 
									
										
										
										
											2016-01-12 15:36:25 +08:00
										 |  |  | 			Title:      item.Title, | 
					
						
							| 
									
										
										
										
											2016-01-09 00:32:55 +08:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-12 15:36:25 +08:00
										 |  |  | 	return playlistDTOs, nil | 
					
						
							| 
									
										
										
										
											2015-12-22 18:07:15 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-09 00:32:55 +08:00
										 |  |  | func LoadPlaylistItems(id int64) ([]m.PlaylistItem, error) { | 
					
						
							|  |  |  | 	itemQuery := m.GetPlaylistItemsByIdQuery{PlaylistId: id} | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&itemQuery); err != nil { | 
					
						
							| 
									
										
										
										
											2016-01-12 21:28:19 +08:00
										 |  |  | 		return nil, err | 
					
						
							| 
									
										
										
										
											2016-01-09 00:32:55 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-12-22 18:07:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-09 00:32:55 +08:00
										 |  |  | 	return *itemQuery.Result, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func GetPlaylistItems(c *middleware.Context) Response { | 
					
						
							|  |  |  | 	id := c.ParamsInt64(":id") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-12 15:36:25 +08:00
										 |  |  | 	playlistDTOs, err := LoadPlaylistItemDTOs(id) | 
					
						
							| 
									
										
										
										
											2016-01-09 00:32:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return ApiError(500, "Could not load playlist items", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return Json(200, playlistDTOs) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func GetPlaylistDashboards(c *middleware.Context) Response { | 
					
						
							| 
									
										
										
										
											2016-01-29 06:56:56 +08:00
										 |  |  | 	playlistId := c.ParamsInt64(":id") | 
					
						
							| 
									
										
										
										
											2016-01-09 00:32:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-17 08:33:53 +08:00
										 |  |  | 	playlists, err := LoadPlaylistDashboards(c.OrgId, c.SignedInUser, playlistId) | 
					
						
							| 
									
										
										
										
											2016-01-09 00:32:55 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return ApiError(500, "Could not load dashboards", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return Json(200, playlists) | 
					
						
							| 
									
										
										
										
											2015-12-22 18:07:15 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-08 21:21:30 +08:00
										 |  |  | func DeletePlaylist(c *middleware.Context) Response { | 
					
						
							| 
									
										
										
										
											2015-12-22 18:07:15 +08:00
										 |  |  | 	id := c.ParamsInt64(":id") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-18 23:00:11 +08:00
										 |  |  | 	cmd := m.DeletePlaylistCommand{Id: id, OrgId: c.OrgId} | 
					
						
							| 
									
										
										
										
											2015-12-22 18:07:15 +08:00
										 |  |  | 	if err := bus.Dispatch(&cmd); err != nil { | 
					
						
							| 
									
										
										
										
											2016-01-08 21:21:30 +08:00
										 |  |  | 		return ApiError(500, "Failed to delete playlist", err) | 
					
						
							| 
									
										
										
										
											2015-12-22 18:07:15 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-08 21:21:30 +08:00
										 |  |  | 	return Json(200, "") | 
					
						
							| 
									
										
										
										
											2015-12-22 18:07:15 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-18 23:00:11 +08:00
										 |  |  | func CreatePlaylist(c *middleware.Context, cmd m.CreatePlaylistCommand) Response { | 
					
						
							|  |  |  | 	cmd.OrgId = c.OrgId | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&cmd); err != nil { | 
					
						
							| 
									
										
										
										
											2016-01-08 21:21:30 +08:00
										 |  |  | 		return ApiError(500, "Failed to create playlist", err) | 
					
						
							| 
									
										
										
										
											2015-12-22 18:07:15 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-18 23:00:11 +08:00
										 |  |  | 	return Json(200, cmd.Result) | 
					
						
							| 
									
										
										
										
											2015-12-22 18:07:15 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-18 23:00:11 +08:00
										 |  |  | func UpdatePlaylist(c *middleware.Context, cmd m.UpdatePlaylistCommand) Response { | 
					
						
							|  |  |  | 	cmd.OrgId = c.OrgId | 
					
						
							| 
									
										
										
										
											2016-01-18 23:05:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-18 23:00:11 +08:00
										 |  |  | 	if err := bus.Dispatch(&cmd); err != nil { | 
					
						
							| 
									
										
										
										
											2016-01-08 21:21:30 +08:00
										 |  |  | 		return ApiError(500, "Failed to save playlist", err) | 
					
						
							| 
									
										
										
										
											2015-12-22 18:07:15 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-18 23:00:11 +08:00
										 |  |  | 	playlistDTOs, err := LoadPlaylistItemDTOs(cmd.Id) | 
					
						
							| 
									
										
										
										
											2016-01-09 00:32:55 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return ApiError(500, "Failed to save playlist", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-18 23:00:11 +08:00
										 |  |  | 	cmd.Result.Items = playlistDTOs | 
					
						
							|  |  |  | 	return Json(200, cmd.Result) | 
					
						
							| 
									
										
										
										
											2015-12-22 18:07:15 +08:00
										 |  |  | } |