2015-02-02 04:32:32 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2022-01-14 10:55:57 -06:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
2020-03-04 05:57:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2022-01-14 10:55:57 -06:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2015-02-02 04:32:32 -06:00
|
|
|
)
|
|
|
|
|
2022-05-03 10:52:19 -05:00
|
|
|
func (hs *HTTPServer) GetStars(c *models.ReqContext) response.Response {
|
|
|
|
query := models.GetUserStarsQuery{
|
|
|
|
UserId: c.SignedInUser.UserId,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := hs.SQLStore.GetUserStars(c.Req.Context(), &query)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(500, "Failed to get user stars", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
iuserstars := query.Result
|
|
|
|
uids := []string{}
|
|
|
|
for dashboardId := range iuserstars {
|
|
|
|
query := &models.GetDashboardQuery{
|
|
|
|
Id: dashboardId,
|
|
|
|
OrgId: c.OrgId,
|
|
|
|
}
|
2022-05-17 13:52:22 -05:00
|
|
|
err := hs.dashboardService.GetDashboard(c.Req.Context(), query)
|
2022-05-03 10:52:19 -05:00
|
|
|
if err != nil {
|
|
|
|
return response.Error(500, "Failed to get dashboard", err)
|
|
|
|
}
|
|
|
|
uids = append(uids, query.Result.Uid)
|
|
|
|
}
|
|
|
|
return response.JSON(200, uids)
|
|
|
|
}
|
|
|
|
|
2022-01-24 04:52:35 -06:00
|
|
|
func (hs *HTTPServer) StarDashboard(c *models.ReqContext) response.Response {
|
2022-01-14 10:55:57 -06:00
|
|
|
id, err := strconv.ParseInt(web.Params(c.Req)[":id"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "id is invalid", err)
|
|
|
|
}
|
|
|
|
cmd := models.StarDashboardCommand{UserId: c.UserId, DashboardId: id}
|
2015-02-02 04:32:32 -06:00
|
|
|
|
|
|
|
if cmd.DashboardId <= 0 {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(400, "Missing dashboard id", nil)
|
2015-02-02 04:32:32 -06:00
|
|
|
}
|
|
|
|
|
2022-01-24 04:52:35 -06:00
|
|
|
if err := hs.SQLStore.StarDashboard(c.Req.Context(), &cmd); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to star dashboard", err)
|
2015-02-02 04:32:32 -06:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Success("Dashboard starred!")
|
2015-02-02 04:32:32 -06:00
|
|
|
}
|
|
|
|
|
2022-01-24 04:52:35 -06:00
|
|
|
func (hs *HTTPServer) UnstarDashboard(c *models.ReqContext) response.Response {
|
2022-01-14 10:55:57 -06:00
|
|
|
id, err := strconv.ParseInt(web.Params(c.Req)[":id"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "id is invalid", err)
|
|
|
|
}
|
|
|
|
cmd := models.UnstarDashboardCommand{UserId: c.UserId, DashboardId: id}
|
2015-02-02 04:32:32 -06:00
|
|
|
|
|
|
|
if cmd.DashboardId <= 0 {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(400, "Missing dashboard id", nil)
|
2015-02-02 04:32:32 -06:00
|
|
|
}
|
|
|
|
|
2022-01-24 04:52:35 -06:00
|
|
|
if err := hs.SQLStore.UnstarDashboard(c.Req.Context(), &cmd); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to unstar dashboard", err)
|
2015-02-02 04:32:32 -06:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Success("Dashboard unstarred")
|
2015-02-02 04:32:32 -06:00
|
|
|
}
|