Folders: Add pagination to list (#102334)
This commit is contained in:
@@ -498,7 +498,11 @@ func (ss *FolderStoreImpl) GetFolders(ctx context.Context, q folder.GetFoldersFr
|
||||
}
|
||||
|
||||
if len(q.AncestorUIDs) == 0 {
|
||||
if q.OrderByTitle {
|
||||
if q.Limit > 0 {
|
||||
s.WriteString(` ORDER BY f0.title ASC`)
|
||||
s.WriteString(` LIMIT ? OFFSET ?`)
|
||||
args = append(args, q.Limit, (q.Page-1)*q.Limit)
|
||||
} else if q.OrderByTitle {
|
||||
s.WriteString(` ORDER BY f0.title ASC`)
|
||||
}
|
||||
|
||||
|
||||
@@ -878,7 +878,7 @@ func TestIntegrationGetFolders(t *testing.T) {
|
||||
for i := 0; i < foldersNum; i++ {
|
||||
uid := util.GenerateShortUID()
|
||||
f, err := folderStore.Create(context.Background(), folder.CreateFolderCommand{
|
||||
Title: folderTitle,
|
||||
Title: folderTitle + fmt.Sprintf("-%d", i),
|
||||
Description: folderDsc,
|
||||
OrgID: orgID,
|
||||
UID: uid,
|
||||
@@ -982,6 +982,23 @@ func TestIntegrationGetFolders(t *testing.T) {
|
||||
assert.NotEmpty(t, actualFolder.Updated)
|
||||
assert.NotEmpty(t, actualFolder.URL)
|
||||
})
|
||||
|
||||
t.Run("get folders with limit and page should work as expected", func(t *testing.T) {
|
||||
q := folder.NewGetFoldersQuery(folder.GetFoldersQuery{
|
||||
OrgID: orgID,
|
||||
UIDs: uids,
|
||||
Limit: 3,
|
||||
Page: 2,
|
||||
})
|
||||
|
||||
actualFolders, err := folderStore.GetFolders(context.Background(), q)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 3, len(actualFolders))
|
||||
|
||||
for i, actualFolder := range actualFolders {
|
||||
assert.Equal(t, fmt.Sprintf("folder1-%d", i+3), actualFolder.Title)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func CreateOrg(t *testing.T, db db.DB, cfg *setting.Cfg) int64 {
|
||||
|
||||
@@ -13,6 +13,7 @@ type FakeService struct {
|
||||
ExpectedHitList model.HitList
|
||||
ExpectedError error
|
||||
ExpectedDescendantCounts map[string]int64
|
||||
LastQuery folder.GetFoldersQuery
|
||||
}
|
||||
|
||||
func NewFakeService() *FakeService {
|
||||
@@ -90,5 +91,6 @@ func (s *FakeService) SearchFolders(ctx context.Context, q folder.SearchFoldersQ
|
||||
}
|
||||
|
||||
func (s *FakeService) GetFoldersLegacy(ctx context.Context, q folder.GetFoldersQuery) ([]*folder.Folder, error) {
|
||||
s.LastQuery = q
|
||||
return s.ExpectedFolders, s.ExpectedError
|
||||
}
|
||||
|
||||
@@ -175,6 +175,10 @@ type GetFoldersQuery struct {
|
||||
WithFullpathUIDs bool
|
||||
BatchSize uint64
|
||||
|
||||
// Pagination options
|
||||
Limit int64
|
||||
Page int64
|
||||
|
||||
// OrderByTitle is used to sort the folders by title
|
||||
// Set to true when ordering is meaningful (used for listing folders)
|
||||
// otherwise better to keep it false since ordering can have a performance impact
|
||||
|
||||
Reference in New Issue
Block a user