Nested Folders: More API fixes (#59316)

* Nested Folder: Fix create, use camel case for JSON properties

* Fix get parents if the folder does not exist

* Add store test for get parents
This commit is contained in:
Sofia Papagiannaki
2022-11-24 20:28:53 +02:00
committed by GitHub
parent 5a3f0e8696
commit f5c41ea497
7 changed files with 310 additions and 288 deletions

View File

@@ -3,6 +3,7 @@ package folderimpl
import (
"context"
"errors"
"fmt"
"strings"
"github.com/grafana/grafana/pkg/bus"
@@ -517,7 +518,7 @@ func (s *Service) nestedFolderCreate(ctx context.Context, cmd *folder.CreateFold
func (s *Service) validateParent(ctx context.Context, orgID int64, parentUID string) error {
ancestors, err := s.store.GetParents(ctx, folder.GetParentsQuery{UID: parentUID, OrgID: orgID})
if err != nil {
return err
return fmt.Errorf("failed to get parents: %w", err)
}
if len(ancestors) == folder.MaxNestedFolderDepth {

View File

@@ -201,6 +201,13 @@ func (ss *sqlStore) GetParents(ctx context.Context, q folder.GetParentsQuery) ([
}
return nil, err
}
if len(folders) < 1 {
// the query is expected to return at least the same folder
// if it's empty it means that the folder does not exist
return nil, folder.ErrFolderNotFound
}
return util.Reverse(folders[1:]), nil
}

View File

@@ -397,6 +397,11 @@ func TestIntegrationGetParents(t *testing.T) {
require.NoError(t, err)
})
t.Run("get parents of unknown folder should return an error", func(t *testing.T) {
_, err := folderStore.GetParents(context.Background(), folder.GetParentsQuery{})
require.ErrorIs(t, err, folder.ErrFolderNotFound)
})
t.Run("get parents of 1-st level folder should be empty", func(t *testing.T) {
parents, err := folderStore.GetParents(context.Background(), folder.GetParentsQuery{
UID: f.UID,

View File

@@ -66,7 +66,7 @@ type CreateFolderCommand struct {
OrgID int64 `json:"-"`
Title string `json:"title"`
Description string `json:"description"`
ParentUID string `json:"parent_uid"`
ParentUID string `json:"parentUid"`
SignedInUser *user.SignedInUser `json:"-"`
}
@@ -86,7 +86,7 @@ type UpdateFolderCommand struct {
// to move a folder.
type MoveFolderCommand struct {
UID string `json:"uid"`
NewParentUID string `json:"new_parent_uid"`
NewParentUID string `json:"newParentUid"`
OrgID int64 `json:"-"`
SignedInUser *user.SignedInUser `json:"-"`