playlist.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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) {
  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. c.JsonApiErr(500, "Search failed", err)
  34. return
  35. }
  36. c.JSON(200, searchQuery.Result)
  37. }
  38. func GetPlaylist(c *middleware.Context) {
  39. id := c.ParamsInt64(":id")
  40. cmd := m.GetPlaylistByIdQuery{Id: id}
  41. if err := bus.Dispatch(&cmd); err != nil {
  42. c.JsonApiErr(500, "Playlist not found", err)
  43. return
  44. }
  45. c.JSON(200, cmd.Result)
  46. }
  47. func GetPlaylistDashboards(c *middleware.Context) {
  48. id := c.ParamsInt64(":id")
  49. query := m.GetPlaylistDashboardsQuery{Id: id}
  50. if err := bus.Dispatch(&query); err != nil {
  51. c.JsonApiErr(500, "Playlist not found", err)
  52. return
  53. }
  54. c.JSON(200, query.Result)
  55. }
  56. func DeletePlaylist(c *middleware.Context) {
  57. id := c.ParamsInt64(":id")
  58. cmd := m.DeletePlaylistQuery{Id: id}
  59. if err := bus.Dispatch(&cmd); err != nil {
  60. c.JsonApiErr(500, "Failed to delete playlist", err)
  61. return
  62. }
  63. c.JSON(200, "")
  64. }
  65. func CreatePlaylist(c *middleware.Context, query m.CreatePlaylistQuery) {
  66. query.OrgId = c.OrgId
  67. err := bus.Dispatch(&query)
  68. if err != nil {
  69. c.JsonApiErr(500, "Failed to create playlist", err)
  70. return
  71. }
  72. c.JSON(200, query.Result)
  73. }
  74. func UpdatePlaylist(c *middleware.Context, query m.UpdatePlaylistQuery) {
  75. err := bus.Dispatch(&query)
  76. if err != nil {
  77. c.JsonApiErr(500, "Failed to save playlist", err)
  78. return
  79. }
  80. c.JSON(200, query.Result)
  81. }