Skip to content

Commit

Permalink
Add page to customize playoff break descriptions (closes #171).
Browse files Browse the repository at this point in the history
  • Loading branch information
patfair committed May 29, 2024
1 parent d1c63e9 commit f4adccc
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 1 deletion.
8 changes: 8 additions & 0 deletions model/scheduled_break.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ func (database *Database) CreateScheduledBreak(scheduledBreak *ScheduledBreak) e
return database.scheduledBreakTable.create(scheduledBreak)
}

func (database *Database) GetScheduledBreakById(id int) (*ScheduledBreak, error) {
return database.scheduledBreakTable.getById(id)
}

func (database *Database) UpdateScheduledBreak(scheduledBreak *ScheduledBreak) error {
return database.scheduledBreakTable.update(scheduledBreak)
}

func (database *Database) GetScheduledBreaksByMatchType(matchType MatchType) ([]ScheduledBreak, error) {
scheduledBreaks, err := database.scheduledBreakTable.getAll()
if err != nil {
Expand Down
17 changes: 16 additions & 1 deletion model/scheduled_break_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ func TestScheduledBreakCrud(t *testing.T) {
scheduledBreak3 := ScheduledBreak{0, Playoff, 4, time.Unix(500, 0).UTC(), 900, "Awards"}
assert.Nil(t, db.CreateScheduledBreak(&scheduledBreak3))

// Test retrieval by ID.
scheduledBreak, err := db.GetScheduledBreakById(1)
assert.Nil(t, err)
assert.Equal(t, scheduledBreak1, *scheduledBreak)
scheduledBreak, err = db.GetScheduledBreakById(2)
assert.Nil(t, err)
assert.Equal(t, scheduledBreak2, *scheduledBreak)

// Test update.
scheduledBreak2.Description = "Brunch"
assert.Nil(t, db.UpdateScheduledBreak(&scheduledBreak2))
scheduledBreak, err = db.GetScheduledBreakById(2)
assert.Nil(t, err)
assert.Equal(t, scheduledBreak2, *scheduledBreak)

// Test retrieval of all blocks by match type.
scheduledBreaks, err := db.GetScheduledBreaksByMatchType(Practice)
assert.Nil(t, err)
Expand All @@ -37,7 +52,7 @@ func TestScheduledBreakCrud(t *testing.T) {
}

// Test individual retrieval by match type and order.
scheduledBreak, err := db.GetScheduledBreakByMatchTypeOrder(Qualification, 25)
scheduledBreak, err = db.GetScheduledBreakByMatchTypeOrder(Qualification, 25)
assert.Nil(t, err)
assert.Equal(t, scheduledBreak2, *scheduledBreak)
scheduledBreak, err = db.GetScheduledBreakByMatchTypeOrder(Playoff, 4)
Expand Down
1 change: 1 addition & 0 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<a class="dropdown-item" href="/setup/awards">Awards</a>
<a class="dropdown-item" href="/setup/lower_thirds">Lower Thirds</a>
<a class="dropdown-item" href="/setup/sponsor_slides">Sponsor Slides</a>
<a class="dropdown-item" href="/setup/breaks">Scheduled Breaks</a>
<a class="dropdown-item" href="/setup/displays">Display Configuration</a>
<a class="dropdown-item" href="/setup/field_testing">Field Testing</a>
</div>
Expand Down
42 changes: 42 additions & 0 deletions templates/setup_breaks.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{{/*
Copyright 2024 Team 254. All Rights Reserved.
Author: [email protected] (Patrick Fairbank)

UI for configuring scheduled breaks.
*/}}
{{define "title"}}Breaks Configuration{{end}}
{{define "body"}}
<div class="row justify-content-center">
<div class="col-lg-6">
<div class="card card-body bg-body-tertiary">
<legend>Scheduled Break Configuration</legend>
{{if not .ScheduledBreaks}}
<p>
Playoff breaks won't appear here until alliance selection is complete and the playoff tournament is created.
</p>
{{end}}
{{range $i, $scheduledBreak := .ScheduledBreaks}}
<form method="POST">
<h5>Break #{{add $i 1}}</h5>
<input type="hidden" name="id" value="{{$scheduledBreak.Id}}" />
<div class="row mb-3">
<div class="col-lg-3">
<label class="control-label">Description</label>
</div>
<div class="col-lg-6">
<input type="text" class="form-control" name="description" value="{{$scheduledBreak.Description}}">
</div>
<div class="col-lg-3">
<button type="submit" class="btn btn-primary btn-lower-third mb-1" name="action" value="save">
Save
</button>
</div>
</div>
</form>
{{end}}
</div>
</div>
</div>
{{end}}
{{define "script"}}
{{end}}
60 changes: 60 additions & 0 deletions web/setup_breaks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2024 Team 254. All Rights Reserved.
// Author: [email protected] (Patrick Fairbank)
//
// Web routes for managing scheduled breaks.

package web

import (
"github.com/Team254/cheesy-arena/model"
"net/http"
"strconv"
)

// Shows the breaks configuration page.
func (web *Web) breaksGetHandler(w http.ResponseWriter, r *http.Request) {
if !web.userIsAdmin(w, r) {
return
}

template, err := web.parseFiles("templates/setup_breaks.html", "templates/base.html")
if err != nil {
handleWebErr(w, err)
return
}
breaks, err := web.arena.Database.GetScheduledBreaksByMatchType(model.Playoff)
if err != nil {
handleWebErr(w, err)
return
}
data := struct {
*model.EventSettings
ScheduledBreaks []model.ScheduledBreak
}{web.arena.EventSettings, breaks}
err = template.ExecuteTemplate(w, "base", data)
if err != nil {
handleWebErr(w, err)
return
}
}

// Saves the modified breaks to the database.
func (web *Web) breaksPostHandler(w http.ResponseWriter, r *http.Request) {
if !web.userIsAdmin(w, r) {
return
}

scheduledBreakId, _ := strconv.Atoi(r.PostFormValue("id"))
scheduledBreak, err := web.arena.Database.GetScheduledBreakById(scheduledBreakId)
if err != nil {
handleWebErr(w, err)
return
}
scheduledBreak.Description = r.PostFormValue("description")
if err = web.arena.Database.UpdateScheduledBreak(scheduledBreak); err != nil {
handleWebErr(w, err)
return
}

http.Redirect(w, r, "/setup/breaks", 303)
}
35 changes: 35 additions & 0 deletions web/setup_breaks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2024 Team 254. All Rights Reserved.
// Author: [email protected] (Patrick Fairbank)

package web

import (
"github.com/Team254/cheesy-arena/model"
"github.com/stretchr/testify/assert"
"testing"
"time"
)

func TestSetupBreaks(t *testing.T) {
web := setupTestWeb(t)

web.arena.Database.CreateScheduledBreak(
&model.ScheduledBreak{0, model.Playoff, 4, time.Unix(500, 0).UTC(), 900, "Field Break 1"},
)
web.arena.Database.CreateScheduledBreak(
&model.ScheduledBreak{0, model.Playoff, 4, time.Unix(500, 0).UTC(), 900, "Field Break 2"},
)

recorder := web.getHttpResponse("/setup/breaks")
assert.Equal(t, 200, recorder.Code)
assert.Contains(t, recorder.Body.String(), "Field Break 1")
assert.Contains(t, recorder.Body.String(), "Field Break 2")

recorder = web.postHttpResponse("/setup/breaks", "id=2&description=Award Break 3")
assert.Equal(t, 303, recorder.Code)
recorder = web.getHttpResponse("/setup/breaks")
assert.Equal(t, 200, recorder.Code)
assert.Contains(t, recorder.Body.String(), "Field Break 1")
assert.NotContains(t, recorder.Body.String(), "Field Break 2")
assert.Contains(t, recorder.Body.String(), "Award Break 3")
}
2 changes: 2 additions & 0 deletions web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ func (web *Web) newHandler() http.Handler {
mux.HandleFunc("GET /reports/pdf/teams", web.teamsPdfReportHandler)
mux.HandleFunc("GET /setup/awards", web.awardsGetHandler)
mux.HandleFunc("POST /setup/awards", web.awardsPostHandler)
mux.HandleFunc("GET /setup/breaks", web.breaksGetHandler)
mux.HandleFunc("POST /setup/breaks", web.breaksPostHandler)
mux.HandleFunc("POST /setup/db/clear", web.clearDbHandler)
mux.HandleFunc("POST /setup/db/restore", web.restoreDbHandler)
mux.HandleFunc("GET /setup/db/save", web.saveDbHandler)
Expand Down

0 comments on commit f4adccc

Please sign in to comment.