diff options
author | Unknwon <u@gogs.io> | 2016-03-11 15:33:12 -0500 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2016-03-11 15:33:12 -0500 |
commit | 2bf8494332592b3c57f9a12a26b9abd356fb3f15 (patch) | |
tree | e46c61ba30018ebbafcda6b97b36d3ffece3cfb0 /models/user.go | |
parent | df2bdf7ea36ad0729e0326ca799d8896d7f96946 (diff) | |
download | gitea-2bf8494332592b3c57f9a12a26b9abd356fb3f15.tar.gz gitea-2bf8494332592b3c57f9a12a26b9abd356fb3f15.zip |
#13 finish user and repository search
Both are possible on explore and admin panel
Diffstat (limited to 'models/user.go')
-rw-r--r-- | models/user.go | 51 |
1 files changed, 40 insertions, 11 deletions
diff --git a/models/user.go b/models/user.go index 865178b4f5..a95e1f1925 100644 --- a/models/user.go +++ b/models/user.go @@ -36,8 +36,8 @@ import ( type UserType int const ( - INDIVIDUAL UserType = iota // Historic reason to make it starts at 0. - ORGANIZATION + USER_TYPE_INDIVIDUAL UserType = iota // Historic reason to make it starts at 0. + USER_TYPE_ORGANIZATION ) var ( @@ -389,7 +389,7 @@ func (u *User) IsWriterOfRepo(repo *Repository) bool { // IsOrganization returns true if user is actually a organization. func (u *User) IsOrganization() bool { - return u.Type == ORGANIZATION + return u.Type == USER_TYPE_ORGANIZATION } // IsUserOrgOwner returns true if user is in the owner team of given organization. @@ -1114,16 +1114,45 @@ func GetUserByEmail(email string) (*User, error) { return nil, ErrUserNotExist{0, email} } -// SearchUserByName returns given number of users whose name contains keyword. -func SearchUserByName(opt SearchOption) (us []*User, err error) { - if len(opt.Keyword) == 0 { - return us, nil +type SearchUserOptions struct { + Keyword string + Type UserType + OrderBy string + Page int + PageSize int // Can be smaller than or equal to setting.ExplorePagingNum +} + +// SearchUserByName takes keyword and part of user name to search, +// it returns results in given range and number of total results. +func SearchUserByName(opts *SearchUserOptions) (users []*User, _ int64, _ error) { + if len(opts.Keyword) == 0 { + return users, 0, nil + } + opts.Keyword = strings.ToLower(opts.Keyword) + + if opts.PageSize <= 0 || opts.PageSize > setting.ExplorePagingNum { + opts.PageSize = setting.ExplorePagingNum + } + if opts.Page <= 0 { + opts.Page = 1 + } + + users = make([]*User, 0, opts.PageSize) + // Append conditions + fmt.Println(opts.Type) + sess := x.Where("lower_name like ?", "%"+opts.Keyword+"%").And("type = ?", opts.Type) + if len(opts.OrderBy) > 0 { + sess.OrderBy(opts.OrderBy) + } + + var countSess xorm.Session + countSess = *sess + count, err := countSess.Count(new(User)) + if err != nil { + return nil, 0, fmt.Errorf("Count: %v", err) } - opt.Keyword = strings.ToLower(opt.Keyword) - us = make([]*User, 0, opt.Limit) - err = x.Limit(opt.Limit).Where("type=0").And("lower_name like ?", "%"+opt.Keyword+"%").Find(&us) - return us, err + return users, count, sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).Find(&users) } // ___________ .__ .__ |