2017-06-08 17:20:43 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2017-07-14 17:27:42 +00:00
|
|
|
_ "image/gif"
|
|
|
|
_ "image/jpeg"
|
|
|
|
_ "image/png"
|
|
|
|
|
2017-06-08 17:20:43 +00:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
2017-07-06 17:09:49 +00:00
|
|
|
func refreshTeamsInMemory() {
|
2017-10-02 23:06:35 +00:00
|
|
|
site.Teams = db.getAllTeams()
|
2017-07-06 17:09:49 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 17:20:43 +00:00
|
|
|
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-02 23:06:35 +00:00
|
|
|
if db.getTeamByName(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 {
|
2017-10-02 23:06:35 +00:00
|
|
|
if err := db.newTeam(name); err != nil {
|
2017-06-08 17:20:43 +00:00
|
|
|
page.session.setFlashMessage(err.Error(), "error")
|
|
|
|
} else {
|
|
|
|
page.session.setFlashMessage("Team "+name+" created!", "success")
|
|
|
|
}
|
|
|
|
}
|
2017-07-06 17:09:49 +00:00
|
|
|
refreshTeamsInMemory()
|
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-02 23:06:35 +00:00
|
|
|
tm := db.getTeam(teamId)
|
|
|
|
if tm != nil {
|
2017-06-08 17:20:43 +00:00
|
|
|
switch vars["function"] {
|
|
|
|
case "save":
|
|
|
|
tm.UUID = teamId
|
|
|
|
tm.Name = req.FormValue("teamname")
|
2017-10-02 23:06:35 +00:00
|
|
|
if err := tm.save(); err != nil {
|
2017-06-08 17:20:43 +00:00
|
|
|
page.session.setFlashMessage("Error updating team: "+err.Error(), "error")
|
|
|
|
} else {
|
|
|
|
page.session.setFlashMessage("Team Updated!", "success")
|
|
|
|
}
|
2017-07-06 17:09:49 +00:00
|
|
|
refreshTeamsInMemory()
|
2017-06-08 17:20:43 +00:00
|
|
|
redirect("/admin/teams", w, req)
|
|
|
|
case "delete":
|
|
|
|
var err error
|
2017-10-02 23:06:35 +00:00
|
|
|
if err = tm.delete(); err != nil {
|
2017-06-08 17:20:43 +00:00
|
|
|
page.session.setFlashMessage("Error deleting team: "+err.Error(), "error")
|
2017-06-15 17:35:53 +00:00
|
|
|
} else {
|
2017-10-02 23:06:35 +00:00
|
|
|
page.session.setFlashMessage("Team "+tm.Name+" Deleted", "success")
|
2017-06-08 17:20:43 +00:00
|
|
|
}
|
2017-07-06 17:09:49 +00:00
|
|
|
refreshTeamsInMemory()
|
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-02 23:06:35 +00:00
|
|
|
mbr := newTeamMember(mbrName)
|
|
|
|
mbr.SlackId = req.FormValue("newmemberslackid")
|
|
|
|
mbr.Twitter = req.FormValue("newmembertwitter")
|
|
|
|
mbr.Email = req.FormValue("newmemberemail")
|
|
|
|
if err := tm.updateTeamMember(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")
|
|
|
|
}
|
2017-07-06 17:09:49 +00:00
|
|
|
refreshTeamsInMemory()
|
2017-07-25 15:49:26 +00:00
|
|
|
redirect("/admin/teams/"+teamId+"#members", w, req)
|
2017-06-12 23:23:46 +00:00
|
|
|
case "deletemember":
|
2017-10-02 23:06:35 +00:00
|
|
|
m := tm.getTeamMember(req.FormValue("memberid"))
|
|
|
|
if m != nil {
|
|
|
|
if err := tm.deleteTeamMember(m); err != nil {
|
|
|
|
page.session.setFlashMessage("Error deleting team member: "+err.Error(), "error")
|
|
|
|
} else {
|
|
|
|
page.session.setFlashMessage(m.Name+" deleted from team", "success")
|
|
|
|
}
|
|
|
|
refreshTeamsInMemory()
|
2017-06-12 23:23:46 +00:00
|
|
|
} else {
|
2017-10-02 23:06:35 +00:00
|
|
|
page.session.setFlashMessage("Couldn't find team member to delete", "error")
|
2017-06-12 23:23:46 +00:00
|
|
|
}
|
2017-07-25 15:49:26 +00:00
|
|
|
redirect("/admin/teams/"+teamId+"#members", w, req)
|
2017-06-08 17:20:43 +00:00
|
|
|
default:
|
|
|
|
page.SubTitle = "Edit Team"
|
2017-10-02 23:06:35 +00:00
|
|
|
t := db.getTeam(teamId)
|
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-02 23:06:35 +00:00
|
|
|
page.TemplateData = teamsPageData{Teams: db.getAllTeams()}
|
2017-06-08 17:20:43 +00:00
|
|
|
page.SubTitle = "Teams"
|
|
|
|
page.show("admin-teams.html", w)
|
|
|
|
}
|
|
|
|
}
|