playlist.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package api
  2. import (
  3. "github.com/grafana/grafana/pkg/bus"
  4. "github.com/grafana/grafana/pkg/middleware"
  5. m "github.com/grafana/grafana/pkg/models"
  6. )
  7. func ValidateOrgPlaylist(c *middleware.Context) {
  8. id := c.ParamsInt64(":id")
  9. query := m.GetPlaylistByIdQuery{Id: id}
  10. err := bus.Dispatch(&query)
  11. if err != nil {
  12. c.JsonApiErr(404, "Playlist not found", err)
  13. return
  14. }
  15. if query.Result.OrgId != c.OrgId {
  16. c.JsonApiErr(403, "You are not allowed to edit/view playlist", nil)
  17. return
  18. }
  19. }
  20. func SearchPlaylists(c *middleware.Context) Response {
  21. query := c.Query("query")
  22. limit := c.QueryInt("limit")
  23. if limit == 0 {
  24. limit = 1000
  25. }
  26. searchQuery := m.PlaylistQuery{
  27. Title: query,
  28. Limit: limit,
  29. OrgId: c.OrgId,
  30. }
  31. err := bus.Dispatch(&searchQuery)
  32. if err != nil {
  33. return ApiError(500, "Search failed", err)
  34. }
  35. return Json(200, searchQuery.Result)
  36. }
  37. func GetPlaylist(c *middleware.Context) Response {
  38. id := c.ParamsInt64(":id")
  39. cmd := m.GetPlaylistByIdQuery{Id: id}
  40. if err := bus.Dispatch(&cmd); err != nil {
  41. return ApiError(500, "Playlist not found", err)
  42. }
  43. return Json(200, cmd.Result)
  44. }
  45. func GetPlaylistDashboards(c *middleware.Context) Response {
  46. id := c.ParamsInt64(":id")
  47. query := m.GetPlaylistDashboardsQuery{Id: id}
  48. if err := bus.Dispatch(&query); err != nil {
  49. return ApiError(500, "Playlist not found", err)
  50. }
  51. dtos := make([]m.PlaylistDashboardDto, 0)
  52. for _, item := range *query.Result {
  53. dtos = append(dtos, m.PlaylistDashboardDto{
  54. Id: item.Id,
  55. Slug: item.Slug,
  56. Title: item.Title,
  57. Uri: "db/" + item.Slug,
  58. })
  59. }
  60. return Json(200, dtos)
  61. }
  62. func DeletePlaylist(c *middleware.Context) Response {
  63. id := c.ParamsInt64(":id")
  64. cmd := m.DeletePlaylistQuery{Id: id}
  65. if err := bus.Dispatch(&cmd); err != nil {
  66. return ApiError(500, "Failed to delete playlist", err)
  67. }
  68. return Json(200, "")
  69. }
  70. func CreatePlaylist(c *middleware.Context, query m.CreatePlaylistQuery) Response {
  71. query.OrgId = c.OrgId
  72. err := bus.Dispatch(&query)
  73. if err != nil {
  74. return ApiError(500, "Failed to create playlist", err)
  75. }
  76. return Json(200, query.Result)
  77. }
  78. func UpdatePlaylist(c *middleware.Context, query m.UpdatePlaylistQuery) Response {
  79. err := bus.Dispatch(&query)
  80. if err != nil {
  81. return ApiError(500, "Failed to save playlist", err)
  82. }
  83. return Json(200, query.Result)
  84. }