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 ( import (
"context" "context"
"errors" "errors"
"fmt"
"log" "log"
"os" "os"
@ -19,6 +20,8 @@ type Store struct {
} }
func NewStore(filename string, genString GenString) (*Store, error) { 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 var needCreate bool
if _, err := os.Stat(filename); os.IsNotExist(err) { if _, err := os.Stat(filename); os.IsNotExist(err) {
needCreate = true needCreate = true
@ -275,6 +278,7 @@ type CreateEventResponseResult struct {
func (s *Store) CreateEventResponse(ctx context.Context, cmd CreateEventResponseCommand) (result CreateEventResponseResult, err error) { func (s *Store) CreateEventResponse(ctx context.Context, cmd CreateEventResponseCommand) (result CreateEventResponseResult, err error) {
const responseAlphaIDLength = 10 const responseAlphaIDLength = 10
fmt.Printf("creating response: %+v\n", cmd)
conn := s.pool.Get(ctx) conn := s.pool.Get(ctx)
defer s.pool.Put(conn) defer s.pool.Put(conn)

View File

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