From a64df092a5ccdafe9dcd9bf70cc14630c416d5e9 Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Sun, 27 Sep 2020 20:25:21 -0600 Subject: [PATCH] Finish event response input --- back/store.go | 4 ++++ front/server.go | 47 ++++++++++++++++++++++++++--------------------- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/back/store.go b/back/store.go index 753250b..3a48db7 100644 --- a/back/store.go +++ b/back/store.go @@ -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) diff --git a/front/server.go b/front/server.go index 6e4ced0..7b5ef38 100644 --- a/front/server.go +++ b/front/server.go @@ -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,8 +128,9 @@ func (h *handler) handleVote(w http.ResponseWriter, r *http.Request) { } state := voteState{ - earliest: event.Earliest, - latest: event.Latest, + eventAlphaID: eventAlphaID, + earliest: event.Earliest, + latest: event.Latest, } body := hm.Terms{ e.P()(hm.Text(event.Description)), @@ -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"