Finish event response input

This commit is contained in:
Brandon Dyck 2020-09-27 20:25:21 -06:00
parent d1c4c8ab1d
commit a64df092a5
2 changed files with 30 additions and 21 deletions

View File

@ -3,6 +3,7 @@ package back
import (
"context"
"errors"
"fmt"
"log"
"os"
@ -19,6 +20,8 @@ type Store struct {
}
func NewStore(filename string, genString GenString) (*Store, error) {
// TODO This stat needs to change, though I can't remember how, now that
// SQLite has been updated.
var needCreate bool
if _, err := os.Stat(filename); os.IsNotExist(err) {
needCreate = true
@ -275,6 +278,7 @@ type CreateEventResponseResult struct {
func (s *Store) CreateEventResponse(ctx context.Context, cmd CreateEventResponseCommand) (result CreateEventResponseResult, err error) {
const responseAlphaIDLength = 10
fmt.Printf("creating response: %+v\n", cmd)
conn := s.pool.Get(ctx)
defer s.pool.Put(conn)

View File

@ -95,7 +95,7 @@ func voteForm(disabled bool, st voteState) hm.Term {
for _, day := range dates {
value := fmt.Sprintf("%8s-%02d", day.Format("20060102"), hour)
row = append(row, e.Td()(e.Input(
a.Name("slot"),
a.Name(fieldNameDateHour),
a.Value(value),
a.Type("checkbox"),
a.Disabled(disabled),
@ -104,7 +104,7 @@ func voteForm(disabled bool, st voteState) hm.Term {
rows = append(rows, e.Tr()(row))
}
return e.Form(a.Method(http.MethodPost), a.Action(pathDoVote))(
e.Input(a.Type("hidden"), a.Name(keyEventID), a.Value(st.eventAlphaID)),
e.Input(a.Type("hidden"), a.Name(fieldNameEventID), a.Value(st.eventAlphaID)),
e.Label(a.For(fieldNameGuestName))(hm.Text("What's your name?")),
e.Br(),
e.Input(a.Name(fieldNameGuestName), a.Size(40), a.Value(st.name), a.Disabled(disabled)),
@ -117,9 +117,9 @@ func voteForm(disabled bool, st voteState) hm.Term {
}
func (h *handler) handleVote(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
eventAlphaID := r.URL.Query().Get(fieldNameEventID)
event, err := h.store.GetEventMetadata(context.Background(), back.GetEventMetadataQuery{
AlphaID: query.Get(keyEventID),
AlphaID: eventAlphaID,
})
// TODO return 404 if event not found
if err != nil {
@ -128,6 +128,7 @@ func (h *handler) handleVote(w http.ResponseWriter, r *http.Request) {
}
state := voteState{
eventAlphaID: eventAlphaID,
earliest: event.Earliest,
latest: event.Latest,
}
@ -146,14 +147,17 @@ func (h *handler) handleDoVote(w http.ResponseWriter, r *http.Request) {
return
}
fmt.Printf("received form: %+v\n", r.Form)
cmd := back.CreateEventResponseCommand{
EventAlphaID: r.URL.Query().Get(keyEventID),
EventAlphaID: r.Form.Get(fieldNameEventID),
GuestName: r.Form.Get(fieldNameGuestName),
DateHours: make(map[date.Date]map[int]struct{}),
}
for _, slot := range r.PostForm["slot"] {
for _, dateHourString := range r.Form[fieldNameDateHour] {
var dateString string
var hour int
_, err := fmt.Sscanf(slot, "%8s-%02d", &dateString, &hour)
_, err := fmt.Sscanf(dateHourString, "%8s-%02d", &dateString, &hour)
if err != nil {
log.Println(err)
http.Error(w, "invalid form values", http.StatusBadRequest)
@ -165,6 +169,7 @@ func (h *handler) handleDoVote(w http.ResponseWriter, r *http.Request) {
http.Error(w, "invalid form values", http.StatusBadRequest)
return
}
fmt.Println("parsed date/hour:", d, hour)
if cmd.DateHours[d] == nil {
cmd.DateHours[d] = make(map[int]struct{})
}
@ -250,12 +255,12 @@ func (h *handler) handleDoCreate(w http.ResponseWriter, r *http.Request) {
return
}
var adminQuery = make(url.Values)
adminQuery.Add(keyEventID, event.AlphaID)
adminQuery.Add(keyAdminCode, event.AdminCode)
adminQuery.Add(fieldNameEventID, event.AlphaID)
adminQuery.Add(fieldNameAdminCode, event.AdminCode)
adminURL := h.baseURL + pathAdmin + "?" + adminQuery.Encode()
var voteQuery = make(url.Values)
voteQuery.Add(keyEventID, event.AlphaID)
voteQuery.Add(fieldNameEventID, event.AlphaID)
voteURL := h.baseURL + pathVote + "?" + voteQuery.Encode()
const dateDisplayFmt = "Monday, January 2, 2006"
@ -289,7 +294,7 @@ func (h *handler) handleDoCreate(w http.ResponseWriter, r *http.Request) {
func (h *handler) handleAdmin(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
eventID := query.Get(keyEventID)
eventID := query.Get(fieldNameEventID)
// TODO authenticate with admin code
metadata, err := h.store.GetEventMetadata(context.Background(), back.GetEventMetadataQuery{
AlphaID: eventID,
@ -370,14 +375,14 @@ func (h *handler) writePage(w io.Writer, title string, contents hm.Term) error {
}
const (
fieldNameEarliest = "earliestDate"
fieldNameLatest = "latestDate"
fieldNameEventName = "eventName"
fieldNameDescription = "eventDescription"
fieldNameGuestName = "guestName"
fieldNameDateHour = "date_hour"
fieldNameEarliest = "earliest_date"
fieldNameLatest = "latest_date"
fieldNameEventName = "event_name"
fieldNameDescription = "event_description"
fieldNameGuestName = "guest_name"
fieldNameEventID = "event_id"
fieldNameAdminCode = "admin_code"
)
const keyEventID = "event_id"
const keyAdminCode = "admin_code"
const formDateLayout = "2006-01-02"