[API Split] Move star api inside of packages (#61987)

move star api inside of packages
This commit is contained in:
ying-jeanne 2023-01-25 22:58:54 +08:00 committed by GitHub
parent 7c85db5bfa
commit b0b2b72290
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 62 additions and 18 deletions

View File

@ -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))

View File

@ -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")

View File

@ -102,6 +102,7 @@ import (
"github.com/grafana/grafana/pkg/services/shorturls"
"github.com/grafana/grafana/pkg/services/shorturls/shorturlimpl"
"github.com/grafana/grafana/pkg/services/sqlstore"
starApi "github.com/grafana/grafana/pkg/services/star/api"
"github.com/grafana/grafana/pkg/services/star/starimpl"
"github.com/grafana/grafana/pkg/services/store"
entitystoredummy "github.com/grafana/grafana/pkg/services/store/entity/dummy"
@ -289,6 +290,7 @@ var wireSet = wire.NewSet(
publicdashboardsStore.ProvideStore,
wire.Bind(new(publicdashboards.Store), new(*publicdashboardsStore.PublicDashboardStoreImpl)),
publicdashboardsApi.ProvideApi,
starApi.ProvideApi,
userimpl.ProvideService,
orgimpl.ProvideService,
teamimpl.ProvideService,

View File

@ -116,6 +116,7 @@ import (
"github.com/grafana/grafana/pkg/services/shorturls"
"github.com/grafana/grafana/pkg/services/shorturls/shorturlimpl"
"github.com/grafana/grafana/pkg/services/sqlstore"
starApi "github.com/grafana/grafana/pkg/services/star/api"
"github.com/grafana/grafana/pkg/services/star/starimpl"
"github.com/grafana/grafana/pkg/services/stats/statsimpl"
"github.com/grafana/grafana/pkg/services/store"
@ -327,6 +328,7 @@ var wireBasicSet = wire.NewSet(
publicdashboardsStore.ProvideStore,
wire.Bind(new(publicdashboards.Store), new(*publicdashboardsStore.PublicDashboardStoreImpl)),
publicdashboardsApi.ProvideApi,
starApi.ProvideApi,
userimpl.ProvideService,
orgimpl.ProvideService,
statsimpl.ProvideService,

View File

@ -1,6 +1,7 @@
package api
import (
"context"
"net/http"
"strconv"
@ -11,12 +12,45 @@ import (
"github.com/grafana/grafana/pkg/web"
)
func (hs *HTTPServer) GetStars(c *models.ReqContext) response.Response {
type API struct {
starService star.Service
dashboardService dashboards.DashboardService
}
func ProvideApi(
starService star.Service,
dashboardService dashboards.DashboardService,
) *API {
api := &API{
starService: starService,
dashboardService: dashboardService,
}
return api
}
func (api *API) getDashboardHelper(ctx context.Context, orgID int64, id int64, uid string) (*dashboards.Dashboard, response.Response) {
var query dashboards.GetDashboardQuery
if len(uid) > 0 {
query = dashboards.GetDashboardQuery{UID: uid, ID: id, OrgID: orgID}
} else {
query = dashboards.GetDashboardQuery{ID: id, OrgID: orgID}
}
result, err := api.dashboardService.GetDashboard(ctx, &query)
if err != nil {
return nil, response.Error(404, "Dashboard not found", err)
}
return result, nil
}
func (api *API) GetStars(c *models.ReqContext) response.Response {
query := star.GetUserStarsQuery{
UserID: c.SignedInUser.UserID,
}
iuserstars, err := hs.starService.GetByUser(c.Req.Context(), &query)
iuserstars, err := api.starService.GetByUser(c.Req.Context(), &query)
if err != nil {
return response.Error(500, "Failed to get user stars", err)
}
@ -27,7 +61,7 @@ func (hs *HTTPServer) GetStars(c *models.ReqContext) response.Response {
ID: dashboardId,
OrgID: c.OrgID,
}
queryResult, err := hs.DashboardService.GetDashboard(c.Req.Context(), query)
queryResult, err := api.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 {
@ -51,7 +85,7 @@ func (hs *HTTPServer) GetStars(c *models.ReqContext) response.Response {
// 401: unauthorisedError
// 403: forbiddenError
// 500: internalServerError
func (hs *HTTPServer) StarDashboard(c *models.ReqContext) response.Response {
func (api *API) 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)
@ -62,7 +96,7 @@ func (hs *HTTPServer) StarDashboard(c *models.ReqContext) response.Response {
return response.Error(400, "Missing dashboard id", nil)
}
if err := hs.starService.Add(c.Req.Context(), &cmd); err != nil {
if err := api.starService.Add(c.Req.Context(), &cmd); err != nil {
return response.Error(http.StatusInternalServerError, "Failed to star dashboard", err)
}
@ -81,12 +115,12 @@ func (hs *HTTPServer) StarDashboard(c *models.ReqContext) response.Response {
// 401: unauthorisedError
// 403: forbiddenError
// 500: internalServerError
func (hs *HTTPServer) StarDashboardByUID(c *models.ReqContext) response.Response {
func (api *API) 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)
dash, rsp := api.getDashboardHelper(c.Req.Context(), c.OrgID, 0, uid)
if rsp != nil {
return rsp
@ -94,7 +128,7 @@ func (hs *HTTPServer) StarDashboardByUID(c *models.ReqContext) response.Response
cmd := star.StarDashboardCommand{UserID: c.UserID, DashboardID: dash.ID}
if err := hs.starService.Add(c.Req.Context(), &cmd); err != nil {
if err := api.starService.Add(c.Req.Context(), &cmd); err != nil {
return response.Error(http.StatusInternalServerError, "Failed to star dashboard", err)
}
@ -117,7 +151,7 @@ func (hs *HTTPServer) StarDashboardByUID(c *models.ReqContext) response.Response
// 401: unauthorisedError
// 403: forbiddenError
// 500: internalServerError
func (hs *HTTPServer) UnstarDashboard(c *models.ReqContext) response.Response {
func (api *API) 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)
@ -128,7 +162,7 @@ func (hs *HTTPServer) UnstarDashboard(c *models.ReqContext) response.Response {
return response.Error(400, "Missing dashboard id", nil)
}
if err := hs.starService.Delete(c.Req.Context(), &cmd); err != nil {
if err := api.starService.Delete(c.Req.Context(), &cmd); err != nil {
return response.Error(http.StatusInternalServerError, "Failed to unstar dashboard", err)
}
@ -147,19 +181,19 @@ func (hs *HTTPServer) UnstarDashboard(c *models.ReqContext) response.Response {
// 401: unauthorisedError
// 403: forbiddenError
// 500: internalServerError
func (hs *HTTPServer) UnstarDashboardByUID(c *models.ReqContext) response.Response {
func (api *API) 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)
dash, rsp := api.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 {
if err := api.starService.Delete(c.Req.Context(), &cmd); err != nil {
return response.Error(http.StatusInternalServerError, "Failed to unstar dashboard", err)
}