From 256f7176a294aed10ba7525c32c7fa91a0a02a7e Mon Sep 17 00:00:00 2001 From: bergquist Date: Thu, 14 Dec 2017 17:22:45 +0100 Subject: [PATCH] teams: add team count when searching for team closes #10207 --- pkg/api/team_test.go | 2 +- pkg/models/team.go | 17 +++++--- pkg/services/sqlstore/team.go | 46 ++++++++++++++------- public/app/features/org/partials/teams.html | 2 +- 4 files changed, 46 insertions(+), 21 deletions(-) diff --git a/pkg/api/team_test.go b/pkg/api/team_test.go index 535d24ff553..0bf06d723c8 100644 --- a/pkg/api/team_test.go +++ b/pkg/api/team_test.go @@ -13,7 +13,7 @@ import ( func TestTeamApiEndpoint(t *testing.T) { Convey("Given two teams", t, func() { mockResult := models.SearchTeamQueryResult{ - Teams: []*models.Team{ + Teams: []*models.SearchTeamDto{ {Name: "team1"}, {Name: "team2"}, }, diff --git a/pkg/models/team.go b/pkg/models/team.go index a8f53e81a1e..b9759f059cf 100644 --- a/pkg/models/team.go +++ b/pkg/models/team.go @@ -60,9 +60,16 @@ type SearchTeamsQuery struct { Result SearchTeamQueryResult } -type SearchTeamQueryResult struct { - TotalCount int64 `json:"totalCount"` - Teams []*Team `json:"teams"` - Page int `json:"page"` - PerPage int `json:"perPage"` +type SearchTeamDto struct { + Id int64 `json:"id"` + OrgId int64 `json:"orgId"` + Name string `json:"name"` + MemberCount int64 `json:"memberCount"` +} + +type SearchTeamQueryResult struct { + TotalCount int64 `json:"totalCount"` + Teams []*SearchTeamDto `json:"teams"` + Page int `json:"page"` + PerPage int `json:"perPage"` } diff --git a/pkg/services/sqlstore/team.go b/pkg/services/sqlstore/team.go index 2bc04bad0c6..3e9a6e6ec56 100644 --- a/pkg/services/sqlstore/team.go +++ b/pkg/services/sqlstore/team.go @@ -1,6 +1,7 @@ package sqlstore import ( + "bytes" "fmt" "time" @@ -114,37 +115,54 @@ func isTeamNameTaken(name string, existingId int64, sess *DBSession) (bool, erro func SearchTeams(query *m.SearchTeamsQuery) error { query.Result = m.SearchTeamQueryResult{ - Teams: make([]*m.Team, 0), + Teams: make([]*m.SearchTeamDto, 0), } queryWithWildcards := "%" + query.Query + "%" - sess := x.Table("team") - sess.Where("org_id=?", query.OrgId) + var sql bytes.Buffer + params := make([]interface{}, 0) + + sql.WriteString(`select + team.id as id, + team.name as name, + (select count(*) from team_member where team_member.team_id = team.id) as member_count + from team as team + where team.org_id = ?`) + + params = append(params, query.OrgId) if query.Query != "" { - sess.Where("name LIKE ?", queryWithWildcards) + sql.WriteString(` and team.name ` + dialect.LikeStr() + ` ?`) + params = append(params, queryWithWildcards) } - if query.Name != "" { - sess.Where("name=?", query.Name) - } - sess.Asc("name") - offset := query.Limit * (query.Page - 1) - sess.Limit(query.Limit, offset) - sess.Cols("id", "name") - if err := sess.Find(&query.Result.Teams); err != nil { + if query.Name != "" { + sql.WriteString(` and team.name = ?`) + params = append(params, query.Name) + } + + sql.WriteString(` order by team.name asc`) + + if query.Limit != 0 { + sql.WriteString(` limit ? offset ?`) + offset := query.Limit * (query.Page - 1) + params = append(params, query.Limit, offset) + } + + if err := x.Sql(sql.String(), params...).Find(&query.Result.Teams); err != nil { return err } team := m.Team{} - countSess := x.Table("team") if query.Query != "" { - countSess.Where("name LIKE ?", queryWithWildcards) + countSess.Where(`name `+dialect.LikeStr()+` ?`, queryWithWildcards) } + if query.Name != "" { countSess.Where("name=?", query.Name) } + count, err := countSess.Count(&team) query.Result.TotalCount = count diff --git a/public/app/features/org/partials/teams.html b/public/app/features/org/partials/teams.html index 44682ba18e9..6db586712bd 100644 --- a/public/app/features/org/partials/teams.html +++ b/public/app/features/org/partials/teams.html @@ -28,7 +28,7 @@ {{team.name}} - #Count + {{team.memberCount}}