teams: add team count when searching for team

closes #10207
This commit is contained in:
bergquist 2017-12-14 17:22:45 +01:00
parent 854d22fa8e
commit 256f7176a2
No known key found for this signature in database
GPG Key ID: F918C8F3C59049BE
4 changed files with 46 additions and 21 deletions

View File

@ -13,7 +13,7 @@ import (
func TestTeamApiEndpoint(t *testing.T) { func TestTeamApiEndpoint(t *testing.T) {
Convey("Given two teams", t, func() { Convey("Given two teams", t, func() {
mockResult := models.SearchTeamQueryResult{ mockResult := models.SearchTeamQueryResult{
Teams: []*models.Team{ Teams: []*models.SearchTeamDto{
{Name: "team1"}, {Name: "team1"},
{Name: "team2"}, {Name: "team2"},
}, },

View File

@ -60,9 +60,16 @@ type SearchTeamsQuery struct {
Result SearchTeamQueryResult Result SearchTeamQueryResult
} }
type SearchTeamDto struct {
Id int64 `json:"id"`
OrgId int64 `json:"orgId"`
Name string `json:"name"`
MemberCount int64 `json:"memberCount"`
}
type SearchTeamQueryResult struct { type SearchTeamQueryResult struct {
TotalCount int64 `json:"totalCount"` TotalCount int64 `json:"totalCount"`
Teams []*Team `json:"teams"` Teams []*SearchTeamDto `json:"teams"`
Page int `json:"page"` Page int `json:"page"`
PerPage int `json:"perPage"` PerPage int `json:"perPage"`
} }

View File

@ -1,6 +1,7 @@
package sqlstore package sqlstore
import ( import (
"bytes"
"fmt" "fmt"
"time" "time"
@ -114,37 +115,54 @@ func isTeamNameTaken(name string, existingId int64, sess *DBSession) (bool, erro
func SearchTeams(query *m.SearchTeamsQuery) error { func SearchTeams(query *m.SearchTeamsQuery) error {
query.Result = m.SearchTeamQueryResult{ query.Result = m.SearchTeamQueryResult{
Teams: make([]*m.Team, 0), Teams: make([]*m.SearchTeamDto, 0),
} }
queryWithWildcards := "%" + query.Query + "%" queryWithWildcards := "%" + query.Query + "%"
sess := x.Table("team") var sql bytes.Buffer
sess.Where("org_id=?", query.OrgId) 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 != "" { 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")
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) offset := query.Limit * (query.Page - 1)
sess.Limit(query.Limit, offset) params = append(params, query.Limit, offset)
sess.Cols("id", "name") }
if err := sess.Find(&query.Result.Teams); err != nil {
if err := x.Sql(sql.String(), params...).Find(&query.Result.Teams); err != nil {
return err return err
} }
team := m.Team{} team := m.Team{}
countSess := x.Table("team") countSess := x.Table("team")
if query.Query != "" { if query.Query != "" {
countSess.Where("name LIKE ?", queryWithWildcards) countSess.Where(`name `+dialect.LikeStr()+` ?`, queryWithWildcards)
} }
if query.Name != "" { if query.Name != "" {
countSess.Where("name=?", query.Name) countSess.Where("name=?", query.Name)
} }
count, err := countSess.Count(&team) count, err := countSess.Count(&team)
query.Result.TotalCount = count query.Result.TotalCount = count

View File

@ -28,7 +28,7 @@
<td> <td>
<a href="org/teams/edit/{{team.id}}">{{team.name}}</a> <a href="org/teams/edit/{{team.id}}">{{team.name}}</a>
</td> </td>
<td>#Count</td> <td>{{team.memberCount}}</td>
<td class="text-right"> <td class="text-right">
<a href="org/teams/edit/{{team.id}}" class="btn btn-inverse btn-small"> <a href="org/teams/edit/{{team.id}}" class="btn btn-inverse btn-small">
<i class="fa fa-edit"></i> <i class="fa fa-edit"></i>