mirror of
https://github.com/grafana/grafana.git
synced 2026-08-18 17:15:08 -05:00
integrat star service into APIs (#49220)
This commit is contained in:
@@ -308,24 +308,6 @@ func (m *SQLStoreMock) UpdatePluginSettingVersion(ctx context.Context, cmd *mode
|
||||
return m.ExpectedError
|
||||
}
|
||||
|
||||
func (m *SQLStoreMock) IsStarredByUserCtx(ctx context.Context, query *models.IsStarredByUserQuery) error {
|
||||
query.Result = false
|
||||
return m.ExpectedError
|
||||
}
|
||||
|
||||
func (m *SQLStoreMock) StarDashboard(ctx context.Context, cmd *models.StarDashboardCommand) error {
|
||||
return m.ExpectedError
|
||||
}
|
||||
|
||||
func (m *SQLStoreMock) UnstarDashboard(ctx context.Context, cmd *models.UnstarDashboardCommand) error {
|
||||
return m.ExpectedError
|
||||
}
|
||||
|
||||
func (m *SQLStoreMock) GetUserStars(ctx context.Context, query *models.GetUserStarsQuery) error {
|
||||
query.Result = m.ExpectedUserStars
|
||||
return m.ExpectedError
|
||||
}
|
||||
|
||||
func (m *SQLStoreMock) GetOrgQuotaByTarget(ctx context.Context, query *models.GetOrgQuotaByTargetQuery) error {
|
||||
return m.ExpectedError
|
||||
}
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
)
|
||||
|
||||
func (ss *SQLStore) IsStarredByUserCtx(ctx context.Context, query *models.IsStarredByUserQuery) error {
|
||||
return ss.WithDbSession(ctx, func(sess *DBSession) error {
|
||||
rawSQL := "SELECT 1 from star where user_id=? and dashboard_id=?"
|
||||
results, err := sess.Query(rawSQL, query.UserId, query.DashboardId)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(results) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
query.Result = true
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (ss *SQLStore) StarDashboard(ctx context.Context, cmd *models.StarDashboardCommand) error {
|
||||
if cmd.DashboardId == 0 || cmd.UserId == 0 {
|
||||
return models.ErrCommandValidationFailed
|
||||
}
|
||||
|
||||
return ss.WithTransactionalDbSession(ctx, func(sess *DBSession) error {
|
||||
entity := models.Star{
|
||||
UserId: cmd.UserId,
|
||||
DashboardId: cmd.DashboardId,
|
||||
}
|
||||
|
||||
_, err := sess.Insert(&entity)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func (ss *SQLStore) UnstarDashboard(ctx context.Context, cmd *models.UnstarDashboardCommand) error {
|
||||
if cmd.DashboardId == 0 || cmd.UserId == 0 {
|
||||
return models.ErrCommandValidationFailed
|
||||
}
|
||||
|
||||
return ss.WithTransactionalDbSession(ctx, func(sess *DBSession) error {
|
||||
var rawSQL = "DELETE FROM star WHERE user_id=? and dashboard_id=?"
|
||||
_, err := sess.Exec(rawSQL, cmd.UserId, cmd.DashboardId)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func (ss *SQLStore) GetUserStars(ctx context.Context, query *models.GetUserStarsQuery) error {
|
||||
return ss.WithDbSession(ctx, func(dbSession *DBSession) error {
|
||||
var stars = make([]models.Star, 0)
|
||||
err := dbSession.Where("user_id=?", query.UserId).Find(&stars)
|
||||
|
||||
query.Result = make(map[int64]bool)
|
||||
for _, star := range stars {
|
||||
query.Result[star.DashboardId] = true
|
||||
}
|
||||
|
||||
return err
|
||||
})
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
//go:build integration
|
||||
// +build integration
|
||||
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestUserStarsDataAccess(t *testing.T) {
|
||||
t.Run("Testing User Stars Data Access", func(t *testing.T) {
|
||||
sqlStore := InitTestDB(t)
|
||||
|
||||
t.Run("Given saved star", func(t *testing.T) {
|
||||
cmd := models.StarDashboardCommand{
|
||||
DashboardId: 10,
|
||||
UserId: 12,
|
||||
}
|
||||
|
||||
err := sqlStore.StarDashboard(context.Background(), &cmd)
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Run("IsStarredByUser should return true when starred", func(t *testing.T) {
|
||||
query := models.IsStarredByUserQuery{UserId: 12, DashboardId: 10}
|
||||
err := sqlStore.IsStarredByUserCtx(context.Background(), &query)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.True(t, query.Result)
|
||||
})
|
||||
|
||||
t.Run("IsStarredByUser should return false when not starred", func(t *testing.T) {
|
||||
query := models.IsStarredByUserQuery{UserId: 12, DashboardId: 12}
|
||||
err := sqlStore.IsStarredByUserCtx(context.Background(), &query)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.False(t, query.Result)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -65,10 +65,6 @@ type Store interface {
|
||||
GetPluginSettingById(ctx context.Context, query *models.GetPluginSettingByIdQuery) error
|
||||
UpdatePluginSetting(ctx context.Context, cmd *models.UpdatePluginSettingCmd) error
|
||||
UpdatePluginSettingVersion(ctx context.Context, cmd *models.UpdatePluginSettingVersionCmd) error
|
||||
IsStarredByUserCtx(ctx context.Context, query *models.IsStarredByUserQuery) error
|
||||
StarDashboard(ctx context.Context, cmd *models.StarDashboardCommand) error
|
||||
UnstarDashboard(ctx context.Context, cmd *models.UnstarDashboardCommand) error
|
||||
GetUserStars(ctx context.Context, query *models.GetUserStarsQuery) error
|
||||
GetOrgQuotaByTarget(ctx context.Context, query *models.GetOrgQuotaByTargetQuery) error
|
||||
GetOrgQuotas(ctx context.Context, query *models.GetOrgQuotasQuery) error
|
||||
UpdateOrgQuota(ctx context.Context, cmd *models.UpdateOrgQuotaCmd) error
|
||||
|
||||
Reference in New Issue
Block a user