Enable searching by user ids
continuous-integration/drone/pr Build is failing Details

Signed-off-by: kolaente <k@knt.li>
This commit is contained in:
kolaente 2020-12-21 21:54:54 +01:00
parent 9613d396b3
commit 89a2e00958
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
1 changed files with 30 additions and 10 deletions

View File

@ -17,20 +17,40 @@
package user
import (
"code.vikunja.io/api/pkg/log"
"strconv"
"strings"
)
// ListUsers returns a list with all users, filtered by an optional searchstring
func ListUsers(searchterm string) (users []User, err error) {
func ListUsers(searchterm string) (users []*User, err error) {
vals := strings.Split(searchterm, ",")
ids := []int64{}
for _, val := range vals {
v, err := strconv.ParseInt(val, 10, 64)
if err != nil {
log.Debugf("User search string part '%s' is not a number: %s", val, err)
continue
}
ids = append(ids, v)
}
if len(ids) > 0 {
err = x.
In("id", ids).
Find(&users)
return
}
if searchterm == "" {
err = x.Find(&users)
} else {
err = x.
Where("username LIKE ?", "%"+searchterm+"%").
Find(&users)
return
}
if err != nil {
return []User{}, err
}
return users, nil
err = x.
Where("username LIKE ?", "%"+searchterm+"%").
Find(&users)
return
}