ictgj-voting/admin_teams.go

104 lines
3.1 KiB
Go
Raw Normal View History

2017-06-08 17:20:43 +00:00
package main
import (
2017-10-19 19:53:07 +00:00
"fmt"
2017-06-08 17:20:43 +00:00
"net/http"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
2017-06-08 17:20:43 +00:00
"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-10-19 19:53:07 +00:00
tm := NewTeam("")
tm.Name = name
if err := m.jam.AddTeam(tm); err != nil {
page.session.setFlashMessage("Error adding team: "+err.Error(), "error")
2017-06-08 17:20:43 +00:00
}
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-10-19 19:53:07 +00:00
tm, _ := m.jam.GetTeamById(teamId)
if tm != nil {
2017-06-08 17:20:43 +00:00
switch vars["function"] {
case "save":
tm.Name = req.FormValue("teamname")
2017-10-19 19:53:07 +00:00
page.session.setFlashMessage("Team Updated!", "success")
2017-06-08 17:20:43 +00:00
redirect("/admin/teams", w, req)
case "delete":
var err error
2017-10-19 19:53:07 +00:00
if err = m.jam.RemoveTeamById(teamId); err != nil {
page.session.setFlashMessage("Error removing team: "+err.Error(), "error")
2017-06-15 17:35:53 +00:00
} else {
2017-10-19 19:53:07 +00:00
page.session.setFlashMessage("Team "+tm.Name+" Removed", "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")
2017-10-19 19:53:07 +00:00
mbr, err := NewTeamMember(tm.UUID, "")
if err == nil {
2017-10-21 22:47:01 +00:00
mbr.Name = mbrName
2017-10-19 19:53:07 +00:00
mbr.SlackId = req.FormValue("newmemberslackid")
mbr.Twitter = req.FormValue("newmembertwitter")
mbr.Email = req.FormValue("newmemberemail")
}
if err := tm.AddTeamMember(mbr); err != nil {
2017-06-12 23:23:46 +00:00
page.session.setFlashMessage("Error adding team member: "+err.Error(), "error")
} else {
page.session.setFlashMessage(mbrName+" added to team!", "success")
}
redirect("/admin/teams/"+teamId+"#members", w, req)
2017-06-12 23:23:46 +00:00
case "deletemember":
2017-10-19 19:53:07 +00:00
var err error
var mbr *TeamMember
if mbr, err = tm.GetTeamMemberById(req.FormValue("memberid")); err != nil {
fmt.Println("Error removing team member: " + err.Error())
page.session.setFlashMessage("Error deleting team member", "error")
redirect("/admin/teams/"+teamId+"#members", w, req)
}
if err = tm.RemoveTeamMemberById(mbr.UUID); err != nil {
fmt.Println("Error removing team member: " + err.Error())
page.session.setFlashMessage("Error deleting team member", "error")
2017-06-12 23:23:46 +00:00
} else {
2017-10-19 19:53:07 +00:00
page.session.setFlashMessage(mbr.Name+" deleted from team", "success")
2017-06-12 23:23:46 +00:00
}
redirect("/admin/teams/"+teamId+"#members", w, req)
2017-06-08 17:20:43 +00:00
default:
page.SubTitle = "Edit Team"
2017-10-19 19:53:07 +00:00
t, err := m.jam.GetTeamById(teamId)
if err != nil {
page.session.setFlashMessage("Error loading team: "+err.Error(), "error")
redirect("/admin/teams", w, req)
}
2017-06-08 17:20:43 +00:00
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
}
2017-10-19 19:53:07 +00:00
page.TemplateData = m.jam
2017-06-08 17:20:43 +00:00
page.SubTitle = "Teams"
page.show("admin-teams.html", w)
}
}