integrat star service into APIs (#49220)

This commit is contained in:
ying-jeanne
2022-05-19 14:32:10 +02:00
committed by GitHub
parent 426324ad3e
commit e2ea064584
11 changed files with 44 additions and 167 deletions
+9 -6
View File
@@ -5,19 +5,21 @@ import (
"sort"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/services/star"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/models"
)
func ProvideService(cfg *setting.Cfg, sqlstore *sqlstore.SQLStore) *SearchService {
func ProvideService(cfg *setting.Cfg, sqlstore *sqlstore.SQLStore, starService star.Service) *SearchService {
s := &SearchService{
Cfg: cfg,
sortOptions: map[string]models.SortOption{
SortAlphaAsc.Name: SortAlphaAsc,
SortAlphaDesc.Name: SortAlphaDesc,
},
sqlstore: sqlstore,
sqlstore: sqlstore,
starService: starService,
}
return s
}
@@ -48,6 +50,7 @@ type SearchService struct {
Cfg *setting.Cfg
sortOptions map[string]models.SortOption
sqlstore sqlstore.Store
starService star.Service
}
func (s *SearchService) SearchHandler(ctx context.Context, query *Query) error {
@@ -100,15 +103,15 @@ func sortedHits(unsorted models.HitList) models.HitList {
}
func (s *SearchService) setStarredDashboards(ctx context.Context, userID int64, hits []*models.Hit) error {
query := models.GetUserStarsQuery{
UserId: userID,
query := star.GetUserStarsQuery{
UserID: userID,
}
err := s.sqlstore.GetUserStars(ctx, &query)
res, err := s.starService.GetByUser(ctx, &query)
if err != nil {
return err
}
iuserstars := query.Result
iuserstars := res.UserStars
for _, dashboard := range hits {
if _, ok := iuserstars[dashboard.ID]; ok {
dashboard.IsStarred = true
+6 -2
View File
@@ -6,11 +6,14 @@ import (
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/sqlstore/mockstore"
"github.com/grafana/grafana/pkg/services/star"
"github.com/grafana/grafana/pkg/services/star/startest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSearch_SortedResults(t *testing.T) {
ss := startest.NewStarServiceFake()
ms := mockstore.NewSQLStoreMock()
ms.ExpectedPersistedDashboards = models.HitList{
&models.Hit{ID: 16, Title: "CCAA", Type: "dash-db", Tags: []string{"BB", "AA"}},
@@ -20,9 +23,10 @@ func TestSearch_SortedResults(t *testing.T) {
&models.Hit{ID: 17, Title: "FOLDER", Type: "dash-folder"},
}
ms.ExpectedSignedInUser = &models.SignedInUser{IsGrafanaAdmin: true}
ms.ExpectedUserStars = map[int64]bool{10: true, 12: true}
ss.ExpectedUserStars = &star.GetUserStarsResult{UserStars: map[int64]bool{10: true, 12: true}}
svc := &SearchService{
sqlstore: ms,
sqlstore: ms,
starService: ss,
}
query := &Query{