diff options
author | David Svantesson <davidsvantesson@gmail.com> | 2019-10-01 07:32:28 +0200 |
---|---|---|
committer | Lunny Xiao <xiaolunwen@gmail.com> | 2019-10-01 13:32:28 +0800 |
commit | 36bcd4cd6b6d1131e3f812a825558fbfe5dcca20 (patch) | |
tree | 1606d302648ca6f2f6633d21aeb5d06a568b14d8 /models | |
parent | d3bc3dd4d16aef053699d5bfc45039db060d373e (diff) | |
download | gitea-36bcd4cd6b6d1131e3f812a825558fbfe5dcca20.tar.gz gitea-36bcd4cd6b6d1131e3f812a825558fbfe5dcca20.zip |
API endpoint for searching teams. (#8108)
* Api endpoint for searching teams.
Signed-off-by: dasv <david.svantesson@qrtech.se>
* Move API to /orgs/:org/teams/search
Signed-off-by: David Svantesson <davidsvantesson@gmail.com>
* Regenerate swagger
Signed-off-by: David Svantesson <davidsvantesson@gmail.com>
* Fix search is Get
Signed-off-by: David Svantesson <davidsvantesson@gmail.com>
* Add test for search team API.
Signed-off-by: David Svantesson <davidsvantesson@gmail.com>
* Update routers/api/v1/org/team.go
grammar
Co-Authored-By: Richard Mahn <richmahn@users.noreply.github.com>
* Fix review comments
Signed-off-by: David Svantesson <davidsvantesson@gmail.com>
* Fix some issues in repo collaboration team search, after changes in this PR.
Signed-off-by: David Svantesson <davidsvantesson@gmail.com>
* Remove teamUser which is not used and replace with actual user id.
Signed-off-by: David Svantesson <davidsvantesson@gmail.com>
* Remove unused search variable UserIsAdmin.
* Add paging to team search.
* Re-genereate swagger
Signed-off-by: David Svantesson <davidsvantesson@gmail.com>
* Fix review comments
Signed-off-by: David Svantesson <davidsvantesson@gmail.com>
* fix
* Regenerate swagger
Diffstat (limited to 'models')
-rw-r--r-- | models/org_team.go | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/models/org_team.go b/models/org_team.go index 90a089417d..fc5d5834ef 100644 --- a/models/org_team.go +++ b/models/org_team.go @@ -15,6 +15,7 @@ import ( "code.gitea.io/gitea/modules/setting" "github.com/go-xorm/xorm" + "xorm.io/builder" ) const ownerTeamName = "Owners" @@ -34,6 +35,67 @@ type Team struct { Units []*TeamUnit `xorm:"-"` } +// SearchTeamOptions holds the search options +type SearchTeamOptions struct { + UserID int64 + Keyword string + OrgID int64 + IncludeDesc bool + PageSize int + Page int +} + +// SearchTeam search for teams. Caller is responsible to check permissions. +func SearchTeam(opts *SearchTeamOptions) ([]*Team, int64, error) { + if opts.Page <= 0 { + opts.Page = 1 + } + if opts.PageSize == 0 { + // Default limit + opts.PageSize = 10 + } + + var cond = builder.NewCond() + + if len(opts.Keyword) > 0 { + lowerKeyword := strings.ToLower(opts.Keyword) + var keywordCond builder.Cond = builder.Like{"lower_name", lowerKeyword} + if opts.IncludeDesc { + keywordCond = keywordCond.Or(builder.Like{"LOWER(description)", lowerKeyword}) + } + cond = cond.And(keywordCond) + } + + cond = cond.And(builder.Eq{"org_id": opts.OrgID}) + + sess := x.NewSession() + defer sess.Close() + + count, err := sess. + Where(cond). + Count(new(Team)) + + if err != nil { + return nil, 0, err + } + + sess = sess.Where(cond) + if opts.PageSize == -1 { + opts.PageSize = int(count) + } else { + sess = sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize) + } + + teams := make([]*Team, 0, opts.PageSize) + if err = sess. + OrderBy("lower_name"). + Find(&teams); err != nil { + return nil, 0, err + } + + return teams, count, nil +} + // ColorFormat provides a basic color format for a Team func (t *Team) ColorFormat(s fmt.State) { log.ColorFprintf(s, "%d:%s (OrgID: %d) %-v", |