diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2017-02-25 21:42:20 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-25 21:42:20 +0800 |
commit | fc4f7e82f997c11497ce818d4fa13e561c780ae1 (patch) | |
tree | b045000c8ad73f0ce21577d82c440b0cb473be2d /models | |
parent | 8894f856de70c5c4080f39f05ff0283971faf856 (diff) | |
download | gitea-fc4f7e82f997c11497ce818d4fa13e561c780ae1.tar.gz gitea-fc4f7e82f997c11497ce818d4fa13e561c780ae1.zip |
refactor for searching user (#1038)
* refactor for searching user
* fix like bug
* better format for builder cond
Diffstat (limited to 'models')
-rw-r--r-- | models/user.go | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/models/user.go b/models/user.go index d0f711448d..55cffe1952 100644 --- a/models/user.go +++ b/models/user.go @@ -23,6 +23,7 @@ import ( "unicode/utf8" "github.com/Unknwon/com" + "github.com/go-xorm/builder" "github.com/go-xorm/xorm" "github.com/nfnt/resize" "golang.org/x/crypto/pbkdf2" @@ -1235,27 +1236,28 @@ func SearchUserByName(opts *SearchUserOptions) (users []*User, _ int64, _ error) opts.Page = 1 } - searchQuery := "%" + opts.Keyword + "%" 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) + cond := builder.And( + builder.Eq{"type": opts.Type}, + builder.Or( + builder.Like{"lower_name", opts.Keyword}, + builder.Like{"LOWER(full_name)", opts.Keyword}, + ), + ) - var countSess xorm.Session - countSess = *sess - count, err := countSess.Count(new(User)) + count, err := x.Where(cond).Count(new(User)) if err != nil { 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 { sess.OrderBy(opts.OrderBy) } - return users, count, sess. - Limit(opts.PageSize, (opts.Page-1)*opts.PageSize). - Find(&users) + return users, count, sess.Find(&users) } // ___________ .__ .__ |