mirror of
https://github.com/grafana/grafana.git
synced 2025-02-20 11:48:34 -06:00
Chore: Remove bus from signup and playlist api (#44485)
* Remove bus from signup and playlist api * Remove bus from playlist play
This commit is contained in:
parent
28c51cde3a
commit
82892331c1
@ -116,7 +116,7 @@ func (hs *HTTPServer) registerRoutes() {
|
||||
r.Get("/verify", hs.Index)
|
||||
r.Get("/signup", hs.Index)
|
||||
r.Get("/api/user/signup/options", routing.Wrap(GetSignUpOptions))
|
||||
r.Post("/api/user/signup", quota("user"), routing.Wrap(SignUp))
|
||||
r.Post("/api/user/signup", quota("user"), routing.Wrap(hs.SignUp))
|
||||
r.Post("/api/user/signup/step2", routing.Wrap(hs.SignUpStep2))
|
||||
|
||||
// invited
|
||||
@ -363,13 +363,13 @@ func (hs *HTTPServer) registerRoutes() {
|
||||
|
||||
// Playlist
|
||||
apiRoute.Group("/playlists", func(playlistRoute routing.RouteRegister) {
|
||||
playlistRoute.Get("/", routing.Wrap(SearchPlaylists))
|
||||
playlistRoute.Get("/:id", ValidateOrgPlaylist, routing.Wrap(GetPlaylist))
|
||||
playlistRoute.Get("/:id/items", ValidateOrgPlaylist, routing.Wrap(GetPlaylistItems))
|
||||
playlistRoute.Get("/:id/dashboards", ValidateOrgPlaylist, routing.Wrap(GetPlaylistDashboards))
|
||||
playlistRoute.Delete("/:id", reqEditorRole, ValidateOrgPlaylist, routing.Wrap(DeletePlaylist))
|
||||
playlistRoute.Put("/:id", reqEditorRole, ValidateOrgPlaylist, routing.Wrap(UpdatePlaylist))
|
||||
playlistRoute.Post("/", reqEditorRole, routing.Wrap(CreatePlaylist))
|
||||
playlistRoute.Get("/", routing.Wrap(hs.SearchPlaylists))
|
||||
playlistRoute.Get("/:id", hs.ValidateOrgPlaylist, routing.Wrap(hs.GetPlaylist))
|
||||
playlistRoute.Get("/:id/items", hs.ValidateOrgPlaylist, routing.Wrap(hs.GetPlaylistItems))
|
||||
playlistRoute.Get("/:id/dashboards", hs.ValidateOrgPlaylist, routing.Wrap(hs.GetPlaylistDashboards))
|
||||
playlistRoute.Delete("/:id", reqEditorRole, hs.ValidateOrgPlaylist, routing.Wrap(hs.DeletePlaylist))
|
||||
playlistRoute.Put("/:id", reqEditorRole, hs.ValidateOrgPlaylist, routing.Wrap(hs.UpdatePlaylist))
|
||||
playlistRoute.Post("/", reqEditorRole, routing.Wrap(hs.CreatePlaylist))
|
||||
})
|
||||
|
||||
// Search
|
||||
|
@ -6,19 +6,18 @@ import (
|
||||
"strconv"
|
||||
|
||||
"github.com/grafana/grafana/pkg/api/response"
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/web"
|
||||
)
|
||||
|
||||
func ValidateOrgPlaylist(c *models.ReqContext) {
|
||||
func (hs *HTTPServer) ValidateOrgPlaylist(c *models.ReqContext) {
|
||||
id, err := strconv.ParseInt(web.Params(c.Req)[":id"], 10, 64)
|
||||
if err != nil {
|
||||
c.JsonApiErr(http.StatusBadRequest, "id is invalid", nil)
|
||||
return
|
||||
}
|
||||
query := models.GetPlaylistByIdQuery{Id: id}
|
||||
err = bus.Dispatch(c.Req.Context(), &query)
|
||||
err = hs.SQLStore.GetPlaylist(c.Req.Context(), &query)
|
||||
|
||||
if err != nil {
|
||||
c.JsonApiErr(404, "Playlist not found", err)
|
||||
@ -36,7 +35,7 @@ func ValidateOrgPlaylist(c *models.ReqContext) {
|
||||
}
|
||||
}
|
||||
|
||||
func SearchPlaylists(c *models.ReqContext) response.Response {
|
||||
func (hs *HTTPServer) SearchPlaylists(c *models.ReqContext) response.Response {
|
||||
query := c.Query("query")
|
||||
limit := c.QueryInt("limit")
|
||||
|
||||
@ -50,7 +49,7 @@ func SearchPlaylists(c *models.ReqContext) response.Response {
|
||||
OrgId: c.OrgId,
|
||||
}
|
||||
|
||||
err := bus.Dispatch(c.Req.Context(), &searchQuery)
|
||||
err := hs.SQLStore.SearchPlaylists(c.Req.Context(), &searchQuery)
|
||||
if err != nil {
|
||||
return response.Error(500, "Search failed", err)
|
||||
}
|
||||
@ -58,18 +57,18 @@ func SearchPlaylists(c *models.ReqContext) response.Response {
|
||||
return response.JSON(200, searchQuery.Result)
|
||||
}
|
||||
|
||||
func GetPlaylist(c *models.ReqContext) response.Response {
|
||||
func (hs *HTTPServer) GetPlaylist(c *models.ReqContext) response.Response {
|
||||
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.GetPlaylistByIdQuery{Id: id}
|
||||
|
||||
if err := bus.Dispatch(c.Req.Context(), &cmd); err != nil {
|
||||
if err := hs.SQLStore.GetPlaylist(c.Req.Context(), &cmd); err != nil {
|
||||
return response.Error(500, "Playlist not found", err)
|
||||
}
|
||||
|
||||
playlistDTOs, _ := LoadPlaylistItemDTOs(c.Req.Context(), id)
|
||||
playlistDTOs, _ := hs.LoadPlaylistItemDTOs(c.Req.Context(), id)
|
||||
|
||||
dto := &models.PlaylistDTO{
|
||||
Id: cmd.Result.Id,
|
||||
@ -82,8 +81,8 @@ func GetPlaylist(c *models.ReqContext) response.Response {
|
||||
return response.JSON(200, dto)
|
||||
}
|
||||
|
||||
func LoadPlaylistItemDTOs(ctx context.Context, id int64) ([]models.PlaylistItemDTO, error) {
|
||||
playlistitems, err := LoadPlaylistItems(ctx, id)
|
||||
func (hs *HTTPServer) LoadPlaylistItemDTOs(ctx context.Context, id int64) ([]models.PlaylistItemDTO, error) {
|
||||
playlistitems, err := hs.LoadPlaylistItems(ctx, id)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -105,22 +104,22 @@ func LoadPlaylistItemDTOs(ctx context.Context, id int64) ([]models.PlaylistItemD
|
||||
return playlistDTOs, nil
|
||||
}
|
||||
|
||||
func LoadPlaylistItems(ctx context.Context, id int64) ([]models.PlaylistItem, error) {
|
||||
func (hs *HTTPServer) LoadPlaylistItems(ctx context.Context, id int64) ([]models.PlaylistItem, error) {
|
||||
itemQuery := models.GetPlaylistItemsByIdQuery{PlaylistId: id}
|
||||
if err := bus.Dispatch(ctx, &itemQuery); err != nil {
|
||||
if err := hs.SQLStore.GetPlaylistItem(ctx, &itemQuery); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return *itemQuery.Result, nil
|
||||
}
|
||||
|
||||
func GetPlaylistItems(c *models.ReqContext) response.Response {
|
||||
func (hs *HTTPServer) GetPlaylistItems(c *models.ReqContext) response.Response {
|
||||
id, err := strconv.ParseInt(web.Params(c.Req)[":id"], 10, 64)
|
||||
if err != nil {
|
||||
return response.Error(http.StatusBadRequest, "id is invalid", err)
|
||||
}
|
||||
|
||||
playlistDTOs, err := LoadPlaylistItemDTOs(c.Req.Context(), id)
|
||||
playlistDTOs, err := hs.LoadPlaylistItemDTOs(c.Req.Context(), id)
|
||||
|
||||
if err != nil {
|
||||
return response.Error(500, "Could not load playlist items", err)
|
||||
@ -129,13 +128,13 @@ func GetPlaylistItems(c *models.ReqContext) response.Response {
|
||||
return response.JSON(200, playlistDTOs)
|
||||
}
|
||||
|
||||
func GetPlaylistDashboards(c *models.ReqContext) response.Response {
|
||||
func (hs *HTTPServer) GetPlaylistDashboards(c *models.ReqContext) response.Response {
|
||||
playlistID, err := strconv.ParseInt(web.Params(c.Req)[":id"], 10, 64)
|
||||
if err != nil {
|
||||
return response.Error(http.StatusBadRequest, "id is invalid", err)
|
||||
}
|
||||
|
||||
playlists, err := LoadPlaylistDashboards(c.Req.Context(), c.OrgId, c.SignedInUser, playlistID)
|
||||
playlists, err := hs.LoadPlaylistDashboards(c.Req.Context(), c.OrgId, c.SignedInUser, playlistID)
|
||||
if err != nil {
|
||||
return response.Error(500, "Could not load dashboards", err)
|
||||
}
|
||||
@ -143,35 +142,35 @@ func GetPlaylistDashboards(c *models.ReqContext) response.Response {
|
||||
return response.JSON(200, playlists)
|
||||
}
|
||||
|
||||
func DeletePlaylist(c *models.ReqContext) response.Response {
|
||||
func (hs *HTTPServer) DeletePlaylist(c *models.ReqContext) response.Response {
|
||||
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.DeletePlaylistCommand{Id: id, OrgId: c.OrgId}
|
||||
if err := bus.Dispatch(c.Req.Context(), &cmd); err != nil {
|
||||
if err := hs.SQLStore.DeletePlaylist(c.Req.Context(), &cmd); err != nil {
|
||||
return response.Error(500, "Failed to delete playlist", err)
|
||||
}
|
||||
|
||||
return response.JSON(200, "")
|
||||
}
|
||||
|
||||
func CreatePlaylist(c *models.ReqContext) response.Response {
|
||||
func (hs *HTTPServer) CreatePlaylist(c *models.ReqContext) response.Response {
|
||||
cmd := models.CreatePlaylistCommand{}
|
||||
if err := web.Bind(c.Req, &cmd); err != nil {
|
||||
return response.Error(http.StatusBadRequest, "bad request data", err)
|
||||
}
|
||||
cmd.OrgId = c.OrgId
|
||||
|
||||
if err := bus.Dispatch(c.Req.Context(), &cmd); err != nil {
|
||||
if err := hs.SQLStore.CreatePlaylist(c.Req.Context(), &cmd); err != nil {
|
||||
return response.Error(500, "Failed to create playlist", err)
|
||||
}
|
||||
|
||||
return response.JSON(200, cmd.Result)
|
||||
}
|
||||
|
||||
func UpdatePlaylist(c *models.ReqContext) response.Response {
|
||||
func (hs *HTTPServer) UpdatePlaylist(c *models.ReqContext) response.Response {
|
||||
cmd := models.UpdatePlaylistCommand{}
|
||||
if err := web.Bind(c.Req, &cmd); err != nil {
|
||||
return response.Error(http.StatusBadRequest, "bad request data", err)
|
||||
@ -183,11 +182,11 @@ func UpdatePlaylist(c *models.ReqContext) response.Response {
|
||||
return response.Error(http.StatusBadRequest, "id is invalid", err)
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(c.Req.Context(), &cmd); err != nil {
|
||||
if err := hs.SQLStore.UpdatePlaylist(c.Req.Context(), &cmd); err != nil {
|
||||
return response.Error(500, "Failed to save playlist", err)
|
||||
}
|
||||
|
||||
playlistDTOs, err := LoadPlaylistItemDTOs(c.Req.Context(), cmd.Id)
|
||||
playlistDTOs, err := hs.LoadPlaylistItemDTOs(c.Req.Context(), cmd.Id)
|
||||
if err != nil {
|
||||
return response.Error(500, "Failed to save playlist", err)
|
||||
}
|
||||
|
@ -6,18 +6,17 @@ import (
|
||||
"strconv"
|
||||
|
||||
"github.com/grafana/grafana/pkg/api/dtos"
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
_ "github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/search"
|
||||
)
|
||||
|
||||
func populateDashboardsByID(ctx context.Context, dashboardByIDs []int64, dashboardIDOrder map[int64]int) (dtos.PlaylistDashboardsSlice, error) {
|
||||
func (hs *HTTPServer) populateDashboardsByID(ctx context.Context, dashboardByIDs []int64, dashboardIDOrder map[int64]int) (dtos.PlaylistDashboardsSlice, error) {
|
||||
result := make(dtos.PlaylistDashboardsSlice, 0)
|
||||
|
||||
if len(dashboardByIDs) > 0 {
|
||||
dashboardQuery := models.GetDashboardsQuery{DashboardIds: dashboardByIDs}
|
||||
if err := bus.Dispatch(ctx, &dashboardQuery); err != nil {
|
||||
if err := hs.SQLStore.GetDashboards(ctx, &dashboardQuery); err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
@ -36,7 +35,7 @@ func populateDashboardsByID(ctx context.Context, dashboardByIDs []int64, dashboa
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func populateDashboardsByTag(ctx context.Context, orgID int64, signedInUser *models.SignedInUser, dashboardByTag []string, dashboardTagOrder map[string]int) dtos.PlaylistDashboardsSlice {
|
||||
func (hs *HTTPServer) populateDashboardsByTag(ctx context.Context, orgID int64, signedInUser *models.SignedInUser, dashboardByTag []string, dashboardTagOrder map[string]int) dtos.PlaylistDashboardsSlice {
|
||||
result := make(dtos.PlaylistDashboardsSlice, 0)
|
||||
|
||||
for _, tag := range dashboardByTag {
|
||||
@ -49,7 +48,7 @@ func populateDashboardsByTag(ctx context.Context, orgID int64, signedInUser *mod
|
||||
OrgId: orgID,
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(ctx, &searchQuery); err == nil {
|
||||
if err := hs.SearchService.SearchHandler(ctx, &searchQuery); err == nil {
|
||||
for _, item := range searchQuery.Result {
|
||||
result = append(result, dtos.PlaylistDashboard{
|
||||
Id: item.ID,
|
||||
@ -66,8 +65,8 @@ func populateDashboardsByTag(ctx context.Context, orgID int64, signedInUser *mod
|
||||
return result
|
||||
}
|
||||
|
||||
func LoadPlaylistDashboards(ctx context.Context, orgID int64, signedInUser *models.SignedInUser, playlistID int64) (dtos.PlaylistDashboardsSlice, error) {
|
||||
playlistItems, _ := LoadPlaylistItems(ctx, playlistID)
|
||||
func (hs *HTTPServer) LoadPlaylistDashboards(ctx context.Context, orgID int64, signedInUser *models.SignedInUser, playlistID int64) (dtos.PlaylistDashboardsSlice, error) {
|
||||
playlistItems, _ := hs.LoadPlaylistItems(ctx, playlistID)
|
||||
|
||||
dashboardByIDs := make([]int64, 0)
|
||||
dashboardByTag := make([]string, 0)
|
||||
@ -89,9 +88,9 @@ func LoadPlaylistDashboards(ctx context.Context, orgID int64, signedInUser *mode
|
||||
|
||||
result := make(dtos.PlaylistDashboardsSlice, 0)
|
||||
|
||||
var k, _ = populateDashboardsByID(ctx, dashboardByIDs, dashboardIDOrder)
|
||||
var k, _ = hs.populateDashboardsByID(ctx, dashboardByIDs, dashboardIDOrder)
|
||||
result = append(result, k...)
|
||||
result = append(result, populateDashboardsByTag(ctx, orgID, signedInUser, dashboardByTag, dashboardTagOrder)...)
|
||||
result = append(result, hs.populateDashboardsByTag(ctx, orgID, signedInUser, dashboardByTag, dashboardTagOrder)...)
|
||||
|
||||
sort.Sort(result)
|
||||
return result, nil
|
||||
|
@ -25,7 +25,7 @@ func GetSignUpOptions(c *models.ReqContext) response.Response {
|
||||
}
|
||||
|
||||
// POST /api/user/signup
|
||||
func SignUp(c *models.ReqContext) response.Response {
|
||||
func (hs *HTTPServer) SignUp(c *models.ReqContext) response.Response {
|
||||
form := dtos.SignUpForm{}
|
||||
if err := web.Bind(c.Req, &form); err != nil {
|
||||
return response.Error(http.StatusBadRequest, "bad request data", err)
|
||||
@ -35,7 +35,7 @@ func SignUp(c *models.ReqContext) response.Response {
|
||||
}
|
||||
|
||||
existing := models.GetUserByLoginQuery{LoginOrEmail: form.Email}
|
||||
if err := bus.Dispatch(c.Req.Context(), &existing); err == nil {
|
||||
if err := hs.SQLStore.GetUserByLogin(c.Req.Context(), &existing); err == nil {
|
||||
return response.Error(422, "User with same email address already exists", nil)
|
||||
}
|
||||
|
||||
@ -51,7 +51,7 @@ func SignUp(c *models.ReqContext) response.Response {
|
||||
}
|
||||
cmd.RemoteAddr = c.Req.RemoteAddr
|
||||
|
||||
if err := bus.Dispatch(c.Req.Context(), &cmd); err != nil {
|
||||
if err := hs.SQLStore.CreateTempUser(c.Req.Context(), &cmd); err != nil {
|
||||
return response.Error(500, "Failed to create signup", err)
|
||||
}
|
||||
|
||||
@ -86,7 +86,7 @@ func (hs *HTTPServer) SignUpStep2(c *models.ReqContext) response.Response {
|
||||
|
||||
// verify email
|
||||
if setting.VerifyEmailEnabled {
|
||||
if ok, rsp := verifyUserSignUpEmail(c.Req.Context(), form.Email, form.Code); !ok {
|
||||
if ok, rsp := hs.verifyUserSignUpEmail(c.Req.Context(), form.Email, form.Code); !ok {
|
||||
return rsp
|
||||
}
|
||||
createUserCmd.EmailVerified = true
|
||||
@ -116,7 +116,7 @@ func (hs *HTTPServer) SignUpStep2(c *models.ReqContext) response.Response {
|
||||
|
||||
// check for pending invites
|
||||
invitesQuery := models.GetTempUsersQuery{Email: form.Email, Status: models.TmpUserInvitePending}
|
||||
if err := bus.Dispatch(c.Req.Context(), &invitesQuery); err != nil {
|
||||
if err := hs.SQLStore.GetTempUsersQuery(c.Req.Context(), &invitesQuery); err != nil {
|
||||
return response.Error(500, "Failed to query database for invites", err)
|
||||
}
|
||||
|
||||
@ -138,10 +138,10 @@ func (hs *HTTPServer) SignUpStep2(c *models.ReqContext) response.Response {
|
||||
return response.JSON(200, apiResponse)
|
||||
}
|
||||
|
||||
func verifyUserSignUpEmail(ctx context.Context, email string, code string) (bool, response.Response) {
|
||||
func (hs *HTTPServer) verifyUserSignUpEmail(ctx context.Context, email string, code string) (bool, response.Response) {
|
||||
query := models.GetTempUserByCodeQuery{Code: code}
|
||||
|
||||
if err := bus.Dispatch(ctx, &query); err != nil {
|
||||
if err := hs.SQLStore.GetTempUserByCode(ctx, &query); err != nil {
|
||||
if errors.Is(err, models.ErrTempUserNotFound) {
|
||||
return false, response.Error(404, "Invalid email verification code", nil)
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ func ProvideService(cfg *setting.Cfg, bus bus.Bus) *SearchService {
|
||||
SortAlphaDesc.Name: SortAlphaDesc,
|
||||
},
|
||||
}
|
||||
s.Bus.AddHandler(s.searchHandler)
|
||||
s.Bus.AddHandler(s.SearchHandler)
|
||||
return s
|
||||
}
|
||||
|
||||
@ -66,7 +66,7 @@ type SearchService struct {
|
||||
sortOptions map[string]SortOption
|
||||
}
|
||||
|
||||
func (s *SearchService) searchHandler(ctx context.Context, query *Query) error {
|
||||
func (s *SearchService) SearchHandler(ctx context.Context, query *Query) error {
|
||||
dashboardQuery := FindPersistedDashboardsQuery{
|
||||
Title: query.Title,
|
||||
SignedInUser: query.SignedInUser,
|
||||
|
@ -41,7 +41,7 @@ func TestSearch_SortedResults(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
err := svc.searchHandler(context.Background(), query)
|
||||
err := svc.SearchHandler(context.Background(), query)
|
||||
require.Nil(t, err)
|
||||
|
||||
// Assert results are sorted.
|
||||
|
@ -28,7 +28,6 @@ var shadowSearchCounter = prometheus.NewCounterVec(
|
||||
|
||||
func init() {
|
||||
bus.AddHandler("sql", GetDashboard)
|
||||
bus.AddHandler("sql", GetDashboards)
|
||||
bus.AddHandler("sql", GetDashboardTags)
|
||||
bus.AddHandler("sql", GetDashboardSlugById)
|
||||
bus.AddHandler("sql", GetDashboardsByPluginId)
|
||||
@ -44,6 +43,7 @@ func (ss *SQLStore) addDashboardQueryAndCommandHandlers() {
|
||||
bus.AddHandler("sql", ss.GetDashboardUIDById)
|
||||
bus.AddHandler("sql", ss.SearchDashboards)
|
||||
bus.AddHandler("sql", ss.DeleteDashboard)
|
||||
bus.AddHandler("sql", ss.GetDashboards)
|
||||
}
|
||||
|
||||
var generateNewUid func() string = util.GenerateShortUID
|
||||
@ -509,7 +509,7 @@ func deleteDashboard(cmd *models.DeleteDashboardCommand, sess *DBSession) error
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetDashboards(ctx context.Context, query *models.GetDashboardsQuery) error {
|
||||
func (ss *SQLStore) GetDashboards(ctx context.Context, query *models.GetDashboardsQuery) error {
|
||||
if len(query.DashboardIds) == 0 {
|
||||
return models.ErrCommandValidationFailed
|
||||
}
|
||||
|
@ -75,7 +75,7 @@ func TestDashboardProvisioningTest(t *testing.T) {
|
||||
require.Nil(t, err)
|
||||
|
||||
query := &models.GetDashboardsQuery{DashboardIds: []int64{anotherDash.Id}}
|
||||
err = GetDashboards(context.Background(), query)
|
||||
err = sqlStore.GetDashboards(context.Background(), query)
|
||||
require.Nil(t, err)
|
||||
require.NotNil(t, query.Result)
|
||||
|
||||
@ -83,7 +83,7 @@ func TestDashboardProvisioningTest(t *testing.T) {
|
||||
require.Nil(t, sqlStore.DeleteOrphanedProvisionedDashboards(context.Background(), deleteCmd))
|
||||
|
||||
query = &models.GetDashboardsQuery{DashboardIds: []int64{dash.Id, anotherDash.Id}}
|
||||
err = GetDashboards(context.Background(), query)
|
||||
err = sqlStore.GetDashboards(context.Background(), query)
|
||||
require.Nil(t, err)
|
||||
|
||||
require.Equal(t, 1, len(query.Result))
|
||||
|
Loading…
Reference in New Issue
Block a user