diff --git a/admin_teams.go b/admin_teams.go index 7d02c17..536f4a2 100644 --- a/admin_teams.go +++ b/admin_teams.go @@ -49,6 +49,23 @@ func handleAdminTeams(w http.ResponseWriter, req *http.Request, page *pageData) } redirect("/admin/teams", w, req) case "savemember": + mbrName := req.FormValue("newmembername") + mbrSlack := req.FormValue("newmemberslackid") + mbrTwitter := req.FormValue("newmembertwitter") + mbrEmail := req.FormValue("newmemberemail") + if err := dbAddTeamMember(teamId, mbrName, mbrEmail, mbrSlack, mbrTwitter); err != nil { + page.session.setFlashMessage("Error adding team member: "+err.Error(), "error") + } else { + page.session.setFlashMessage(mbrName+" added to team!", "success") + } + redirect("/admin/teams/"+teamId, w, req) + case "deletemember": + mbrId := req.FormValue("memberid") + if err := dbDeleteTeamMember(teamId, mbrId); err != nil { + page.session.setFlashMessage("Error deleting team member: "+err.Error(), "error") + } else { + page.session.setFlashMessage("Member deleted from team", "success") + } redirect("/admin/teams/"+teamId, w, req) default: page.SubTitle = "Edit Team" diff --git a/model.go b/model.go index 33bddab..83fa246 100644 --- a/model.go +++ b/model.go @@ -8,22 +8,35 @@ import ( ) var db *boltease.DB -var dbOpened bool +var dbOpened int func openDatabase() error { - if !dbOpened { + dbOpened += 1 + if dbOpened == 1 { var err error db, err = boltease.Create(site.DB, 0600, nil) if err != nil { return err } - dbOpened = true + } + return nil +} + +func closeDatabase() error { + dbOpened -= 1 + if dbOpened == 0 { + return db.CloseDB() } return nil } func initDatabase() error { - openDatabase() + var err error + if err = openDatabase(); err != nil { + return err + } + defer closeDatabase() + // Create the path to the bucket to store admin users if err := db.MkBucketPath([]string{"users"}); err != nil { return err @@ -37,10 +50,11 @@ func initDatabase() error { } func dbSetCurrentJam(name string) error { - if err := db.OpenDB(); err != nil { + var err error + if err = openDatabase(); err != nil { return err } - defer db.CloseDB() + defer closeDatabase() return db.SetValue([]string{"site"}, "current-jam", name) } @@ -56,10 +70,10 @@ func dbHasCurrentJam() bool { func dbGetCurrentJam() (string, error) { var ret string var err error - if err = db.OpenDB(); err != nil { + if err = openDatabase(); err != nil { return "", err } - defer db.CloseDB() + defer closeDatabase() ret, err = db.GetValue([]string{"site"}, "current-jam") diff --git a/model_teams.go b/model_teams.go index 12750ba..3e8d812 100644 --- a/model_teams.go +++ b/model_teams.go @@ -2,6 +2,7 @@ package main import ( "errors" + "fmt" "github.com/pborman/uuid" ) @@ -23,10 +24,10 @@ type TeamMember struct { func dbCreateNewTeam(nm string) error { var err error - if err = db.OpenDB(); err != nil { + if err = openDatabase(); err != nil { return err } - defer db.CloseDB() + defer closeDatabase() // Generate a UUID uuid := uuid.New() @@ -53,10 +54,10 @@ func dbCreateNewTeam(nm string) error { func dbIsValidTeam(id string) bool { var err error - if err = db.OpenDB(); err != nil { + if err = openDatabase(); err != nil { return false } - defer db.CloseDB() + defer closeDatabase() teamPath := []string{"teams"} if teamUids, err := db.GetBucketList(teamPath); err == nil { @@ -72,10 +73,10 @@ func dbIsValidTeam(id string) bool { func dbGetAllTeams() []Team { var ret []Team var err error - if err = db.OpenDB(); err != nil { + if err = openDatabase(); err != nil { return ret } - defer db.CloseDB() + defer closeDatabase() teamPath := []string{"teams"} var teamUids []string @@ -92,10 +93,10 @@ func dbGetAllTeams() []Team { func dbGetTeam(id string) *Team { var err error - if err = db.OpenDB(); err != nil { + if err = openDatabase(); err != nil { return nil } - defer db.CloseDB() + defer closeDatabase() teamPath := []string{"teams", id} tm := new(Team) @@ -103,15 +104,16 @@ func dbGetTeam(id string) *Team { if tm.Name, err = db.GetValue(teamPath, "name"); err != nil { return nil } + tm.Members, _ = dbGetTeamMembers(id) return tm } func dbGetTeamByName(nm string) *Team { var err error - if err = db.OpenDB(); err != nil { + if err = openDatabase(); err != nil { return nil } - defer db.CloseDB() + defer closeDatabase() teamPath := []string{"teams"} var teamUids []string @@ -128,10 +130,10 @@ func dbGetTeamByName(nm string) *Team { func dbUpdateTeam(id string, tm *Team) error { var err error - if err = db.OpenDB(); err != nil { - return nil + if err = openDatabase(); err != nil { + return err } - defer db.CloseDB() + defer closeDatabase() teamPath := []string{"teams", id} return db.SetValue(teamPath, "name", tm.Name) @@ -139,10 +141,10 @@ func dbUpdateTeam(id string, tm *Team) error { func dbDeleteTeam(id string) error { var err error - if err = db.OpenDB(); err != nil { + if err = openDatabase(); err != nil { return err } - defer db.CloseDB() + defer closeDatabase() teamPath := []string{"teams"} return db.DeleteBucket(teamPath, id) @@ -150,63 +152,159 @@ func dbDeleteTeam(id string) error { func dbEditTeamGame(teamid, name string) error { var err error - if err = db.OpenDB(); err != nil { + if err = openDatabase(); err != nil { return err } - defer db.CloseDB() + defer closeDatabase() gamePath := []string{"teams", teamid, "game"} return db.SetValue(gamePath, "name", name) } +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 +} + func dbGetTeamMembers(teamid string) ([]TeamMember, error) { var ret []TeamMember var err error - if err = db.OpenDB(); err != nil { - return ret, nil + if err = openDatabase(); err != nil { + return ret, err } - defer db.CloseDB() + defer closeDatabase() - teamMbrPath := []string{"teams", teamid, "members"} + teamPath := []string{"teams", teamid, "members"} var memberUuids []string - if memberUuids, err = db.GetBucketList(teamMbrPath); err != nil { + if memberUuids, err = db.GetBucketList(teamPath); err == nil { for _, v := range memberUuids { var mbr *TeamMember - if mbr, err = dbGetTeamMember(teamid, v); err != nil { + if mbr, err = dbGetTeamMember(teamid, v); err == nil { + fmt.Println("Finding Team Members", teamid, mbr.Name) ret = append(ret, *mbr) } } + } else { + fmt.Println(err.Error()) } return ret, nil } func dbGetTeamMember(teamid, mbrid string) (*TeamMember, error) { var err error - if err = db.OpenDB(); err != nil { + if err = openDatabase(); err != nil { return nil, err } - defer db.CloseDB() + defer closeDatabase() + mbr := new(TeamMember) 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 + 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 + } } - 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") + 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]) + } + } + } + } + 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) } diff --git a/templates/admin-editteam.html b/templates/admin-editteam.html index cfd42ae..f198a9d 100644 --- a/templates/admin-editteam.html +++ b/templates/admin-editteam.html @@ -1,3 +1,4 @@ +{{ $uuid := .TemplateData.UUID }}