Add implementation for folder store methods (#57700)

* Add implementation for folder store methods

* Add Move implementation

* Add back comment and fix query

* Remove Move from store

* Adjust GetChildren

* Fix errutil error declaration and usage

* Add org id to get children query
This commit is contained in:
idafurjes 2022-10-28 15:35:49 +02:00 committed by GitHub
parent 2cd9cbd359
commit 6b483a8dca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 26 deletions

View File

@ -24,30 +24,50 @@ func ProvideStore(db db.DB, cfg *setting.Cfg, features featuremgmt.FeatureManage
return &sqlStore{db: db, log: log.New("folder-store"), cfg: cfg, fm: features}
}
func (s *sqlStore) Create(ctx context.Context, cmd *folder.CreateFolderCommand) (*folder.Folder, error) {
func (ss *sqlStore) Create(ctx context.Context, cmd *folder.CreateFolderCommand) (*folder.Folder, error) {
panic("not implemented")
}
func (s *sqlStore) Delete(ctx context.Context, uid string, orgID int64) error {
func (ss *sqlStore) Delete(ctx context.Context, uid string, orgID int64) error {
return ss.db.WithDbSession(ctx, func(sess *db.Session) error {
_, err := sess.Exec("DELETE FROM folder WHERE folder_uid=? AND org_id=?", uid, orgID)
return err
})
}
func (ss *sqlStore) Update(ctx context.Context, cmd *folder.UpdateFolderCommand) (*folder.Folder, error) {
err := ss.db.WithDbSession(ctx, func(sess *db.Session) error {
_, err := sess.ID(cmd.Folder.ID).AllCols().Update(cmd.Folder)
return err
})
return cmd.Folder, err
}
func (ss *sqlStore) Get(ctx context.Context, cmd *folder.GetFolderQuery) (*folder.Folder, error) {
var foldr *folder.Folder
err := ss.db.WithDbSession(ctx, func(sess *db.Session) error {
exists, err := sess.Where("uid=? OR id=? OR title=?", cmd.UID, cmd.ID, cmd.Title).Get(foldr)
if err != nil {
return err
}
if !exists {
return folder.ErrFolderNotFound.Errorf("folder not found")
}
return nil
})
return foldr, err
}
func (ss *sqlStore) GetParents(ctx context.Context, cmd *folder.GetParentsQuery) ([]*folder.Folder, error) {
panic("not implemented")
}
func (s *sqlStore) Update(ctx context.Context, cmd *folder.UpdateFolderCommand) (*folder.Folder, error) {
panic("not implemented")
}
func (s *sqlStore) Move(ctx context.Context, cmd *folder.MoveFolderCommand) (*folder.Folder, error) {
panic("not implemented")
}
func (s *sqlStore) Get(ctx context.Context, cmd *folder.GetFolderQuery) (*folder.Folder, error) {
panic("not implemented")
}
func (s *sqlStore) GetParents(ctx context.Context, cmd *folder.GetParentsQuery) ([]*folder.Folder, error) {
panic("not implemented")
}
func (s *sqlStore) GetChildren(ctx context.Context, cmd *folder.GetTreeQuery) ([]*folder.Folder, error) {
panic("not implemented")
func (ss *sqlStore) GetChildren(ctx context.Context, cmd *folder.GetTreeQuery) ([]*folder.Folder, error) {
var folders []*folder.Folder
err := ss.db.WithDbSession(ctx, func(sess *db.Session) error {
err := sess.Where("parent_uid=? AND org_id=?", cmd.UID, cmd.OrgID).Find(folders)
return err
})
return folders, err
}

View File

@ -8,8 +8,6 @@ func TestDelete(t *testing.T) {}
func TestUpdate(t *testing.T) {}
func TestMove(t *testing.T) {}
func TestGet(t *testing.T) {}
func TestGetParent(t *testing.T) {}

View File

@ -6,7 +6,7 @@ import (
"github.com/grafana/grafana/pkg/services/folder"
)
// Store is the interface which a folder store must implement.
// store is the interface which a folder store must implement.
type store interface {
// Create creates a folder and returns the newly-created folder.
Create(ctx context.Context, cmd *folder.CreateFolderCommand) (*folder.Folder, error)
@ -18,9 +18,6 @@ type store interface {
// Use Move to change a dashboard's parent ID.
Update(ctx context.Context, cmd *folder.UpdateFolderCommand) (*folder.Folder, error)
// Move changes the given folder's parent folder uid and applies any necessary permissions changes.
Move(ctx context.Context, cmd *folder.MoveFolderCommand) (*folder.Folder, error)
// Get returns a folder.
Get(ctx context.Context, cmd *folder.GetFolderQuery) (*folder.Folder, error)

View File

@ -2,6 +2,8 @@ package folder
import (
"time"
"github.com/grafana/grafana/pkg/util/errutil"
)
const (
@ -9,6 +11,8 @@ const (
MaxNestedFolderDepth = 8
)
var ErrFolderNotFound = errutil.NewBase(errutil.StatusNotFound, "folder.notFound")
type Folder struct {
ID int64
OrgID int64
@ -87,6 +91,7 @@ type GetParentsQuery struct {
type GetTreeQuery struct {
UID string
OrgID int64
Depth int64
// Pagination options