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)
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"

View File

@ -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")

View File

@ -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()
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 {
teamMbrPath := []string{"teams", teamid, "members", mbrid}
mbr.UUID = mbrid
if mbr.Name, err = db.GetValue(teamMbrPath, "name"); err != nil {
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
}
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
}
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 mbr, err
}
}
return nil, errors.New("Couldn't find team member")
}
// 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
}
}
}
}
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">
<form class="pure-form pure-form-aligned" action="/admin/teams/{{ .TemplateData.UUID }}/save" method="POST">
<fieldset>
@ -19,8 +20,6 @@
</form>
<h2>Members</h2>
<form class="pure-form pure-form-aligned" action="/admin/teams/{{ .TemplateData.UUID }}/savemember" method="POST">
<fieldset>
<table>
<thead>
<tr>
@ -28,37 +27,44 @@
<th>Slack ID</th>
<th>Twitter</th>
<th>Email</th>
<th></th>
<th>Edit</th>
<th>Remove</th>
</tr>
</thead>
<tbody>
{{ range $i, $v := .TemplateData.Members }}
<tr>
<td>{{ $v.Name }}</td>
<td>{{ $v.SlackID }}</td>
<td>{{ $v.SlackId }}</td>
<td>{{ $v.Twitter }}</td>
<td>{{ $v.Email }}</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 }}/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>
</tr>
{{ end }}
<tr>
<td colspan="5" class="center">Add a new member</td>
<td colspan="6" class="center">Add a new member</td>
</tr>
<tr>
<td><input id="newmembername" name="newmembername" value="" placeholder="Member Name" /></td>
<td><input id="newmemberslackid" name="newmemberslackid" value="" placeholder="@SlackID" /></td>
<td><input id="newmembertwitter" name="newmembertwitter" value="" placeholder="@Twitter" /></td>
<td><input id="newmemberemail" name="newmemberemail" value="" placeholder="user@email.com" /></td>
<td><button type="submit" class="pull-right space pure-button pure-button-primary">Add</button></td>
<td colspan="6">
<form action="/admin/teams/{{ $uuid }}/savemember" method="POST">
<input id="newmembername" name="newmembername" value="" placeholder="Member Name" />
<input id="newmemberslackid" name="newmemberslackid" value="" placeholder="@SlackID" />
<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>
</tbody>
</table>
</fieldset>
</form>
</div>
<script>
snack.listener(
@ -66,7 +72,7 @@
function() {
showModal({
title: 'Delete Team',
subtitle: '({{ .TemplateData.Name }} - {{ .TemplateData.UUID }})',
subtitle: '({{ .TemplateData.Name }} - {{ $uuid}})',
body: 'Are you sure? This cannot be undone.',
buttons: [{
title:'Cancel',
@ -76,7 +82,7 @@
title:'Delete',
position:'right',
class: 'pure-button-error',
href: '/admin/teams/{{ .TemplateData.UUID }}/delete'
href: '/admin/teams/{{ $uuid }}/delete'
}]
});
}