Preferences: Support Create+Update+Delete over apiserver (#111715)
This commit is contained in:
@@ -74,6 +74,12 @@ type SavePreferenceCommand struct {
|
||||
Navbar *NavbarPreference `json:"navbar,omitempty"`
|
||||
}
|
||||
|
||||
// One (and only one) of the values must be non-zero
|
||||
type DeleteCommand struct {
|
||||
OrgID int64
|
||||
UserID int64
|
||||
TeamID int64
|
||||
}
|
||||
type PatchPreferenceCommand struct {
|
||||
UserID int64
|
||||
OrgID int64
|
||||
|
||||
@@ -10,5 +10,5 @@ type Service interface {
|
||||
Save(context.Context, *SavePreferenceCommand) error
|
||||
Patch(context.Context, *PatchPreferenceCommand) error
|
||||
GetDefaults() *Preference
|
||||
DeleteByUser(context.Context, int64) error
|
||||
Delete(context.Context, *DeleteCommand) error
|
||||
}
|
||||
|
||||
@@ -121,6 +121,6 @@ func (s *inmemStore) Update(ctx context.Context, preference *pref.Preference) er
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *inmemStore) DeleteByUser(ctx context.Context, userID int64) error {
|
||||
func (s *inmemStore) Delete(context.Context, *pref.DeleteCommand) error {
|
||||
panic("not yet implemented")
|
||||
}
|
||||
|
||||
@@ -272,8 +272,8 @@ func (s *Service) GetDefaults() *pref.Preference {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) DeleteByUser(ctx context.Context, userID int64) error {
|
||||
return s.store.DeleteByUser(ctx, userID)
|
||||
func (s *Service) Delete(ctx context.Context, cmd *pref.DeleteCommand) error {
|
||||
return s.store.Delete(ctx, cmd)
|
||||
}
|
||||
|
||||
func parseCookiePreferences(prefs []pref.CookieType) (map[string]struct{}, error) {
|
||||
|
||||
@@ -12,5 +12,5 @@ type store interface {
|
||||
// Insert adds a new preference and returns its sequential ID
|
||||
Insert(context.Context, *pref.Preference) (int64, error)
|
||||
Update(context.Context, *pref.Preference) error
|
||||
DeleteByUser(context.Context, int64) error
|
||||
Delete(context.Context, *pref.DeleteCommand) error
|
||||
}
|
||||
|
||||
@@ -185,9 +185,10 @@ func testIntegrationPreferencesDataAccess(t *testing.T, fn getStore) {
|
||||
require.NoError(t, err)
|
||||
})
|
||||
t.Run("delete preference by user", func(t *testing.T) {
|
||||
err := prefStore.DeleteByUser(context.Background(), user.SignedInUser{}.UserID)
|
||||
userId := int64(1)
|
||||
err := prefStore.Delete(context.Background(), &pref.DeleteCommand{UserID: userId})
|
||||
require.NoError(t, err)
|
||||
query := &pref.Preference{OrgID: 0, UserID: user.SignedInUser{}.UserID, TeamID: 0}
|
||||
query := &pref.Preference{OrgID: 0, UserID: userId, TeamID: 0}
|
||||
_, err = prefStore.Get(context.Background(), query)
|
||||
require.EqualError(t, err, pref.ErrPrefNotFound.Error())
|
||||
})
|
||||
|
||||
@@ -2,6 +2,7 @@ package prefimpl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/db"
|
||||
@@ -80,10 +81,27 @@ func (s *sqlStore) Insert(ctx context.Context, cmd *pref.Preference) (int64, err
|
||||
return ID, err
|
||||
}
|
||||
|
||||
func (s *sqlStore) DeleteByUser(ctx context.Context, userID int64) error {
|
||||
return s.db.WithDbSession(ctx, func(dbSession *db.Session) error {
|
||||
var rawSQL = "DELETE FROM preferences WHERE user_id = ?"
|
||||
_, err := dbSession.Exec(rawSQL, userID)
|
||||
return err
|
||||
})
|
||||
func (s *sqlStore) Delete(ctx context.Context, cmd *pref.DeleteCommand) error {
|
||||
if cmd.UserID > 0 {
|
||||
return s.db.WithDbSession(ctx, func(dbSession *db.Session) error {
|
||||
var rawSQL = "DELETE FROM preferences WHERE user_id = ?"
|
||||
_, err := dbSession.Exec(rawSQL, cmd.UserID)
|
||||
return err
|
||||
})
|
||||
}
|
||||
if cmd.TeamID > 0 {
|
||||
return s.db.WithDbSession(ctx, func(dbSession *db.Session) error {
|
||||
var rawSQL = "DELETE FROM preferences WHERE team_id = ?"
|
||||
_, err := dbSession.Exec(rawSQL, cmd.TeamID)
|
||||
return err
|
||||
})
|
||||
}
|
||||
if cmd.OrgID > 0 {
|
||||
return s.db.WithDbSession(ctx, func(dbSession *db.Session) error {
|
||||
var rawSQL = "DELETE FROM preferences WHERE org_id = ? AND user_id=0 AND team_id=0"
|
||||
_, err := dbSession.Exec(rawSQL, cmd.OrgID)
|
||||
return err
|
||||
})
|
||||
}
|
||||
return fmt.Errorf("expecting one of team, org, user to be non-zero")
|
||||
}
|
||||
|
||||
@@ -35,6 +35,6 @@ func (f *FakePreferenceService) Patch(ctx context.Context, cmd *pref.PatchPrefer
|
||||
return f.ExpectedError
|
||||
}
|
||||
|
||||
func (f *FakePreferenceService) DeleteByUser(context.Context, int64) error {
|
||||
func (f *FakePreferenceService) Delete(context.Context, *pref.DeleteCommand) error {
|
||||
return f.ExpectedError
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user