mirror of
https://github.com/grafana/grafana.git
synced 2026-09-05 04:40:13 -05:00
[API Split] Move star api inside of packages (#61987)
move star api inside of packages
This commit is contained in:
+7
-5
@@ -233,14 +233,16 @@ func (hs *HTTPServer) registerRoutes() {
|
||||
userRoute.Get("/orgs", routing.Wrap(hs.GetSignedInUserOrgList))
|
||||
userRoute.Get("/teams", routing.Wrap(hs.GetSignedInUserTeamList))
|
||||
|
||||
userRoute.Get("/stars", routing.Wrap(hs.GetStars))
|
||||
userRoute.Get("/stars", routing.Wrap(hs.starApi.GetStars))
|
||||
// Deprecated: use /stars/dashboard/uid/:uid API instead.
|
||||
userRoute.Post("/stars/dashboard/:id", routing.Wrap(hs.StarDashboard))
|
||||
// nolint:staticcheck
|
||||
userRoute.Post("/stars/dashboard/:id", routing.Wrap(hs.starApi.StarDashboard))
|
||||
// Deprecated: use /stars/dashboard/uid/:uid API instead.
|
||||
userRoute.Delete("/stars/dashboard/:id", routing.Wrap(hs.UnstarDashboard))
|
||||
// nolint:staticcheck
|
||||
userRoute.Delete("/stars/dashboard/:id", routing.Wrap(hs.starApi.UnstarDashboard))
|
||||
|
||||
userRoute.Post("/stars/dashboard/uid/:uid", routing.Wrap(hs.StarDashboardByUID))
|
||||
userRoute.Delete("/stars/dashboard/uid/:uid", routing.Wrap(hs.UnstarDashboardByUID))
|
||||
userRoute.Post("/stars/dashboard/uid/:uid", routing.Wrap(hs.starApi.StarDashboardByUID))
|
||||
userRoute.Delete("/stars/dashboard/uid/:uid", routing.Wrap(hs.starApi.UnstarDashboardByUID))
|
||||
|
||||
userRoute.Put("/password", routing.Wrap(hs.ChangeUserPassword))
|
||||
userRoute.Get("/quotas", routing.Wrap(hs.GetUserQuotas))
|
||||
|
||||
@@ -89,6 +89,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/shorturls"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
"github.com/grafana/grafana/pkg/services/star"
|
||||
starApi "github.com/grafana/grafana/pkg/services/star/api"
|
||||
"github.com/grafana/grafana/pkg/services/stats"
|
||||
"github.com/grafana/grafana/pkg/services/store"
|
||||
"github.com/grafana/grafana/pkg/services/store/entity/httpentitystore"
|
||||
@@ -213,6 +214,7 @@ type HTTPServer struct {
|
||||
oauthTokenService oauthtoken.OAuthTokenService
|
||||
statsService stats.Service
|
||||
authnService authn.Service
|
||||
starApi *starApi.API
|
||||
}
|
||||
|
||||
type ServerOptions struct {
|
||||
@@ -257,6 +259,7 @@ func ProvideHTTPServer(opts ServerOptions, cfg *setting.Cfg, routeRegister routi
|
||||
queryLibraryHTTPService querylibrary.HTTPService, queryLibraryService querylibrary.Service, oauthTokenService oauthtoken.OAuthTokenService,
|
||||
statsService stats.Service, authnService authn.Service,
|
||||
k8saccess k8saccess.K8SAccess, // required so that the router is registered
|
||||
starApi *starApi.API,
|
||||
) (*HTTPServer, error) {
|
||||
web.Env = cfg.Env
|
||||
m := web.New()
|
||||
@@ -363,6 +366,7 @@ func ProvideHTTPServer(opts ServerOptions, cfg *setting.Cfg, routeRegister routi
|
||||
oauthTokenService: oauthTokenService,
|
||||
statsService: statsService,
|
||||
authnService: authnService,
|
||||
starApi: starApi,
|
||||
}
|
||||
if hs.Listener != nil {
|
||||
hs.log.Debug("Using provided listener")
|
||||
|
||||
@@ -1,195 +0,0 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/grafana/grafana/pkg/api/response"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/dashboards"
|
||||
"github.com/grafana/grafana/pkg/services/star"
|
||||
"github.com/grafana/grafana/pkg/web"
|
||||
)
|
||||
|
||||
func (hs *HTTPServer) GetStars(c *models.ReqContext) response.Response {
|
||||
query := star.GetUserStarsQuery{
|
||||
UserID: c.SignedInUser.UserID,
|
||||
}
|
||||
|
||||
iuserstars, err := hs.starService.GetByUser(c.Req.Context(), &query)
|
||||
if err != nil {
|
||||
return response.Error(500, "Failed to get user stars", err)
|
||||
}
|
||||
|
||||
uids := []string{}
|
||||
for dashboardId := range iuserstars.UserStars {
|
||||
query := &dashboards.GetDashboardQuery{
|
||||
ID: dashboardId,
|
||||
OrgID: c.OrgID,
|
||||
}
|
||||
queryResult, err := hs.DashboardService.GetDashboard(c.Req.Context(), query)
|
||||
|
||||
// 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, queryResult.UID)
|
||||
}
|
||||
}
|
||||
return response.JSON(200, uids)
|
||||
}
|
||||
|
||||
// swagger:route POST /user/stars/dashboard/{dashboard_id} signed_in_user starDashboard
|
||||
//
|
||||
// Star a dashboard.
|
||||
//
|
||||
// Stars the given Dashboard for the actual user.
|
||||
//
|
||||
// Deprecated: true
|
||||
//
|
||||
// Responses:
|
||||
// 200: okResponse
|
||||
// 400: badRequestError
|
||||
// 401: unauthorisedError
|
||||
// 403: forbiddenError
|
||||
// 500: internalServerError
|
||||
func (hs *HTTPServer) StarDashboard(c *models.ReqContext) response.Response {
|
||||
id, err := strconv.ParseInt(web.Params(c.Req)[":id"], 10, 64)
|
||||
if err != nil {
|
||||
return response.Error(http.StatusBadRequest, "Invalid dashboard ID", nil)
|
||||
}
|
||||
|
||||
cmd := star.StarDashboardCommand{UserID: c.UserID, DashboardID: id}
|
||||
if cmd.DashboardID <= 0 {
|
||||
return response.Error(400, "Missing dashboard id", nil)
|
||||
}
|
||||
|
||||
if err := hs.starService.Add(c.Req.Context(), &cmd); err != nil {
|
||||
return response.Error(http.StatusInternalServerError, "Failed to star dashboard", err)
|
||||
}
|
||||
|
||||
return response.Success("Dashboard starred!")
|
||||
}
|
||||
|
||||
// swagger:route POST /user/stars/dashboard/uid/{dashboard_uid} signed_in_user starDashboardByUID
|
||||
//
|
||||
// Star a dashboard.
|
||||
//
|
||||
// Stars the given Dashboard for the actual user.
|
||||
//
|
||||
// Responses:
|
||||
// 200: okResponse
|
||||
// 400: badRequestError
|
||||
// 401: unauthorisedError
|
||||
// 403: forbiddenError
|
||||
// 500: internalServerError
|
||||
func (hs *HTTPServer) StarDashboardByUID(c *models.ReqContext) response.Response {
|
||||
uid := web.Params(c.Req)[":uid"]
|
||||
if uid == "" {
|
||||
return response.Error(http.StatusBadRequest, "Invalid dashboard UID", nil)
|
||||
}
|
||||
dash, rsp := hs.getDashboardHelper(c.Req.Context(), c.OrgID, 0, uid)
|
||||
|
||||
if rsp != nil {
|
||||
return rsp
|
||||
}
|
||||
|
||||
cmd := star.StarDashboardCommand{UserID: c.UserID, DashboardID: dash.ID}
|
||||
|
||||
if err := hs.starService.Add(c.Req.Context(), &cmd); err != nil {
|
||||
return response.Error(http.StatusInternalServerError, "Failed to star dashboard", err)
|
||||
}
|
||||
|
||||
return response.Success("Dashboard starred!")
|
||||
}
|
||||
|
||||
// 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.
|
||||
//
|
||||
// Deprecated: true
|
||||
//
|
||||
// Please refer to the [new](#/signed_in_user/unstarDashboardByUID) API instead
|
||||
//
|
||||
// Responses:
|
||||
// 200: okResponse
|
||||
// 400: badRequestError
|
||||
// 401: unauthorisedError
|
||||
// 403: forbiddenError
|
||||
// 500: internalServerError
|
||||
func (hs *HTTPServer) UnstarDashboard(c *models.ReqContext) response.Response {
|
||||
id, err := strconv.ParseInt(web.Params(c.Req)[":id"], 10, 64)
|
||||
if err != nil {
|
||||
return response.Error(http.StatusBadRequest, "Invalid dashboard ID", nil)
|
||||
}
|
||||
|
||||
cmd := star.UnstarDashboardCommand{UserID: c.UserID, DashboardID: id}
|
||||
if cmd.DashboardID <= 0 {
|
||||
return response.Error(400, "Missing dashboard id", nil)
|
||||
}
|
||||
|
||||
if err := hs.starService.Delete(c.Req.Context(), &cmd); err != nil {
|
||||
return response.Error(http.StatusInternalServerError, "Failed to unstar dashboard", err)
|
||||
}
|
||||
|
||||
return response.Success("Dashboard unstarred")
|
||||
}
|
||||
|
||||
// swagger:route DELETE /user/stars/dashboard/uid/{dashboard_uid} signed_in_user unstarDashboardByUID
|
||||
//
|
||||
// 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
|
||||
func (hs *HTTPServer) UnstarDashboardByUID(c *models.ReqContext) response.Response {
|
||||
uid := web.Params(c.Req)[":uid"]
|
||||
if uid == "" {
|
||||
return response.Error(http.StatusBadRequest, "Invalid dashboard UID", nil)
|
||||
}
|
||||
dash, rsp := hs.getDashboardHelper(c.Req.Context(), c.OrgID, 0, uid)
|
||||
if rsp != nil {
|
||||
return rsp
|
||||
}
|
||||
|
||||
cmd := star.UnstarDashboardCommand{UserID: c.UserID, DashboardID: dash.ID}
|
||||
|
||||
if err := hs.starService.Delete(c.Req.Context(), &cmd); err != nil {
|
||||
return response.Error(http.StatusInternalServerError, "Failed to unstar dashboard", err)
|
||||
}
|
||||
|
||||
return response.Success("Dashboard unstarred")
|
||||
}
|
||||
|
||||
// swagger:parameters starDashboard
|
||||
type StarDashboardParams struct {
|
||||
// in:path
|
||||
// required:true
|
||||
DashboardID string `json:"dashboard_id"`
|
||||
}
|
||||
|
||||
// swagger:parameters starDashboardByUID
|
||||
type StarDashboardByUIDParams struct {
|
||||
// in:path
|
||||
// required:true
|
||||
DashboardUID string `json:"dashboard_uid"`
|
||||
}
|
||||
|
||||
// swagger:parameters unstarDashboard
|
||||
type UnstarDashboardParams struct {
|
||||
// in:path
|
||||
// required:true
|
||||
DashboardID string `json:"dashboard_id"`
|
||||
}
|
||||
|
||||
// swagger:parameters unstarDashboardByUID
|
||||
type UnstarDashboardByUIDParams struct {
|
||||
// in:path
|
||||
// required:true
|
||||
DashboardUID string `json:"dashboard_uid"`
|
||||
}
|
||||
Reference in New Issue
Block a user