dashboard.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package api
  2. import (
  3. "github.com/torkelo/grafana-pro/pkg/api/dtos"
  4. "github.com/torkelo/grafana-pro/pkg/bus"
  5. "github.com/torkelo/grafana-pro/pkg/middleware"
  6. m "github.com/torkelo/grafana-pro/pkg/models"
  7. "github.com/torkelo/grafana-pro/pkg/util"
  8. )
  9. func GetDashboard(c *middleware.Context) {
  10. slug := c.Params(":slug")
  11. query := m.GetDashboardQuery{Slug: slug, AccountId: c.AccountId}
  12. err := bus.Dispatch(&query)
  13. if err != nil {
  14. c.JsonApiErr(404, "Dashboard not found", nil)
  15. return
  16. }
  17. dash := query.Result
  18. dto := dtos.Dashboard{
  19. IsFavorite: false,
  20. Dashboard: dash.Data,
  21. }
  22. c.JSON(200, dto)
  23. }
  24. func DeleteDashboard(c *middleware.Context) {
  25. slug := c.Params(":slug")
  26. query := m.GetDashboardQuery{Slug: slug, AccountId: c.AccountId}
  27. if err := bus.Dispatch(&query); err != nil {
  28. c.JsonApiErr(404, "Dashboard not found", nil)
  29. return
  30. }
  31. cmd := m.DeleteDashboardCommand{Slug: slug, AccountId: c.AccountId}
  32. if err := bus.Dispatch(&cmd); err != nil {
  33. c.JsonApiErr(500, "Failed to delete dashboard", err)
  34. return
  35. }
  36. var resp = map[string]interface{}{"title": query.Result.Title}
  37. c.JSON(200, resp)
  38. }
  39. func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) {
  40. cmd.AccountId = c.AccountId
  41. err := bus.Dispatch(&cmd)
  42. if err != nil {
  43. if err == m.ErrDashboardWithSameNameExists {
  44. c.JsonApiErr(400, "Dashboard with the same title already exists", nil)
  45. return
  46. }
  47. c.JsonApiErr(500, "Failed to save dashboard", err)
  48. return
  49. }
  50. c.JSON(200, util.DynMap{"status": "success", "slug": cmd.Result.Slug})
  51. }