1
0
Fork 0

Save team members function

This commit is contained in:
Gregory Eremin 2015-03-19 20:31:28 +07:00
parent 2028b24e3c
commit a37a7ce315
1 changed files with 34 additions and 3 deletions

View File

@ -7,7 +7,7 @@ import (
"time" "time"
) )
func SaveOrgMembers(org uint64, members []uint64) { func SaveOrgMembers(orgID uint64, members []uint64) {
defer measure("SaveOrgMembers", time.Now()) defer measure("SaveOrgMembers", time.Now())
tx := db.MustBegin() tx := db.MustBegin()
@ -20,11 +20,11 @@ func SaveOrgMembers(org uint64, members []uint64) {
where where
org_id = %d and org_id = %d and
user_id not in (%s) user_id not in (%s)
`, org, strings.Join(ids, ", "))) `, orgID, strings.Join(ids, ", ")))
var values = []string{} var values = []string{}
for _, id := range members { for _, id := range members {
values = append(values, fmt.Sprintf("(%d, %d)", org, id)) values = append(values, fmt.Sprintf("(%d, %d)", orgID, id))
} }
tx.MustExec(` tx.MustExec(`
insert into org_members (org_id, user_id) insert into org_members (org_id, user_id)
@ -36,3 +36,34 @@ func SaveOrgMembers(org uint64, members []uint64) {
panic(err) panic(err)
} }
} }
func SaveTeamMembers(orgID, teamID uint64, members []uint64) {
defer measure("SaveTeamMembers", time.Now())
tx := db.MustBegin()
var ids = []string{}
for _, id := range members {
ids = append(ids, strconv.FormatUint(id, 10))
}
tx.MustExec(fmt.Sprintf(`
delete from team_members
where
org_id = %d and
team_id = %d and
user_id not in (%s)
`, orgID, teamID, strings.Join(ids, ", ")))
var values = []string{}
for _, id := range members {
values = append(values, fmt.Sprintf("(%d, %d, %d)", orgID, teamID, id))
}
tx.MustExec(`
insert into team_members (org_id, team_id, user_id)
values ` + strings.Join(values, ", ") + `
on duplicate key update org_id = org_id
`)
if err := tx.Commit(); err != nil {
panic(err)
}
}