mirror of
https://github.com/go-gitea/gitea
synced 2024-11-17 15:31:06 +01:00
refactor for searching user (#1038)
* refactor for searching user * fix like bug * better format for builder cond
This commit is contained in:
parent
8894f856de
commit
fc4f7e82f9
1 changed files with 14 additions and 12 deletions
|
@ -23,6 +23,7 @@ import (
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
|
||||||
"github.com/Unknwon/com"
|
"github.com/Unknwon/com"
|
||||||
|
"github.com/go-xorm/builder"
|
||||||
"github.com/go-xorm/xorm"
|
"github.com/go-xorm/xorm"
|
||||||
"github.com/nfnt/resize"
|
"github.com/nfnt/resize"
|
||||||
"golang.org/x/crypto/pbkdf2"
|
"golang.org/x/crypto/pbkdf2"
|
||||||
|
@ -1235,27 +1236,28 @@ func SearchUserByName(opts *SearchUserOptions) (users []*User, _ int64, _ error)
|
||||||
opts.Page = 1
|
opts.Page = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
searchQuery := "%" + opts.Keyword + "%"
|
|
||||||
users = make([]*User, 0, opts.PageSize)
|
users = make([]*User, 0, opts.PageSize)
|
||||||
// Append conditions
|
|
||||||
sess := x.
|
|
||||||
Where("LOWER(lower_name) LIKE ?", searchQuery).
|
|
||||||
Or("LOWER(full_name) LIKE ?", searchQuery).
|
|
||||||
And("type = ?", opts.Type)
|
|
||||||
|
|
||||||
var countSess xorm.Session
|
// Append conditions
|
||||||
countSess = *sess
|
cond := builder.And(
|
||||||
count, err := countSess.Count(new(User))
|
builder.Eq{"type": opts.Type},
|
||||||
|
builder.Or(
|
||||||
|
builder.Like{"lower_name", opts.Keyword},
|
||||||
|
builder.Like{"LOWER(full_name)", opts.Keyword},
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
count, err := x.Where(cond).Count(new(User))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, fmt.Errorf("Count: %v", err)
|
return nil, 0, fmt.Errorf("Count: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sess := x.Where(cond).
|
||||||
|
Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
|
||||||
if len(opts.OrderBy) > 0 {
|
if len(opts.OrderBy) > 0 {
|
||||||
sess.OrderBy(opts.OrderBy)
|
sess.OrderBy(opts.OrderBy)
|
||||||
}
|
}
|
||||||
return users, count, sess.
|
return users, count, sess.Find(&users)
|
||||||
Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
|
|
||||||
Find(&users)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ___________ .__ .__
|
// ___________ .__ .__
|
||||||
|
|
Loading…
Reference in a new issue