ictgj-voting/model_teams.go

213 lines
4.3 KiB
Go
Raw Normal View History

2017-04-21 18:17:18 +00:00
package main
import (
2017-06-08 20:43:11 +00:00
"errors"
2017-04-21 18:17:18 +00:00
"github.com/pborman/uuid"
)
type Team struct {
UUID string
Name string
Members []TeamMember
Game *Game
}
type TeamMember struct {
UUID string
Name string
SlackId string
Twitter string
Email string
}
func dbCreateNewTeam(nm string) error {
var err error
if err = db.OpenDB(); err != nil {
return err
}
defer db.CloseDB()
// Generate a UUID
uuid := uuid.New()
2017-06-08 17:20:43 +00:00
teamPath := []string{"teams", uuid}
2017-04-21 18:17:18 +00:00
if err := db.MkBucketPath(teamPath); err != nil {
return err
}
if err := db.SetValue(teamPath, "name", nm); err != nil {
return err
}
if err := db.MkBucketPath(append(teamPath, "members")); err != nil {
return err
}
gamePath := append(teamPath, "game")
if err := db.MkBucketPath(gamePath); err != nil {
return err
}
if err := db.SetValue(append(gamePath), "name", ""); err != nil {
return err
}
return db.MkBucketPath(append(gamePath, "screenshots"))
}
2017-06-08 17:20:43 +00:00
func dbIsValidTeam(id string) bool {
2017-04-21 18:17:18 +00:00
var err error
if err = db.OpenDB(); err != nil {
return false
}
defer db.CloseDB()
2017-06-08 17:20:43 +00:00
teamPath := []string{"teams"}
2017-04-21 18:17:18 +00:00
if teamUids, err := db.GetBucketList(teamPath); err == nil {
for _, v := range teamUids {
2017-06-08 17:20:43 +00:00
if v == id {
return true
2017-04-21 18:17:18 +00:00
}
}
}
return false
}
func dbGetAllTeams() []Team {
var ret []Team
var err error
if err = db.OpenDB(); err != nil {
return ret
}
defer db.CloseDB()
2017-06-08 17:20:43 +00:00
teamPath := []string{"teams"}
var teamUids []string
if teamUids, err = db.GetBucketList(teamPath); err != nil {
2017-04-21 18:17:18 +00:00
return ret
}
2017-06-08 17:20:43 +00:00
for _, v := range teamUids {
if tm := dbGetTeam(v); tm != nil {
ret = append(ret, *tm)
2017-04-21 18:17:18 +00:00
}
}
return ret
}
func dbGetTeam(id string) *Team {
var err error
if err = db.OpenDB(); err != nil {
return nil
}
defer db.CloseDB()
2017-06-08 17:20:43 +00:00
teamPath := []string{"teams", id}
2017-04-21 18:17:18 +00:00
tm := new(Team)
2017-06-08 17:20:43 +00:00
tm.UUID = id
2017-04-21 18:17:18 +00:00
if tm.Name, err = db.GetValue(teamPath, "name"); err != nil {
return nil
}
return tm
}
func dbGetTeamByName(nm string) *Team {
var err error
if err = db.OpenDB(); err != nil {
return nil
}
defer db.CloseDB()
2017-06-08 17:20:43 +00:00
teamPath := []string{"teams"}
2017-04-21 18:17:18 +00:00
var teamUids []string
if teamUids, err = db.GetBucketList(teamPath); err != nil {
for _, v := range teamUids {
var name string
if name, err = db.GetValue(append(teamPath, v), "name"); name == nm {
return dbGetTeam(v)
}
}
}
return nil
}
2017-06-08 17:20:43 +00:00
func dbUpdateTeam(id string, tm *Team) error {
2017-04-21 18:17:18 +00:00
var err error
if err = db.OpenDB(); err != nil {
2017-06-08 17:20:43 +00:00
return nil
2017-04-21 18:17:18 +00:00
}
defer db.CloseDB()
2017-06-08 17:20:43 +00:00
teamPath := []string{"teams", id}
return db.SetValue(teamPath, "name", tm.Name)
}
func dbDeleteTeam(id string) error {
var err error
if err = db.OpenDB(); err != nil {
2017-04-21 18:17:18 +00:00
return err
}
2017-06-08 17:20:43 +00:00
defer db.CloseDB()
teamPath := []string{"teams"}
2017-04-21 18:17:18 +00:00
return db.DeleteBucket(teamPath, id)
}
2017-06-08 20:43:11 +00:00
func dbEditTeamGame(teamid, name string) error {
var err error
if err = db.OpenDB(); err != nil {
return err
}
defer db.CloseDB()
gamePath := []string{"teams", teamid, "game"}
return db.SetValue(gamePath, "name", name)
}
func dbGetTeamMembers(teamid string) ([]TeamMember, error) {
var ret []TeamMember
var err error
if err = db.OpenDB(); err != nil {
return ret, nil
}
defer db.CloseDB()
teamMbrPath := []string{"teams", teamid, "members"}
var memberUuids []string
if memberUuids, err = db.GetBucketList(teamMbrPath); err != nil {
for _, v := range memberUuids {
var mbr *TeamMember
if mbr, err = dbGetTeamMember(teamid, v); err != nil {
ret = append(ret, *mbr)
}
}
}
return ret, nil
}
func dbGetTeamMember(teamid, mbrid string) (*TeamMember, error) {
var err error
if err = db.OpenDB(); err != nil {
return nil, err
}
defer db.CloseDB()
teamMbrPath := []string{"teams", teamid, "members", mbrid}
var memberUuids []string
if memberUuids, err = db.GetBucketList(teamMbrPath); err != nil {
for _, v := range memberUuids {
mbr := new(TeamMember)
mbr.UUID = v
if mbr.Name, err = db.GetValue(append(teamMbrPath, v), "name"); err != nil {
return nil, err
}
if mbr.SlackId, err = db.GetValue(append(teamMbrPath, v), "slackid"); err != nil {
return nil, err
}
if mbr.Twitter, err = db.GetValue(append(teamMbrPath, v), "twitter"); err != nil {
return nil, err
}
if mbr.Email, err = db.GetValue(append(teamMbrPath, v), "email"); err != nil {
return nil, err
}
return mbr, err
}
}
return nil, errors.New("Couldn't find team member")
}