Move hatmill terms into handlers
This commit is contained in:
parent
966b337a93
commit
d0937474b5
215
http/server.go
215
http/server.go
@ -12,7 +12,7 @@ import (
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
h "gitlab.codemonkeysoftware.net/b/hatmill"
|
||||
hm "gitlab.codemonkeysoftware.net/b/hatmill"
|
||||
a "gitlab.codemonkeysoftware.net/b/hatmill/attribute"
|
||||
e "gitlab.codemonkeysoftware.net/b/hatmill/element"
|
||||
"gitlab.codemonkeysoftware.net/b/henwen"
|
||||
@ -49,11 +49,35 @@ func NewHandler() http.Handler {
|
||||
}
|
||||
|
||||
func (h *handler) handleRoot(w http.ResponseWriter, r *http.Request) {
|
||||
_ = writePage(w, pageRoot())
|
||||
body := hm.Terms{
|
||||
e.H2()(hm.Text("Welcome!")),
|
||||
e.A(a.Href(pathCreate))(
|
||||
hm.Text("Create event"),
|
||||
),
|
||||
}
|
||||
_ = writePage(w, body)
|
||||
}
|
||||
|
||||
func (h *handler) handleCreate(w http.ResponseWriter, r *http.Request) {
|
||||
_ = writePage(w, pageCreate())
|
||||
body := hm.Terms{
|
||||
e.H2()(hm.Text("Create an event")),
|
||||
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")),
|
||||
),
|
||||
}
|
||||
_ = writePage(w, body)
|
||||
}
|
||||
|
||||
func (h *handler) handleDoCreate(w http.ResponseWriter, r *http.Request) {
|
||||
@ -74,92 +98,72 @@ func (h *handler) handleDoCreate(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
description := r.FormValue(fieldNameDescription)
|
||||
|
||||
_ = writePage(w, pageDoCreate(eventName, description, earliest, latest))
|
||||
event, err := store.CreateEvent(context.Background(), henwen.CreateEventCommand{
|
||||
Name: eventName,
|
||||
Description: description,
|
||||
EarliestDate: earliest,
|
||||
LatestDate: latest,
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Fprint(w, err)
|
||||
return
|
||||
}
|
||||
var adminQuery = make(url.Values)
|
||||
adminQuery.Add(keyEventID, event.AlphaID)
|
||||
adminQuery.Add(keyAdminCode, event.AdminCode)
|
||||
adminURL := baseURL + pathAdmin + "?" + adminQuery.Encode()
|
||||
|
||||
const dateDisplayFmt = "Monday, January 2, 2006"
|
||||
|
||||
body := hm.Terms{
|
||||
e.H2()(hm.Text("Created event!")),
|
||||
e.P()(
|
||||
hm.Text("You can find it again at "),
|
||||
e.A(a.Href(adminURL))(hm.Text(adminURL)),
|
||||
hm.Text("."),
|
||||
),
|
||||
|
||||
e.H3()(hm.Text("Name")),
|
||||
hm.Text(eventName),
|
||||
|
||||
e.H3()(hm.Text("Description")),
|
||||
hm.Text(description),
|
||||
|
||||
e.H3()(hm.Text("Earliest date")),
|
||||
hm.Text(earliest.Format(dateDisplayFmt)),
|
||||
|
||||
e.H3()(hm.Text("Latest date")),
|
||||
hm.Text(latest.Format(dateDisplayFmt)),
|
||||
}
|
||||
_ = writePage(w, body)
|
||||
}
|
||||
|
||||
func (h *handler) handleAdmin(w http.ResponseWriter, r *http.Request) {
|
||||
query := r.URL.Query()
|
||||
_ = writePage(w, pageAdmin(query.Get(keyEventID), query.Get(keyAdminCode)))
|
||||
}
|
||||
|
||||
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
h.mux.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
func writePage(w io.Writer, contents h.Term) error {
|
||||
page := e.Html()(
|
||||
e.Head()(
|
||||
e.Title()(h.Text(Title)),
|
||||
),
|
||||
e.Body()(
|
||||
e.H1()(h.Text(Title)),
|
||||
e.Div()(contents),
|
||||
),
|
||||
)
|
||||
_, err := h.WriteDocument(w, page)
|
||||
return err
|
||||
}
|
||||
|
||||
func pageRoot() h.Term {
|
||||
return h.Terms{
|
||||
e.H2()(h.Text("Welcome!")),
|
||||
e.A(a.Href(pathCreate))(
|
||||
h.Text("Create event"),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
fieldNameEarliest = "earliestDate"
|
||||
fieldNameLatest = "latestDate"
|
||||
fieldNameEventName = "eventName"
|
||||
fieldNameDescription = "eventDescription"
|
||||
)
|
||||
|
||||
func pageCreate() h.Term {
|
||||
return h.Terms{
|
||||
e.H2()(h.Text("Create an event")),
|
||||
e.Form(a.Action(pathDoCreate), a.Method(http.MethodPost))(
|
||||
e.Label(a.For(fieldNameEventName))(h.Text("Event name")),
|
||||
e.Input(a.Name(fieldNameEventName)),
|
||||
|
||||
e.Label(a.For(fieldNameDescription))(h.Text("Description")),
|
||||
e.Textarea(a.Name(fieldNameDescription), a.Placeholder("What's going on?"))(),
|
||||
|
||||
e.Label(a.For(fieldNameEarliest))(h.Text("Earliest date")),
|
||||
e.Input(a.Name(fieldNameEarliest), a.Type("date")),
|
||||
|
||||
e.Label(a.For(fieldNameLatest))(h.Text("Latest date")),
|
||||
e.Input(a.Name(fieldNameLatest), a.Type("date")),
|
||||
|
||||
e.Input(a.Type("submit")),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
func pageAdmin(alphaID, adminCode string) h.Term {
|
||||
event, err := store.GetEvent(context.Background(), henwen.GetEventQuery{
|
||||
AlphaID: alphaID,
|
||||
AdminCode: adminCode,
|
||||
AlphaID: query.Get(keyEventID),
|
||||
AdminCode: query.Get(keyAdminCode),
|
||||
})
|
||||
if err != nil {
|
||||
return h.Text(err.Error())
|
||||
fmt.Fprint(w, err)
|
||||
return
|
||||
}
|
||||
return h.Terms{
|
||||
e.H2()(h.Text("Edit your event")),
|
||||
|
||||
body := hm.Terms{
|
||||
e.H2()(hm.Text("Edit your event")),
|
||||
e.Form(a.Action(pathDoCreate), a.Method(http.MethodPost))(
|
||||
e.Label(a.For(fieldNameEventName))(h.Text("Event name")),
|
||||
e.Label(a.For(fieldNameEventName))(hm.Text("Event name")),
|
||||
e.Input(
|
||||
a.Name(fieldNameEventName),
|
||||
a.Value(event.Name),
|
||||
),
|
||||
e.Br(),
|
||||
|
||||
e.Label(a.For(fieldNameDescription))(h.Text("Description")),
|
||||
e.Textarea(a.Name(fieldNameEventName))(h.Text(event.Description)),
|
||||
e.Label(a.For(fieldNameDescription))(hm.Text("Description")),
|
||||
e.Textarea(a.Name(fieldNameEventName))(hm.Text(event.Description)),
|
||||
e.Br(),
|
||||
|
||||
e.Label(a.For(fieldNameEarliest))(h.Text("Earliest date")),
|
||||
e.Label(a.For(fieldNameEarliest))(hm.Text("Earliest date")),
|
||||
e.Input(
|
||||
a.Name(fieldNameEarliest),
|
||||
a.Type("date"),
|
||||
@ -167,7 +171,7 @@ func pageAdmin(alphaID, adminCode string) h.Term {
|
||||
),
|
||||
e.Br(),
|
||||
|
||||
e.Label(a.For(fieldNameLatest))(h.Text("Latest date")),
|
||||
e.Label(a.For(fieldNameLatest))(hm.Text("Latest date")),
|
||||
e.Input(
|
||||
a.Name(fieldNameLatest),
|
||||
a.Type("date"),
|
||||
@ -178,50 +182,37 @@ func pageAdmin(alphaID, adminCode string) h.Term {
|
||||
e.Input(a.Type("submit")),
|
||||
),
|
||||
}
|
||||
_ = writePage(w, body)
|
||||
}
|
||||
|
||||
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
h.mux.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
func writePage(w io.Writer, contents hm.Term) error {
|
||||
page := e.Html()(
|
||||
e.Head()(
|
||||
e.Title()(hm.Text(Title)),
|
||||
),
|
||||
e.Body()(
|
||||
e.H1()(hm.Text(Title)),
|
||||
e.Div()(contents),
|
||||
),
|
||||
)
|
||||
_, err := hm.WriteDocument(w, page)
|
||||
return err
|
||||
}
|
||||
|
||||
const (
|
||||
fieldNameEarliest = "earliestDate"
|
||||
fieldNameLatest = "latestDate"
|
||||
fieldNameEventName = "eventName"
|
||||
fieldNameDescription = "eventDescription"
|
||||
)
|
||||
|
||||
const keyEventID = "event_id"
|
||||
const keyAdminCode = "admin_code"
|
||||
|
||||
func pageDoCreate(name, description string, earliest, latest time.Time) h.Term {
|
||||
event, err := store.CreateEvent(context.Background(), henwen.CreateEventCommand{
|
||||
Name: name,
|
||||
Description: description,
|
||||
EarliestDate: earliest,
|
||||
LatestDate: latest,
|
||||
})
|
||||
if err != nil {
|
||||
return h.Text(err.Error())
|
||||
}
|
||||
var adminQuery = make(url.Values)
|
||||
adminQuery.Add(keyEventID, event.AlphaID)
|
||||
adminQuery.Add(keyAdminCode, event.AdminCode)
|
||||
adminURL := baseURL + pathAdmin + "?" + adminQuery.Encode()
|
||||
|
||||
const dateDisplayFmt = "Monday, January 2, 2006"
|
||||
|
||||
return h.Terms{
|
||||
e.H2()(h.Text("Created event!")),
|
||||
e.P()(
|
||||
h.Text("You can find it again at "),
|
||||
e.A(a.Href(adminURL))(h.Text(adminURL)),
|
||||
h.Text("."),
|
||||
),
|
||||
|
||||
e.H3()(h.Text("Name")),
|
||||
h.Text(name),
|
||||
|
||||
e.H3()(h.Text("Description")),
|
||||
h.Text(description),
|
||||
|
||||
e.H3()(h.Text("Earliest date")),
|
||||
h.Text(earliest.Format(dateDisplayFmt)),
|
||||
|
||||
e.H3()(h.Text("Latest date")),
|
||||
h.Text(latest.Format(dateDisplayFmt)),
|
||||
}
|
||||
}
|
||||
|
||||
var chars = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
||||
|
||||
func genString() (string, error) {
|
||||
|
Loading…
Reference in New Issue
Block a user