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-05-19 07:32:10 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/star"
|
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 {
|
2022-05-19 07:32:10 -05:00
|
|
|
query := star.GetUserStarsQuery{
|
2022-08-11 06:28:55 -05:00
|
|
|
UserID: c.SignedInUser.UserID,
|
2022-05-03 10:52:19 -05:00
|
|
|
}
|
|
|
|
|
2022-05-19 07:32:10 -05:00
|
|
|
iuserstars, err := hs.starService.GetByUser(c.Req.Context(), &query)
|
2022-05-03 10:52:19 -05:00
|
|
|
if err != nil {
|
|
|
|
return response.Error(500, "Failed to get user stars", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
uids := []string{}
|
2022-05-19 07:32:10 -05:00
|
|
|
for dashboardId := range iuserstars.UserStars {
|
2022-05-03 10:52:19 -05:00
|
|
|
query := &models.GetDashboardQuery{
|
|
|
|
Id: dashboardId,
|
2022-08-11 06:28:55 -05:00
|
|
|
OrgId: c.OrgID,
|
2022-05-03 10:52:19 -05:00
|
|
|
}
|
2022-07-06 13:42:39 -05:00
|
|
|
err := hs.DashboardService.GetDashboard(c.Req.Context(), query)
|
2022-05-25 13:57:18 -05:00
|
|
|
|
|
|
|
// Grafana admin users may have starred dashboards in multiple orgs. This will avoid returning errors when the dashboard is in another org
|
|
|
|
if err == nil {
|
|
|
|
uids = append(uids, query.Result.Uid)
|
2022-05-03 10:52:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return response.JSON(200, uids)
|
|
|
|
}
|
|
|
|
|
2022-07-27 08:54:37 -05:00
|
|
|
// swagger:route POST /user/stars/dashboard/{dashboard_id} signed_in_user starDashboard
|
|
|
|
//
|
|
|
|
// Star a dashboard.
|
|
|
|
//
|
|
|
|
// Stars the given Dashboard for the actual user.
|
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: okResponse
|
|
|
|
// 400: badRequestError
|
|
|
|
// 401: unauthorisedError
|
|
|
|
// 403: forbiddenError
|
|
|
|
// 500: internalServerError
|
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)
|
|
|
|
}
|
2022-08-11 06:28:55 -05:00
|
|
|
cmd := star.StarDashboardCommand{UserID: c.UserID, DashboardID: id}
|
2015-02-02 04:32:32 -06:00
|
|
|
|
2022-05-19 07:32:10 -05: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-05-19 07:32:10 -05:00
|
|
|
if err := hs.starService.Add(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-07-27 08:54:37 -05:00
|
|
|
// swagger:route DELETE /user/stars/dashboard/{dashboard_id} signed_in_user unstarDashboard
|
|
|
|
//
|
|
|
|
// Unstar a dashboard.
|
|
|
|
//
|
|
|
|
// Deletes the starring of the given Dashboard for the actual user.
|
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: okResponse
|
|
|
|
// 400: badRequestError
|
|
|
|
// 401: unauthorisedError
|
|
|
|
// 403: forbiddenError
|
|
|
|
// 500: internalServerError
|
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)
|
|
|
|
}
|
2022-08-11 06:28:55 -05:00
|
|
|
cmd := star.UnstarDashboardCommand{UserID: c.UserID, DashboardID: id}
|
2015-02-02 04:32:32 -06:00
|
|
|
|
2022-05-19 07:32:10 -05: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-05-19 07:32:10 -05:00
|
|
|
if err := hs.starService.Delete(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
|
|
|
}
|
2022-07-27 08:54:37 -05:00
|
|
|
|
|
|
|
// swagger:parameters starDashboard
|
|
|
|
type StarDashboardParams struct {
|
|
|
|
// in:path
|
|
|
|
// required:true
|
|
|
|
DashboardID string `json:"dashboard_id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// swagger:parameters unstarDashboard
|
|
|
|
type UnstarDashboardParams struct {
|
|
|
|
// in:path
|
|
|
|
// required:true
|
|
|
|
DashboardID string `json:"dashboard_id"`
|
|
|
|
}
|