[#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 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) { func (dao *Dao) FindCollectionByNameOrId(nameOrId string) (*models.Collection, error) {
model := &models.Collection{} model := &models.Collection{}
err := dao.CollectionQuery(). err := dao.CollectionQuery().
AndWhere(dbx.Or( AndWhere(dbx.NewExp("[[id]] = {:id} OR LOWER([[name]])={:name}", dbx.Params{
dbx.HashExp{"id": nameOrId}, "id": nameOrId,
dbx.HashExp{"name": nameOrId}, "name": strings.ToLower(nameOrId),
)). })).
Limit(1). Limit(1).
One(model) One(model)

View File

@ -3,6 +3,7 @@ package daos_test
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"strings"
"testing" "testing"
"github.com/pocketbase/pocketbase/daos" "github.com/pocketbase/pocketbase/daos"
@ -71,6 +72,7 @@ func TestFindCollectionByNameOrId(t *testing.T) {
{"missing", true}, {"missing", true},
{"wsmn24bux7wo113", false}, {"wsmn24bux7wo113", false},
{"demo1", false}, {"demo1", false},
{"DEMO1", false}, // case insensitive check
} }
for i, scenario := range scenarios { 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) 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) t.Errorf("(%d) Expected model with identifier %s, got %v", i, scenario.nameOrId, model)
} }
} }