Redirect from POST handlers
This commit is contained in:
parent
a64df092a5
commit
fa330e4397
@ -8,7 +8,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/rickb777/date"
|
"github.com/rickb777/date"
|
||||||
hm "gitlab.codemonkeysoftware.net/b/hatmill"
|
hm "gitlab.codemonkeysoftware.net/b/hatmill"
|
||||||
@ -18,12 +17,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
pathRoot = "/"
|
pathRoot = "/"
|
||||||
pathCreate = "/create"
|
pathCreate = "/create"
|
||||||
pathDoCreate = "/create/do"
|
pathDoCreate = "/create/do"
|
||||||
pathAdmin = "/admin"
|
pathCreateSuccess = "/create/success"
|
||||||
pathVote = "/vote"
|
pathAdmin = "/admin"
|
||||||
pathDoVote = "/vote/do"
|
pathVote = "/vote"
|
||||||
|
pathDoVote = "/vote/do"
|
||||||
|
pathVoteSuccess = "/vote/success"
|
||||||
)
|
)
|
||||||
|
|
||||||
type handler struct {
|
type handler struct {
|
||||||
@ -52,6 +53,8 @@ func NewHandler(params HandlerParams) http.Handler {
|
|||||||
h.mux.HandleFunc(pathAdmin, h.handleAdmin)
|
h.mux.HandleFunc(pathAdmin, h.handleAdmin)
|
||||||
h.mux.HandleFunc(pathVote, h.handleVote)
|
h.mux.HandleFunc(pathVote, h.handleVote)
|
||||||
h.mux.HandleFunc(pathDoVote, h.handleDoVote)
|
h.mux.HandleFunc(pathDoVote, h.handleDoVote)
|
||||||
|
h.mux.HandleFunc(pathVoteSuccess, h.handleVoteSuccess)
|
||||||
|
h.mux.HandleFunc(pathCreateSuccess, h.handleCreateSuccess)
|
||||||
return h
|
return h
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -175,7 +178,7 @@ func (h *handler) handleDoVote(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
cmd.DateHours[d][hour] = struct{}{}
|
cmd.DateHours[d][hour] = struct{}{}
|
||||||
}
|
}
|
||||||
_, err = h.store.CreateEventResponse(r.Context(), cmd)
|
eventResponse, err := h.store.CreateEventResponse(r.Context(), cmd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
// TODO handle not found
|
// TODO handle not found
|
||||||
@ -183,16 +186,41 @@ func (h *handler) handleDoVote(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO Consider redirecting to vote edit page once that exists.
|
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),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
// TODO handle not found
|
||||||
|
http.Error(w, "internal server error", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
event, err := h.store.GetEventMetadata(r.Context(), back.GetEventMetadataQuery{
|
||||||
|
AlphaID: r.URL.Query().Get(fieldNameEventID),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
// TODO handle not found
|
||||||
|
http.Error(w, "internal server error", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// TODO Use actual data.
|
|
||||||
state := voteState{
|
state := voteState{
|
||||||
name: r.Form.Get(fieldNameGuestName),
|
name: eventResponse.GuestName,
|
||||||
earliest: date.New(2006, time.May, 3),
|
earliest: event.Earliest,
|
||||||
latest: date.New(2006, time.May, 8),
|
latest: event.Latest,
|
||||||
}
|
}
|
||||||
body := hm.Terms{
|
body := hm.Terms{
|
||||||
e.P()(hm.Text("At Billy's house. Bring presents. Eat cake.")),
|
e.P()(hm.Text(event.Description)),
|
||||||
e.H3()(hm.Text("Thanks for voting!")),
|
e.H3()(hm.Text("Thanks for voting!")),
|
||||||
e.P()(
|
e.P()(
|
||||||
hm.Text("You can edit your response anytime at "),
|
hm.Text("You can edit your response anytime at "),
|
||||||
@ -201,7 +229,7 @@ func (h *handler) handleDoVote(w http.ResponseWriter, r *http.Request) {
|
|||||||
),
|
),
|
||||||
voteForm(true, state),
|
voteForm(true, state),
|
||||||
}
|
}
|
||||||
_ = h.writePage(w, "Billy's birthday party", body)
|
_ = h.writePage(w, event.Name, body)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *handler) handleCreate(w http.ResponseWriter, r *http.Request) {
|
func (h *handler) handleCreate(w http.ResponseWriter, r *http.Request) {
|
||||||
@ -254,13 +282,32 @@ func (h *handler) handleDoCreate(w http.ResponseWriter, r *http.Request) {
|
|||||||
fmt.Fprint(w, err)
|
fmt.Fprint(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
// TODO handle not found
|
||||||
|
http.Error(w, "internal server error", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
var adminQuery = make(url.Values)
|
var adminQuery = make(url.Values)
|
||||||
adminQuery.Add(fieldNameEventID, event.AlphaID)
|
adminQuery.Add(fieldNameEventID, eventID)
|
||||||
adminQuery.Add(fieldNameAdminCode, event.AdminCode)
|
adminQuery.Add(fieldNameAdminCode, r.URL.Query().Get(fieldNameAdminCode))
|
||||||
adminURL := h.baseURL + pathAdmin + "?" + adminQuery.Encode()
|
adminURL := h.baseURL + pathAdmin + "?" + adminQuery.Encode()
|
||||||
|
|
||||||
var voteQuery = make(url.Values)
|
var voteQuery = make(url.Values)
|
||||||
voteQuery.Add(fieldNameEventID, event.AlphaID)
|
voteQuery.Add(fieldNameEventID, eventID)
|
||||||
voteURL := h.baseURL + pathVote + "?" + voteQuery.Encode()
|
voteURL := h.baseURL + pathVote + "?" + voteQuery.Encode()
|
||||||
|
|
||||||
const dateDisplayFmt = "Monday, January 2, 2006"
|
const dateDisplayFmt = "Monday, January 2, 2006"
|
||||||
@ -278,16 +325,16 @@ func (h *handler) handleDoCreate(w http.ResponseWriter, r *http.Request) {
|
|||||||
),
|
),
|
||||||
|
|
||||||
e.H3()(hm.Text("Name")),
|
e.H3()(hm.Text("Name")),
|
||||||
hm.Text(eventName),
|
hm.Text(event.Name),
|
||||||
|
|
||||||
e.H3()(hm.Text("Description")),
|
e.H3()(hm.Text("Description")),
|
||||||
hm.Text(description),
|
hm.Text(event.Description),
|
||||||
|
|
||||||
e.H3()(hm.Text("Earliest date")),
|
e.H3()(hm.Text("Earliest date")),
|
||||||
hm.Text(earliest.Format(dateDisplayFmt)),
|
hm.Text(event.Earliest.Format(dateDisplayFmt)),
|
||||||
|
|
||||||
e.H3()(hm.Text("Latest date")),
|
e.H3()(hm.Text("Latest date")),
|
||||||
hm.Text(latest.Format(dateDisplayFmt)),
|
hm.Text(event.Latest.Format(dateDisplayFmt)),
|
||||||
}
|
}
|
||||||
_ = h.writePage(w, "Created event", body)
|
_ = h.writePage(w, "Created event", body)
|
||||||
}
|
}
|
||||||
@ -327,6 +374,8 @@ func (h *handler) handleAdmin(w http.ResponseWriter, r *http.Request) {
|
|||||||
e.Textarea(a.Name(fieldNameDescription))(hm.Text(metadata.Description)),
|
e.Textarea(a.Name(fieldNameDescription))(hm.Text(metadata.Description)),
|
||||||
e.Br(),
|
e.Br(),
|
||||||
|
|
||||||
|
// TODO Should the date fields be disabled, or should we cull invalid
|
||||||
|
// response times after changing event dates?
|
||||||
e.Label(a.For(fieldNameEarliest))(hm.Text("Earliest date")),
|
e.Label(a.For(fieldNameEarliest))(hm.Text("Earliest date")),
|
||||||
e.Input(
|
e.Input(
|
||||||
a.Name(fieldNameEarliest),
|
a.Name(fieldNameEarliest),
|
||||||
@ -383,6 +432,7 @@ const (
|
|||||||
fieldNameGuestName = "guest_name"
|
fieldNameGuestName = "guest_name"
|
||||||
fieldNameEventID = "event_id"
|
fieldNameEventID = "event_id"
|
||||||
fieldNameAdminCode = "admin_code"
|
fieldNameAdminCode = "admin_code"
|
||||||
|
fieldNameResponseID = "response_id"
|
||||||
)
|
)
|
||||||
|
|
||||||
const formDateLayout = "2006-01-02"
|
const formDateLayout = "2006-01-02"
|
||||||
|
Loading…
Reference in New Issue
Block a user