ictgj-voting/model_teams.go

311 lines
6.6 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-06-12 23:23:46 +00:00
"fmt"
2017-06-08 20:43:11 +00:00
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
2017-06-12 23:23:46 +00:00
if err = openDatabase(); err != nil {
2017-04-21 18:17:18 +00:00
return err
}
2017-06-12 23:23:46 +00:00
defer closeDatabase()
2017-04-21 18:17:18 +00:00
// 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
2017-06-12 23:23:46 +00:00
if err = openDatabase(); err != nil {
2017-04-21 18:17:18 +00:00
return false
}
2017-06-12 23:23:46 +00:00
defer closeDatabase()
2017-04-21 18:17:18 +00:00
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
2017-06-12 23:23:46 +00:00
if err = openDatabase(); err != nil {
2017-04-21 18:17:18 +00:00
return ret
}
2017-06-12 23:23:46 +00:00
defer closeDatabase()
2017-04-21 18:17:18 +00:00
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
2017-06-12 23:23:46 +00:00
if err = openDatabase(); err != nil {
2017-04-21 18:17:18 +00:00
return nil
}
2017-06-12 23:23:46 +00:00
defer closeDatabase()
2017-04-21 18:17:18 +00:00
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
}
2017-06-12 23:23:46 +00:00
tm.Members, _ = dbGetTeamMembers(id)
2017-06-15 17:35:53 +00:00
tm.Game = dbGetTeamGame(id)
2017-04-21 18:17:18 +00:00
return tm
}
func dbGetTeamByName(nm string) *Team {
var err error
2017-06-12 23:23:46 +00:00
if err = openDatabase(); err != nil {
2017-04-21 18:17:18 +00:00
return nil
}
2017-06-12 23:23:46 +00:00
defer closeDatabase()
2017-04-21 18:17:18 +00:00
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
2017-06-12 23:23:46 +00:00
if err = openDatabase(); err != nil {
return err
2017-04-21 18:17:18 +00:00
}
2017-06-12 23:23:46 +00:00
defer closeDatabase()
2017-04-21 18:17:18 +00:00
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
2017-06-12 23:23:46 +00:00
if err = openDatabase(); err != nil {
2017-04-21 18:17:18 +00:00
return err
}
2017-06-12 23:23:46 +00:00
defer closeDatabase()
2017-06-08 17:20:43 +00:00
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
2017-06-12 23:23:46 +00:00
if err = openDatabase(); err != nil {
2017-06-08 20:43:11 +00:00
return err
}
2017-06-12 23:23:46 +00:00
defer closeDatabase()
2017-06-08 20:43:11 +00:00
gamePath := []string{"teams", teamid, "game"}
return db.SetValue(gamePath, "name", name)
}
2017-06-12 23:23:46 +00:00
func dbAddTeamMember(teamid, mbrName, mbrEmail, mbrSlack, mbrTwitter string) error {
// First check if this member already exists on this team
mbrs, _ := dbGetTeamMembers(teamid)
if len(mbrs) > 0 {
for i := range mbrs {
if mbrs[i].Name == mbrName {
return dbUpdateTeamMember(teamid, mbrs[i].UUID, mbrName, mbrEmail, mbrSlack, mbrTwitter)
}
}
}
// It's really an add
mbrId := uuid.New()
return dbUpdateTeamMember(teamid, mbrId, mbrName, mbrEmail, mbrSlack, mbrTwitter)
}
func dbUpdateTeamMember(teamid, mbrId, mbrName, mbrEmail, mbrSlack, mbrTwitter string) error {
var err error
if err = openDatabase(); err != nil {
return err
}
defer closeDatabase()
mbrPath := []string{"teams", teamid, "members", mbrId}
if db.SetValue(mbrPath, "name", mbrName) != nil {
return err
}
if db.SetValue(mbrPath, "slackid", mbrSlack) != nil {
return err
}
if db.SetValue(mbrPath, "twitter", mbrTwitter) != nil {
return err
}
if db.SetValue(mbrPath, "email", mbrEmail) != nil {
return err
}
return nil
}
2017-06-08 20:43:11 +00:00
func dbGetTeamMembers(teamid string) ([]TeamMember, error) {
var ret []TeamMember
var err error
2017-06-12 23:23:46 +00:00
if err = openDatabase(); err != nil {
return ret, err
2017-06-08 20:43:11 +00:00
}
2017-06-12 23:23:46 +00:00
defer closeDatabase()
2017-06-08 20:43:11 +00:00
2017-06-12 23:23:46 +00:00
teamPath := []string{"teams", teamid, "members"}
2017-06-08 20:43:11 +00:00
var memberUuids []string
2017-06-12 23:23:46 +00:00
if memberUuids, err = db.GetBucketList(teamPath); err == nil {
2017-06-08 20:43:11 +00:00
for _, v := range memberUuids {
var mbr *TeamMember
2017-06-12 23:23:46 +00:00
if mbr, err = dbGetTeamMember(teamid, v); err == nil {
2017-06-08 20:43:11 +00:00
ret = append(ret, *mbr)
}
}
2017-06-12 23:23:46 +00:00
} else {
fmt.Println(err.Error())
2017-06-08 20:43:11 +00:00
}
return ret, nil
}
func dbGetTeamMember(teamid, mbrid string) (*TeamMember, error) {
var err error
2017-06-12 23:23:46 +00:00
if err = openDatabase(); err != nil {
2017-06-08 20:43:11 +00:00
return nil, err
}
2017-06-12 23:23:46 +00:00
defer closeDatabase()
2017-06-08 20:43:11 +00:00
2017-06-12 23:23:46 +00:00
mbr := new(TeamMember)
2017-06-08 20:43:11 +00:00
teamMbrPath := []string{"teams", teamid, "members", mbrid}
2017-06-12 23:23:46 +00:00
mbr.UUID = mbrid
if mbr.Name, err = db.GetValue(teamMbrPath, "name"); err != nil {
return nil, err
}
if mbr.SlackId, err = db.GetValue(teamMbrPath, "slackid"); err != nil {
return nil, err
}
if mbr.Twitter, err = db.GetValue(teamMbrPath, "twitter"); err != nil {
return nil, err
}
if mbr.Email, err = db.GetValue(teamMbrPath, "email"); err != nil {
return nil, err
}
return mbr, err
}
// This function returns the team for a specific member
func dbGetMembersTeam(mbrid string) (*Team, error) {
var err error
if err = openDatabase(); err != nil {
return nil, err
}
defer closeDatabase()
teams := dbGetAllTeams()
for i := range teams {
var tmMbrs []TeamMember
tmMbrs, err = dbGetTeamMembers(teams[i].UUID)
if err == nil {
for j := range tmMbrs {
if tmMbrs[j].UUID == mbrid {
return &teams[i], nil
}
2017-06-08 20:43:11 +00:00
}
2017-06-12 23:23:46 +00:00
}
}
return nil, errors.New("Unable to find team member")
}
// This function searches all teams for a member with the given name
func dbGetTeamMembersByName(mbrName string) ([]TeamMember, error) {
var ret []TeamMember
var err error
if err = openDatabase(); err != nil {
return ret, err
}
defer closeDatabase()
teams := dbGetAllTeams()
for i := range teams {
var tmMbrs []TeamMember
tmMbrs, err = dbGetTeamMembers(teams[i].UUID)
if err == nil {
for j := range tmMbrs {
if tmMbrs[j].Name == mbrName {
ret = append(ret, tmMbrs[j])
}
2017-06-08 20:43:11 +00:00
}
}
}
2017-06-12 23:23:46 +00:00
if len(ret) == 0 {
return nil, errors.New("Couldn't find any members with the requested name")
}
return ret, nil
}
func dbDeleteTeamMember(teamId, mbrId string) error {
var err error
if err = openDatabase(); err != nil {
return err
}
defer closeDatabase()
teamPath := []string{"teams", teamId, "members"}
return db.DeleteBucket(teamPath, mbrId)
2017-06-08 20:43:11 +00:00
}