Worked on dashboard starring and unstarring, renamed favorite model to star
This commit is contained in:
+2
-2
@@ -45,8 +45,8 @@ func Register(r *macaron.Macaron) {
|
||||
r.Put("/", bind(m.UpdateUserCommand{}), UpdateUser)
|
||||
r.Post("/using/:id", SetUsingAccount)
|
||||
r.Get("/accounts", GetUserAccounts)
|
||||
r.Post("/favorites/dashboard/:id", AddAsFavorite)
|
||||
r.Delete("/favorites/dashboard/:id", RemoveAsFavorite)
|
||||
r.Post("/stars/dashboard/:id", StarDashboard)
|
||||
r.Delete("/stars/dashboard/:id", UnstarDashboard)
|
||||
})
|
||||
|
||||
// account
|
||||
|
||||
+21
-2
@@ -8,6 +8,19 @@ import (
|
||||
"github.com/torkelo/grafana-pro/pkg/util"
|
||||
)
|
||||
|
||||
func isDasboardStarredByUser(c *middleware.Context, dashId int64) (bool, error) {
|
||||
if !c.IsSignedIn {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
query := m.IsStarredByUserQuery{UserId: c.UserId, DashboardId: dashId}
|
||||
if err := bus.Dispatch(&query); err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return query.Result, nil
|
||||
}
|
||||
|
||||
func GetDashboard(c *middleware.Context) {
|
||||
slug := c.Params(":slug")
|
||||
|
||||
@@ -18,10 +31,16 @@ func GetDashboard(c *middleware.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
isStarred, err := isDasboardStarredByUser(c, query.Result.Id)
|
||||
if err != nil {
|
||||
c.JsonApiErr(500, "Error while checking if dashboard was starred by user", err)
|
||||
return
|
||||
}
|
||||
|
||||
dash := query.Result
|
||||
dto := dtos.Dashboard{
|
||||
IsFavorite: false,
|
||||
Dashboard: dash.Data,
|
||||
Model: dash.Data,
|
||||
Meta: dtos.DashboardMeta{IsStarred: isStarred},
|
||||
}
|
||||
|
||||
c.JSON(200, dto)
|
||||
|
||||
@@ -25,9 +25,13 @@ type CurrentUser struct {
|
||||
GravatarUrl string `json:"gravatarUrl"`
|
||||
}
|
||||
|
||||
type DashboardMeta struct {
|
||||
IsStarred bool `json:"isStarred"`
|
||||
}
|
||||
|
||||
type Dashboard struct {
|
||||
IsFavorite bool `json:"isFavorite"`
|
||||
Dashboard map[string]interface{} `json:"dashboard"`
|
||||
Meta DashboardMeta `json:"meta"`
|
||||
Model map[string]interface{} `json:"model"`
|
||||
}
|
||||
|
||||
type DataSource struct {
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/torkelo/grafana-pro/pkg/bus"
|
||||
"github.com/torkelo/grafana-pro/pkg/middleware"
|
||||
m "github.com/torkelo/grafana-pro/pkg/models"
|
||||
)
|
||||
|
||||
func AddAsFavorite(c *middleware.Context) {
|
||||
var cmd = m.AddAsFavoriteCommand{
|
||||
UserId: c.UserId,
|
||||
DashboardId: c.ParamsInt64(":id"),
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(&cmd); err != nil {
|
||||
c.JsonApiErr(500, "Failed to add favorite", err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JsonOK("Dashboard marked as favorite")
|
||||
}
|
||||
|
||||
func RemoveAsFavorite(c *middleware.Context) {
|
||||
var cmd = m.RemoveAsFavoriteCommand{
|
||||
UserId: c.UserId,
|
||||
DashboardId: c.ParamsInt64(":id"),
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(&cmd); err != nil {
|
||||
c.JsonApiErr(500, "Failed to remove favorite", err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JsonOK("Favorite removed")
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/torkelo/grafana-pro/pkg/bus"
|
||||
"github.com/torkelo/grafana-pro/pkg/middleware"
|
||||
m "github.com/torkelo/grafana-pro/pkg/models"
|
||||
)
|
||||
|
||||
func StarDashboard(c *middleware.Context) {
|
||||
var cmd = m.StarDashboardCommand{
|
||||
UserId: c.UserId,
|
||||
DashboardId: c.ParamsInt64(":id"),
|
||||
}
|
||||
|
||||
if cmd.DashboardId <= 0 {
|
||||
c.JsonApiErr(400, "Missing dashboard id", nil)
|
||||
return
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(&cmd); err != nil {
|
||||
c.JsonApiErr(500, "Failed to star dashboard", err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JsonOK("Dashboard starred!")
|
||||
}
|
||||
|
||||
func UnstarDashboard(c *middleware.Context) {
|
||||
var cmd = m.UnstarDashboardCommand{
|
||||
UserId: c.UserId,
|
||||
DashboardId: c.ParamsInt64(":id"),
|
||||
}
|
||||
|
||||
if cmd.DashboardId <= 0 {
|
||||
c.JsonApiErr(400, "Missing dashboard id", nil)
|
||||
return
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(&cmd); err != nil {
|
||||
c.JsonApiErr(500, "Failed to unstar dashboard", err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JsonOK("Dashboard unstarred")
|
||||
}
|
||||
Reference in New Issue
Block a user