[#1028] added case insensitive collection name lookup

This commit is contained in:
Gani Georgiev 2022-11-15 00:54:29 +02:00
parent 77d295730e
commit 9322b13d15
2 changed files with 8 additions and 6 deletions

View File

@ -32,15 +32,15 @@ func (dao *Dao) FindCollectionsByType(collectionType string) ([]*models.Collecti
return collections, nil
}
// FindCollectionByNameOrId finds the first collection by its name or id.
// FindCollectionByNameOrId finds a single collection by its name (case insensitive) or id.
func (dao *Dao) FindCollectionByNameOrId(nameOrId string) (*models.Collection, error) {
model := &models.Collection{}
err := dao.CollectionQuery().
AndWhere(dbx.Or(
dbx.HashExp{"id": nameOrId},
dbx.HashExp{"name": nameOrId},
)).
AndWhere(dbx.NewExp("[[id]] = {:id} OR LOWER([[name]])={:name}", dbx.Params{
"id": nameOrId,
"name": strings.ToLower(nameOrId),
})).
Limit(1).
One(model)

View File

@ -3,6 +3,7 @@ package daos_test
import (
"encoding/json"
"errors"
"strings"
"testing"
"github.com/pocketbase/pocketbase/daos"
@ -71,6 +72,7 @@ func TestFindCollectionByNameOrId(t *testing.T) {
{"missing", true},
{"wsmn24bux7wo113", false},
{"demo1", false},
{"DEMO1", false}, // case insensitive check
}
for i, scenario := range scenarios {
@ -81,7 +83,7 @@ func TestFindCollectionByNameOrId(t *testing.T) {
t.Errorf("(%d) Expected hasErr to be %v, got %v (%v)", i, scenario.expectError, hasErr, err)
}
if model != nil && model.Id != scenario.nameOrId && model.Name != scenario.nameOrId {
if model != nil && model.Id != scenario.nameOrId && !strings.EqualFold(model.Name, scenario.nameOrId) {
t.Errorf("(%d) Expected model with identifier %s, got %v", i, scenario.nameOrId, model)
}
}