henwen/front/server.go

446 lines
12 KiB
Go
Raw Normal View History

2020-04-01 06:35:15 +00:00
package front
2020-03-24 14:59:27 +00:00
import (
2020-03-31 14:47:40 +00:00
"context"
2020-03-24 14:59:27 +00:00
"fmt"
"io"
2020-09-27 21:44:04 +00:00
"log"
2020-03-24 14:59:27 +00:00
"net/http"
2020-03-31 14:47:40 +00:00
"net/url"
2020-04-16 13:51:24 +00:00
"strconv"
2020-03-24 14:59:27 +00:00
2020-04-21 14:25:15 +00:00
"github.com/rickb777/date"
2020-04-01 05:49:08 +00:00
hm "gitlab.codemonkeysoftware.net/b/hatmill"
2020-03-24 14:59:27 +00:00
a "gitlab.codemonkeysoftware.net/b/hatmill/attribute"
e "gitlab.codemonkeysoftware.net/b/hatmill/element"
2020-04-01 06:35:15 +00:00
"gitlab.codemonkeysoftware.net/b/henwen/back"
2020-03-24 14:59:27 +00:00
)
const (
2020-09-28 03:11:06 +00:00
pathRoot = "/"
pathCreate = "/create"
pathDoCreate = "/create/do"
pathCreateSuccess = "/create/success"
pathAdmin = "/admin"
pathVote = "/vote"
pathDoVote = "/vote/do"
pathVoteSuccess = "/vote/success"
2020-03-24 14:59:27 +00:00
)
2020-04-01 05:31:30 +00:00
type handler struct {
mux *http.ServeMux
2020-04-01 06:35:15 +00:00
store *back.Store
title string
baseURL string
}
type HandlerParams struct {
Title string
2020-04-01 06:35:15 +00:00
Store *back.Store
BaseURL string
2020-04-01 05:31:30 +00:00
}
func NewHandler(params HandlerParams) http.Handler {
2020-04-01 05:31:30 +00:00
h := &handler{
store: params.Store,
title: params.Title,
baseURL: params.BaseURL,
mux: http.NewServeMux(),
2020-04-01 05:31:30 +00:00
}
h.mux.HandleFunc(pathRoot, h.handleRoot)
h.mux.HandleFunc(pathCreate, h.handleCreate)
h.mux.HandleFunc(pathDoCreate, h.handleDoCreate)
h.mux.HandleFunc(pathAdmin, h.handleAdmin)
2020-04-14 04:09:48 +00:00
h.mux.HandleFunc(pathVote, h.handleVote)
h.mux.HandleFunc(pathDoVote, h.handleDoVote)
2020-09-28 03:11:06 +00:00
h.mux.HandleFunc(pathVoteSuccess, h.handleVoteSuccess)
h.mux.HandleFunc(pathCreateSuccess, h.handleCreateSuccess)
2020-04-01 05:31:30 +00:00
return h
}
func (h *handler) handleRoot(w http.ResponseWriter, r *http.Request) {
2020-04-01 05:49:08 +00:00
body := hm.Terms{
e.A(a.Href(pathCreate))(
hm.Text("Create event"),
),
}
2020-04-14 05:43:04 +00:00
_ = h.writePage(w, "Welcome!", body)
2020-04-01 05:31:30 +00:00
}
2020-04-14 04:09:48 +00:00
var timeLabels = []string{
"12 AM", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11",
"12 PM", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11",
}
2020-04-14 04:37:36 +00:00
type voteState struct {
2020-09-27 21:44:04 +00:00
eventAlphaID string
2020-04-21 14:25:15 +00:00
name string
earliest, latest date.Date
2020-04-14 04:37:36 +00:00
}
func voteForm(disabled bool, st voteState) hm.Term {
2020-04-21 14:25:15 +00:00
dateSpan := st.latest.Sub(st.earliest)
var dates []date.Date
for offset := date.PeriodOfDays(0); offset <= dateSpan; offset++ {
dates = append(dates, st.earliest.Add(offset))
2020-04-14 05:00:37 +00:00
}
var ths = hm.Terms{e.Th()()}
for _, date := range dates {
ths = append(ths, e.Th()(hm.Text(date.Format("Jan 2"))))
2020-04-14 04:09:48 +00:00
}
2020-04-14 05:00:37 +00:00
var rows = hm.Terms{e.Thead()(ths)}
for hour := 0; hour < 24; hour++ {
2020-04-14 04:09:48 +00:00
var row = hm.Terms{
2020-04-14 05:00:37 +00:00
e.Td()(hm.Text(timeLabels[hour])),
2020-04-14 04:09:48 +00:00
}
2020-09-27 21:44:04 +00:00
for _, day := range dates {
value := fmt.Sprintf("%8s-%02d", day.Format("20060102"), hour)
row = append(row, e.Td()(e.Input(
2020-09-28 02:25:21 +00:00
a.Name(fieldNameDateHour),
2020-09-27 21:44:04 +00:00
a.Value(value),
a.Type("checkbox"),
a.Disabled(disabled),
)))
2020-04-14 04:09:48 +00:00
}
rows = append(rows, e.Tr()(row))
}
2020-04-14 04:37:36 +00:00
return e.Form(a.Method(http.MethodPost), a.Action(pathDoVote))(
2020-09-28 02:25:21 +00:00
e.Input(a.Type("hidden"), a.Name(fieldNameEventID), a.Value(st.eventAlphaID)),
2020-09-27 21:44:04 +00:00
e.Label(a.For(fieldNameGuestName))(hm.Text("What's your name?")),
2020-04-14 04:37:36 +00:00
e.Br(),
2020-09-27 21:44:04 +00:00
e.Input(a.Name(fieldNameGuestName), a.Size(40), a.Value(st.name), a.Disabled(disabled)),
2020-04-14 04:37:36 +00:00
e.Fieldset()(
e.Legend()(hm.Text("When are you available?")),
e.Table()(rows),
),
e.Input(a.Type("submit"), a.Value("Submit")),
)
}
2020-09-28 03:40:55 +00:00
func notFound(w http.ResponseWriter, err error, msg string) bool {
if err == back.ErrNotFound {
http.Error(w, msg, http.StatusNotFound)
return true
}
return false
}
func invalidForm(w http.ResponseWriter, err error) bool {
if err != nil {
http.Error(w,
"Invalid form values. That shouldn't have been possible. Please try again later.",
http.StatusBadRequest)
logError(err)
return true
}
return false
}
func logError(err error) {
log.Println("ERROR:", err.Error())
}
func internalServerError(w http.ResponseWriter, err error) bool {
if err != nil {
http.Error(w, "Something went wrong. Please try again later.", http.StatusInternalServerError)
logError(err)
return true
}
return false
}
2020-04-14 04:37:36 +00:00
func (h *handler) handleVote(w http.ResponseWriter, r *http.Request) {
2020-09-28 02:25:21 +00:00
eventAlphaID := r.URL.Query().Get(fieldNameEventID)
2020-04-16 13:51:24 +00:00
event, err := h.store.GetEventMetadata(context.Background(), back.GetEventMetadataQuery{
2020-09-28 02:25:21 +00:00
AlphaID: eventAlphaID,
2020-04-14 05:29:40 +00:00
})
2020-09-28 03:40:55 +00:00
if notFound(w, err, "Event not found") || internalServerError(w, err) {
2020-04-14 05:29:40 +00:00
return
}
2020-04-14 05:00:37 +00:00
state := voteState{
2020-09-28 02:25:21 +00:00
eventAlphaID: eventAlphaID,
earliest: event.Earliest,
latest: event.Latest,
2020-04-14 05:00:37 +00:00
}
2020-04-14 04:09:48 +00:00
body := hm.Terms{
2020-04-14 05:29:40 +00:00
e.P()(hm.Text(event.Description)),
2020-04-14 05:00:37 +00:00
voteForm(false, state),
2020-04-14 04:09:48 +00:00
}
2020-04-14 05:43:04 +00:00
_ = h.writePage(w, event.Name, body)
2020-04-14 04:09:48 +00:00
}
func (h *handler) handleDoVote(w http.ResponseWriter, r *http.Request) {
2020-09-27 21:44:04 +00:00
err := r.ParseForm()
2020-09-28 03:40:55 +00:00
if invalidForm(w, err) {
2020-09-27 21:44:04 +00:00
return
}
2020-09-28 02:25:21 +00:00
fmt.Printf("received form: %+v\n", r.Form)
2020-09-27 21:44:04 +00:00
cmd := back.CreateEventResponseCommand{
2020-09-28 02:25:21 +00:00
EventAlphaID: r.Form.Get(fieldNameEventID),
GuestName: r.Form.Get(fieldNameGuestName),
2020-09-27 21:44:04 +00:00
DateHours: make(map[date.Date]map[int]struct{}),
}
2020-09-28 02:25:21 +00:00
for _, dateHourString := range r.Form[fieldNameDateHour] {
2020-09-27 21:44:04 +00:00
var dateString string
var hour int
2020-09-28 02:25:21 +00:00
_, err := fmt.Sscanf(dateHourString, "%8s-%02d", &dateString, &hour)
2020-09-28 03:40:55 +00:00
if invalidForm(w, err) {
2020-09-27 21:44:04 +00:00
return
}
d, err := date.Parse("20060102", dateString)
2020-09-28 03:40:55 +00:00
if invalidForm(w, err) {
2020-09-27 21:44:04 +00:00
return
}
if cmd.DateHours[d] == nil {
cmd.DateHours[d] = make(map[int]struct{})
}
cmd.DateHours[d][hour] = struct{}{}
}
2020-09-28 03:11:06 +00:00
eventResponse, err := h.store.CreateEventResponse(r.Context(), cmd)
2020-09-28 03:40:55 +00:00
if notFound(w, err, "Event not found") || internalServerError(w, err) {
2020-09-27 21:44:04 +00:00
return
}
2020-09-28 03:11:06 +00:00
var successQuery = make(url.Values)
successQuery.Add(fieldNameEventID, cmd.EventAlphaID)
successQuery.Add(fieldNameResponseID, eventResponse.ResponseAlphaID)
http.Redirect(w, r, pathVoteSuccess+"?"+successQuery.Encode(), http.StatusSeeOther)
}
func (h *handler) handleVoteSuccess(w http.ResponseWriter, r *http.Request) {
eventResponse, err := h.store.GetEventResponse(r.Context(), back.GetEventResponseQuery{
EventAlphaID: r.URL.Query().Get(fieldNameEventID),
ResponseAlphaID: r.URL.Query().Get(fieldNameResponseID),
})
2020-09-28 03:40:55 +00:00
if notFound(w, err, "Event response not found.") || internalServerError(w, err) {
2020-09-28 03:11:06 +00:00
return
}
event, err := h.store.GetEventMetadata(r.Context(), back.GetEventMetadataQuery{
AlphaID: r.URL.Query().Get(fieldNameEventID),
})
2020-09-28 03:40:55 +00:00
if notFound(w, err, "Event not found") || internalServerError(w, err) {
2020-09-28 03:11:06 +00:00
return
}
2020-04-14 05:43:04 +00:00
2020-04-14 05:00:37 +00:00
state := voteState{
2020-09-28 03:11:06 +00:00
name: eventResponse.GuestName,
earliest: event.Earliest,
latest: event.Latest,
2020-04-14 05:00:37 +00:00
}
2020-04-14 04:09:48 +00:00
body := hm.Terms{
2020-09-28 03:11:06 +00:00
e.P()(hm.Text(event.Description)),
2020-04-14 04:09:48 +00:00
e.H3()(hm.Text("Thanks for voting!")),
e.P()(
hm.Text("You can edit your response anytime at "),
e.A(a.Href("#"))(hm.Text("this link")),
hm.Text("."),
),
2020-04-14 05:00:37 +00:00
voteForm(true, state),
2020-04-14 04:09:48 +00:00
}
2020-09-28 03:11:06 +00:00
_ = h.writePage(w, event.Name, body)
2020-04-14 04:09:48 +00:00
}
2020-04-01 05:31:30 +00:00
func (h *handler) handleCreate(w http.ResponseWriter, r *http.Request) {
2020-04-01 05:49:08 +00:00
body := hm.Terms{
e.Form(a.Action(pathDoCreate), a.Method(http.MethodPost))(
e.Label(a.For(fieldNameEventName))(hm.Text("Event name")),
e.Input(a.Name(fieldNameEventName)),
e.Label(a.For(fieldNameDescription))(hm.Text("Description")),
e.Textarea(a.Name(fieldNameDescription), a.Placeholder("What's going on?"))(),
e.Label(a.For(fieldNameEarliest))(hm.Text("Earliest date")),
e.Input(a.Name(fieldNameEarliest), a.Type("date")),
e.Label(a.For(fieldNameLatest))(hm.Text("Latest date")),
e.Input(a.Name(fieldNameLatest), a.Type("date")),
e.Input(a.Type("submit")),
),
}
2020-04-14 05:43:04 +00:00
_ = h.writePage(w, "Create an event", body)
2020-04-01 05:31:30 +00:00
}
func (h *handler) handleDoCreate(w http.ResponseWriter, r *http.Request) {
2020-04-14 05:43:04 +00:00
// TODO consider redirecting to admin
2020-04-21 14:25:15 +00:00
earliest, err := date.Parse(formDateLayout, r.FormValue(fieldNameEarliest))
2020-09-28 03:40:55 +00:00
if invalidForm(w, err) {
2020-04-01 05:31:30 +00:00
return
}
2020-04-21 14:25:15 +00:00
latest, err := date.Parse(formDateLayout, r.FormValue(fieldNameLatest))
2020-09-28 03:40:55 +00:00
if invalidForm(w, err) {
2020-04-01 05:31:30 +00:00
return
}
eventName := r.FormValue(fieldNameEventName)
if eventName == "" {
2020-09-28 03:40:55 +00:00
fmt.Fprint(w, "Event name is required")
2020-04-01 05:31:30 +00:00
return
}
description := r.FormValue(fieldNameDescription)
2020-04-01 06:35:15 +00:00
event, err := h.store.CreateEvent(context.Background(), back.CreateEventCommand{
2020-04-21 14:25:15 +00:00
Name: eventName,
Description: description,
Earliest: earliest,
Latest: latest,
2020-04-01 05:49:08 +00:00
})
if err != nil {
fmt.Fprint(w, err)
return
2020-03-24 14:59:27 +00:00
}
2020-09-28 03:11:06 +00:00
var successQuery = make(url.Values)
successQuery.Add(fieldNameEventID, event.AlphaID)
successQuery.Add(fieldNameAdminCode, event.AdminCode)
http.Redirect(w, r, pathCreateSuccess+"?"+successQuery.Encode(), http.StatusSeeOther)
}
func (h *handler) handleCreateSuccess(w http.ResponseWriter, r *http.Request) {
eventID := r.URL.Query().Get(fieldNameEventID)
event, err := h.store.GetEventMetadata(r.Context(), back.GetEventMetadataQuery{
AlphaID: eventID,
})
2020-09-28 03:40:55 +00:00
if notFound(w, err, "Event not found") || internalServerError(w, err) {
2020-09-28 03:11:06 +00:00
return
}
2020-04-01 05:49:08 +00:00
var adminQuery = make(url.Values)
2020-09-28 03:11:06 +00:00
adminQuery.Add(fieldNameEventID, eventID)
adminQuery.Add(fieldNameAdminCode, r.URL.Query().Get(fieldNameAdminCode))
adminURL := h.baseURL + pathAdmin + "?" + adminQuery.Encode()
2020-03-24 14:59:27 +00:00
2020-04-14 05:29:40 +00:00
var voteQuery = make(url.Values)
2020-09-28 03:11:06 +00:00
voteQuery.Add(fieldNameEventID, eventID)
2020-04-14 05:29:40 +00:00
voteURL := h.baseURL + pathVote + "?" + voteQuery.Encode()
2020-04-01 05:49:08 +00:00
const dateDisplayFmt = "Monday, January 2, 2006"
2020-03-24 14:59:27 +00:00
2020-04-01 05:49:08 +00:00
body := hm.Terms{
e.P()(
hm.Text("You can find it again at "),
e.A(a.Href(adminURL))(hm.Text(adminURL)),
hm.Text("."),
),
2020-04-14 05:29:40 +00:00
e.P()(
hm.Text("Your guests can vote on times at "),
e.A(a.Href(voteURL))(hm.Text(voteURL)),
hm.Text("."),
),
2020-03-24 14:59:27 +00:00
2020-04-01 05:49:08 +00:00
e.H3()(hm.Text("Name")),
2020-09-28 03:11:06 +00:00
hm.Text(event.Name),
2020-04-01 04:36:41 +00:00
2020-04-01 05:49:08 +00:00
e.H3()(hm.Text("Description")),
2020-09-28 03:11:06 +00:00
hm.Text(event.Description),
2020-03-24 14:59:27 +00:00
2020-04-01 05:49:08 +00:00
e.H3()(hm.Text("Earliest date")),
2020-09-28 03:11:06 +00:00
hm.Text(event.Earliest.Format(dateDisplayFmt)),
2020-03-24 14:59:27 +00:00
2020-04-01 05:49:08 +00:00
e.H3()(hm.Text("Latest date")),
2020-09-28 03:11:06 +00:00
hm.Text(event.Latest.Format(dateDisplayFmt)),
2020-03-24 14:59:27 +00:00
}
2020-04-14 05:43:04 +00:00
_ = h.writePage(w, "Created event", body)
2020-03-24 14:59:27 +00:00
}
2020-04-01 05:49:08 +00:00
func (h *handler) handleAdmin(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
2020-09-28 02:25:21 +00:00
eventID := query.Get(fieldNameEventID)
2020-04-14 05:29:40 +00:00
// TODO authenticate with admin code
2020-04-16 13:51:24 +00:00
metadata, err := h.store.GetEventMetadata(context.Background(), back.GetEventMetadataQuery{
AlphaID: eventID,
2020-04-01 04:36:41 +00:00
})
2020-09-28 03:40:55 +00:00
if notFound(w, err, "Event not found") || internalServerError(w, err) {
2020-04-01 05:49:08 +00:00
return
2020-04-01 04:36:41 +00:00
}
2020-04-01 05:49:08 +00:00
2020-09-27 21:44:04 +00:00
responses, err := h.store.GetEventResponseSummary(context.Background(), back.GetEventResponseSummaryQuery{
2020-04-16 13:51:24 +00:00
AlphaID: eventID,
})
if err != nil {
fmt.Fprint(w, err)
return
}
// TODO show results (number of responses, grid)
2020-04-01 05:49:08 +00:00
body := hm.Terms{
2020-04-01 06:37:39 +00:00
e.Form()(
2020-04-01 05:49:08 +00:00
e.Label(a.For(fieldNameEventName))(hm.Text("Event name")),
2020-04-01 04:36:41 +00:00
e.Input(
a.Name(fieldNameEventName),
2020-04-16 13:51:24 +00:00
a.Value(metadata.Name),
2020-04-01 04:36:41 +00:00
),
e.Br(),
2020-04-01 05:49:08 +00:00
e.Label(a.For(fieldNameDescription))(hm.Text("Description")),
2020-04-16 13:51:24 +00:00
e.Textarea(a.Name(fieldNameDescription))(hm.Text(metadata.Description)),
2020-04-01 04:36:41 +00:00
e.Br(),
2020-09-28 03:11:06 +00:00
// TODO Should the date fields be disabled, or should we cull invalid
// response times after changing event dates?
2020-04-01 05:49:08 +00:00
e.Label(a.For(fieldNameEarliest))(hm.Text("Earliest date")),
2020-04-01 04:36:41 +00:00
e.Input(
a.Name(fieldNameEarliest),
a.Type("date"),
2020-04-21 14:25:15 +00:00
a.Value(metadata.Earliest.Format(formDateLayout)),
2020-04-01 04:36:41 +00:00
),
e.Br(),
2020-04-01 05:49:08 +00:00
e.Label(a.For(fieldNameLatest))(hm.Text("Latest date")),
2020-04-01 04:36:41 +00:00
e.Input(
a.Name(fieldNameLatest),
a.Type("date"),
2020-04-21 14:25:15 +00:00
a.Value(metadata.Latest.Format(formDateLayout)),
2020-04-01 04:36:41 +00:00
),
e.Br(),
e.Input(a.Type("submit")),
),
2020-04-16 13:51:24 +00:00
e.H3()(hm.Text("Responses")),
e.P()(hm.Text(strconv.Itoa(responses.TotalResponses))),
2020-04-01 04:36:41 +00:00
}
2020-04-14 05:43:04 +00:00
_ = h.writePage(w, "Edit your event", body)
2020-03-31 12:58:44 +00:00
}
2020-04-01 05:49:08 +00:00
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.mux.ServeHTTP(w, r)
}
2020-04-01 04:36:41 +00:00
2020-04-14 05:43:04 +00:00
func (h *handler) writePage(w io.Writer, title string, contents hm.Term) error {
// TODO Need optional subtitles, and titles should be optional.
// Take a Page struct with title, subtitle, and contents.
2020-04-01 05:49:08 +00:00
page := e.Html()(
e.Head()(
2020-04-14 05:43:04 +00:00
e.Title()(hm.Text(h.title+" — "+title)),
2020-03-31 12:58:44 +00:00
),
2020-04-01 05:49:08 +00:00
e.Body()(
2020-04-14 05:33:31 +00:00
e.H1()(e.A(a.Href(h.baseURL+pathRoot))(hm.Text(h.title))),
2020-04-14 05:43:04 +00:00
e.Div()(
e.H2()(hm.Text(title)),
contents,
),
2020-04-01 05:49:08 +00:00
),
)
_, err := hm.WriteDocument(w, page)
return err
}
2020-03-24 14:59:27 +00:00
2020-04-01 05:49:08 +00:00
const (
2020-09-28 02:25:21 +00:00
fieldNameDateHour = "date_hour"
fieldNameEarliest = "earliest_date"
fieldNameLatest = "latest_date"
fieldNameEventName = "event_name"
fieldNameDescription = "event_description"
fieldNameGuestName = "guest_name"
fieldNameEventID = "event_id"
fieldNameAdminCode = "admin_code"
2020-09-28 03:11:06 +00:00
fieldNameResponseID = "response_id"
2020-04-01 05:49:08 +00:00
)
2020-03-24 14:59:27 +00:00
2020-04-01 06:10:55 +00:00
const formDateLayout = "2006-01-02"