ictgj-voting/admin_teams.go

96 lines
3.0 KiB
Go
Raw Normal View History

2017-06-08 17:20:43 +00:00
package main
import (
"net/http"
"github.com/gorilla/mux"
)
func handleAdminTeams(w http.ResponseWriter, req *http.Request, page *pageData) {
vars := mux.Vars(req)
page.SubTitle = "Teams"
teamId := vars["id"]
if teamId == "new" {
2017-06-15 17:35:53 +00:00
// Add a new team
2017-06-08 17:20:43 +00:00
switch vars["function"] {
case "save":
name := req.FormValue("teamname")
2017-06-08 20:43:11 +00:00
if dbGetTeamByName(name) != nil {
2017-06-08 17:20:43 +00:00
// A team with that name already exists
page.session.setFlashMessage("A team with the name "+name+" already exists!", "error")
} else {
if err := dbCreateNewTeam(name); err != nil {
page.session.setFlashMessage(err.Error(), "error")
} else {
page.session.setFlashMessage("Team "+name+" created!", "success")
}
}
redirect("/admin/teams", w, req)
default:
page.SubTitle = "Add New Team"
page.show("admin-addteam.html", w)
}
} else if teamId != "" {
2017-06-15 17:35:53 +00:00
// Functions for existing team
2017-06-08 17:20:43 +00:00
if dbIsValidTeam(teamId) {
switch vars["function"] {
case "save":
tm := new(Team)
tm.UUID = teamId
tm.Name = req.FormValue("teamname")
if err := dbUpdateTeam(teamId, tm); err != nil {
page.session.setFlashMessage("Error updating team: "+err.Error(), "error")
} else {
page.session.setFlashMessage("Team Updated!", "success")
}
redirect("/admin/teams", w, req)
case "delete":
var err error
2017-06-15 17:35:53 +00:00
t := dbGetTeam(teamId)
2017-06-08 17:20:43 +00:00
if err = dbDeleteTeam(teamId); err != nil {
page.session.setFlashMessage("Error deleting team: "+err.Error(), "error")
2017-06-15 17:35:53 +00:00
} else {
page.session.setFlashMessage("Team "+t.Name+" Deleted", "success")
2017-06-08 17:20:43 +00:00
}
redirect("/admin/teams", w, req)
2017-06-08 20:43:11 +00:00
case "savemember":
2017-06-12 23:23:46 +00:00
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")
2017-06-15 17:35:53 +00:00
m, _ := dbGetTeamMember(teamId, mbrId)
2017-06-12 23:23:46 +00:00
if err := dbDeleteTeamMember(teamId, mbrId); err != nil {
page.session.setFlashMessage("Error deleting team member: "+err.Error(), "error")
} else {
2017-06-15 17:35:53 +00:00
page.session.setFlashMessage(m.Name+" deleted from team", "success")
2017-06-12 23:23:46 +00:00
}
2017-06-08 20:43:11 +00:00
redirect("/admin/teams/"+teamId, w, req)
2017-06-08 17:20:43 +00:00
default:
page.SubTitle = "Edit Team"
t := dbGetTeam(teamId)
page.TemplateData = t
page.show("admin-editteam.html", w)
}
} else {
page.session.setFlashMessage("Couldn't find the requested team, please try again.", "error")
redirect("/admin/teams", w, req)
}
} else {
2017-06-15 17:35:53 +00:00
// Team List
2017-06-08 17:20:43 +00:00
type teamsPageData struct {
Teams []Team
}
page.TemplateData = teamsPageData{Teams: dbGetAllTeams()}
page.SubTitle = "Teams"
page.show("admin-teams.html", w)
}
}