Add/Remove Team Members

This commit is contained in:
Brian Buller 2017-06-12 18:23:46 -05:00
parent 13048426b8
commit 597623d71b
4 changed files with 202 additions and 67 deletions

View File

@ -49,6 +49,23 @@ func handleAdminTeams(w http.ResponseWriter, req *http.Request, page *pageData)
} }
redirect("/admin/teams", w, req) redirect("/admin/teams", w, req)
case "savemember": 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) redirect("/admin/teams/"+teamId, w, req)
default: default:
page.SubTitle = "Edit Team" page.SubTitle = "Edit Team"

View File

@ -8,22 +8,35 @@ import (
) )
var db *boltease.DB var db *boltease.DB
var dbOpened bool var dbOpened int
func openDatabase() error { func openDatabase() error {
if !dbOpened { dbOpened += 1
if dbOpened == 1 {
var err error var err error
db, err = boltease.Create(site.DB, 0600, nil) db, err = boltease.Create(site.DB, 0600, nil)
if err != nil { if err != nil {
return err return err
} }
dbOpened = true }
return nil
}
func closeDatabase() error {
dbOpened -= 1
if dbOpened == 0 {
return db.CloseDB()
} }
return nil return nil
} }
func initDatabase() error { 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 // Create the path to the bucket to store admin users
if err := db.MkBucketPath([]string{"users"}); err != nil { if err := db.MkBucketPath([]string{"users"}); err != nil {
return err return err
@ -37,10 +50,11 @@ func initDatabase() error {
} }
func dbSetCurrentJam(name string) error { func dbSetCurrentJam(name string) error {
if err := db.OpenDB(); err != nil { var err error
if err = openDatabase(); err != nil {
return err return err
} }
defer db.CloseDB() defer closeDatabase()
return db.SetValue([]string{"site"}, "current-jam", name) return db.SetValue([]string{"site"}, "current-jam", name)
} }
@ -56,10 +70,10 @@ func dbHasCurrentJam() bool {
func dbGetCurrentJam() (string, error) { func dbGetCurrentJam() (string, error) {
var ret string var ret string
var err error var err error
if err = db.OpenDB(); err != nil { if err = openDatabase(); err != nil {
return "", err return "", err
} }
defer db.CloseDB() defer closeDatabase()
ret, err = db.GetValue([]string{"site"}, "current-jam") ret, err = db.GetValue([]string{"site"}, "current-jam")

View File

@ -2,6 +2,7 @@ package main
import ( import (
"errors" "errors"
"fmt"
"github.com/pborman/uuid" "github.com/pborman/uuid"
) )
@ -23,10 +24,10 @@ type TeamMember struct {
func dbCreateNewTeam(nm string) error { func dbCreateNewTeam(nm string) error {
var err error var err error
if err = db.OpenDB(); err != nil { if err = openDatabase(); err != nil {
return err return err
} }
defer db.CloseDB() defer closeDatabase()
// Generate a UUID // Generate a UUID
uuid := uuid.New() uuid := uuid.New()
@ -53,10 +54,10 @@ func dbCreateNewTeam(nm string) error {
func dbIsValidTeam(id string) bool { func dbIsValidTeam(id string) bool {
var err error var err error
if err = db.OpenDB(); err != nil { if err = openDatabase(); err != nil {
return false return false
} }
defer db.CloseDB() defer closeDatabase()
teamPath := []string{"teams"} teamPath := []string{"teams"}
if teamUids, err := db.GetBucketList(teamPath); err == nil { if teamUids, err := db.GetBucketList(teamPath); err == nil {
@ -72,10 +73,10 @@ func dbIsValidTeam(id string) bool {
func dbGetAllTeams() []Team { func dbGetAllTeams() []Team {
var ret []Team var ret []Team
var err error var err error
if err = db.OpenDB(); err != nil { if err = openDatabase(); err != nil {
return ret return ret
} }
defer db.CloseDB() defer closeDatabase()
teamPath := []string{"teams"} teamPath := []string{"teams"}
var teamUids []string var teamUids []string
@ -92,10 +93,10 @@ func dbGetAllTeams() []Team {
func dbGetTeam(id string) *Team { func dbGetTeam(id string) *Team {
var err error var err error
if err = db.OpenDB(); err != nil { if err = openDatabase(); err != nil {
return nil return nil
} }
defer db.CloseDB() defer closeDatabase()
teamPath := []string{"teams", id} teamPath := []string{"teams", id}
tm := new(Team) tm := new(Team)
@ -103,15 +104,16 @@ func dbGetTeam(id string) *Team {
if tm.Name, err = db.GetValue(teamPath, "name"); err != nil { if tm.Name, err = db.GetValue(teamPath, "name"); err != nil {
return nil return nil
} }
tm.Members, _ = dbGetTeamMembers(id)
return tm return tm
} }
func dbGetTeamByName(nm string) *Team { func dbGetTeamByName(nm string) *Team {
var err error var err error
if err = db.OpenDB(); err != nil { if err = openDatabase(); err != nil {
return nil return nil
} }
defer db.CloseDB() defer closeDatabase()
teamPath := []string{"teams"} teamPath := []string{"teams"}
var teamUids []string var teamUids []string
@ -128,10 +130,10 @@ func dbGetTeamByName(nm string) *Team {
func dbUpdateTeam(id string, tm *Team) error { func dbUpdateTeam(id string, tm *Team) error {
var err error var err error
if err = db.OpenDB(); err != nil { if err = openDatabase(); err != nil {
return nil return err
} }
defer db.CloseDB() defer closeDatabase()
teamPath := []string{"teams", id} teamPath := []string{"teams", id}
return db.SetValue(teamPath, "name", tm.Name) return db.SetValue(teamPath, "name", tm.Name)
@ -139,10 +141,10 @@ func dbUpdateTeam(id string, tm *Team) error {
func dbDeleteTeam(id string) error { func dbDeleteTeam(id string) error {
var err error var err error
if err = db.OpenDB(); err != nil { if err = openDatabase(); err != nil {
return err return err
} }
defer db.CloseDB() defer closeDatabase()
teamPath := []string{"teams"} teamPath := []string{"teams"}
return db.DeleteBucket(teamPath, id) return db.DeleteBucket(teamPath, id)
@ -150,63 +152,159 @@ func dbDeleteTeam(id string) error {
func dbEditTeamGame(teamid, name string) error { func dbEditTeamGame(teamid, name string) error {
var err error var err error
if err = db.OpenDB(); err != nil { if err = openDatabase(); err != nil {
return err return err
} }
defer db.CloseDB() defer closeDatabase()
gamePath := []string{"teams", teamid, "game"} gamePath := []string{"teams", teamid, "game"}
return db.SetValue(gamePath, "name", name) 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) { func dbGetTeamMembers(teamid string) ([]TeamMember, error) {
var ret []TeamMember var ret []TeamMember
var err error var err error
if err = db.OpenDB(); err != nil { if err = openDatabase(); err != nil {
return ret, nil return ret, err
} }
defer db.CloseDB() defer closeDatabase()
teamMbrPath := []string{"teams", teamid, "members"} teamPath := []string{"teams", teamid, "members"}
var memberUuids []string var memberUuids []string
if memberUuids, err = db.GetBucketList(teamMbrPath); err != nil { if memberUuids, err = db.GetBucketList(teamPath); err == nil {
for _, v := range memberUuids { for _, v := range memberUuids {
var mbr *TeamMember 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) ret = append(ret, *mbr)
} }
} }
} else {
fmt.Println(err.Error())
} }
return ret, nil return ret, nil
} }
func dbGetTeamMember(teamid, mbrid string) (*TeamMember, error) { func dbGetTeamMember(teamid, mbrid string) (*TeamMember, error) {
var err error var err error
if err = db.OpenDB(); err != nil { if err = openDatabase(); err != nil {
return nil, err return nil, err
} }
defer db.CloseDB() defer closeDatabase()
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 := new(TeamMember)
mbr.UUID = v teamMbrPath := []string{"teams", teamid, "members", mbrid}
if mbr.Name, err = db.GetValue(append(teamMbrPath, v), "name"); err != nil { mbr.UUID = mbrid
if mbr.Name, err = db.GetValue(teamMbrPath, "name"); err != nil {
return nil, err return nil, err
} }
if mbr.SlackId, err = db.GetValue(append(teamMbrPath, v), "slackid"); err != nil { if mbr.SlackId, err = db.GetValue(teamMbrPath, "slackid"); err != nil {
return nil, err return nil, err
} }
if mbr.Twitter, err = db.GetValue(append(teamMbrPath, v), "twitter"); err != nil { if mbr.Twitter, err = db.GetValue(teamMbrPath, "twitter"); err != nil {
return nil, err return nil, err
} }
if mbr.Email, err = db.GetValue(append(teamMbrPath, v), "email"); err != nil { if mbr.Email, err = db.GetValue(teamMbrPath, "email"); err != nil {
return nil, err return nil, err
} }
return mbr, 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
} }
return nil, errors.New("Couldn't find team member") 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
}
}
}
}
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)
} }

View File

@ -1,3 +1,4 @@
{{ $uuid := .TemplateData.UUID }}
<div class="center"> <div class="center">
<form class="pure-form pure-form-aligned" action="/admin/teams/{{ .TemplateData.UUID }}/save" method="POST"> <form class="pure-form pure-form-aligned" action="/admin/teams/{{ .TemplateData.UUID }}/save" method="POST">
<fieldset> <fieldset>
@ -19,8 +20,6 @@
</form> </form>
<h2>Members</h2> <h2>Members</h2>
<form class="pure-form pure-form-aligned" action="/admin/teams/{{ .TemplateData.UUID }}/savemember" method="POST">
<fieldset>
<table> <table>
<thead> <thead>
<tr> <tr>
@ -28,37 +27,44 @@
<th>Slack ID</th> <th>Slack ID</th>
<th>Twitter</th> <th>Twitter</th>
<th>Email</th> <th>Email</th>
<th></th> <th>Edit</th>
<th>Remove</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{{ range $i, $v := .TemplateData.Members }} {{ range $i, $v := .TemplateData.Members }}
<tr> <tr>
<td>{{ $v.Name }}</td> <td>{{ $v.Name }}</td>
<td>{{ $v.SlackID }}</td> <td>{{ $v.SlackId }}</td>
<td>{{ $v.Twitter }}</td> <td>{{ $v.Twitter }}</td>
<td>{{ $v.Email }}</td> <td>{{ $v.Email }}</td>
<td> <td>
<a href="/admin/teams/{{ $v.UUID }}/edit" class="pure-button pure-button-plain"><i class="fa fa-pencil"></i></a> <a href="/admin/teams/{{ $v.UUID }}/edit" class="pure-button pure-button-plain"><i class="fa fa-pencil"></i></a>
<a href="/admin/teams/{{ $v.UUID }}/delete" class="pure-button pure-button-plain"><i class="fa fa-trash"></i></a> </td>
<td>
<form action="/admin/teams/{{ $uuid }}/deletemember" method="POST">
<input type="hidden" name="memberid" value="{{ $v.UUID }}"/>
<button type="submit" class="pure-button pure-button-plain"><i class="fa fa-trash"></i></button>
</form>
</td> </td>
</tr> </tr>
{{ end }} {{ end }}
<tr> <tr>
<td colspan="5" class="center">Add a new member</td> <td colspan="6" class="center">Add a new member</td>
</tr> </tr>
<tr> <tr>
<td><input id="newmembername" name="newmembername" value="" placeholder="Member Name" /></td> <td colspan="6">
<td><input id="newmemberslackid" name="newmemberslackid" value="" placeholder="@SlackID" /></td> <form action="/admin/teams/{{ $uuid }}/savemember" method="POST">
<td><input id="newmembertwitter" name="newmembertwitter" value="" placeholder="@Twitter" /></td> <input id="newmembername" name="newmembername" value="" placeholder="Member Name" />
<td><input id="newmemberemail" name="newmemberemail" value="" placeholder="user@email.com" /></td> <input id="newmemberslackid" name="newmemberslackid" value="" placeholder="@SlackID" />
<td><button type="submit" class="pull-right space pure-button pure-button-primary">Add</button></td> <input id="newmembertwitter" name="newmembertwitter" value="" placeholder="@Twitter" />
<input id="newmemberemail" name="newmemberemail" value="" placeholder="user@email.com" />
<button type="submit" class="pull-right space pure-button pure-button-primary">Add</button>
</form>
</td>
</tr> </tr>
</tbody> </tbody>
</table> </table>
</fieldset>
</form>
</div> </div>
<script> <script>
snack.listener( snack.listener(
@ -66,7 +72,7 @@
function() { function() {
showModal({ showModal({
title: 'Delete Team', title: 'Delete Team',
subtitle: '({{ .TemplateData.Name }} - {{ .TemplateData.UUID }})', subtitle: '({{ .TemplateData.Name }} - {{ $uuid}})',
body: 'Are you sure? This cannot be undone.', body: 'Are you sure? This cannot be undone.',
buttons: [{ buttons: [{
title:'Cancel', title:'Cancel',
@ -76,7 +82,7 @@
title:'Delete', title:'Delete',
position:'right', position:'right',
class: 'pure-button-error', class: 'pure-button-error',
href: '/admin/teams/{{ .TemplateData.UUID }}/delete' href: '/admin/teams/{{ $uuid }}/delete'
}] }]
}); });
} }