diff --git a/example/index.gohtml b/example/index.gohtml index 3a34dfa..f7727b3 100644 --- a/example/index.gohtml +++ b/example/index.gohtml @@ -36,7 +36,7 @@ {{template "index.gohtml" .}} {{- end -}} - {{- define "GET /fruits/{fruit}/edit GetFormEditRow(fruit)" -}} + {{- define "GET /fruits/{id}/edit GetFormEditRow(id)" -}} {{ .Row.Name }} @@ -49,9 +49,9 @@ {{- end -}} - {{- define "PATCH /fruits/{fruit} SubmitFormEditRow(request, fruit)" }} + {{- define "PATCH /fruits/{id} SubmitFormEditRow(id, form)" }} {{- if .Error -}} - {{template "GET /fruits/{fruit}/edit GetFormEditRow(fruit)" .}} + {{template "GET /fruits/{id}/edit GetFormEditRow(id)" .}} {{- else -}} {{template "fruit row" .Row}} {{- end -}} diff --git a/example/main.go b/example/main.go index 1cd9b8a..900c12a 100644 --- a/example/main.go +++ b/example/main.go @@ -7,7 +7,6 @@ import ( "html/template" "log" "net/http" - "strconv" ) //go:embed *.gohtml @@ -24,16 +23,16 @@ type EditRowPage struct { Error error } -func (b *Backend) SubmitFormEditRow(request *http.Request, fruitID int) EditRowPage { +type EditRow struct { + Value int `input:"count"` +} + +func (b *Backend) SubmitFormEditRow(fruitID int, form EditRow) EditRowPage { if fruitID < 0 || fruitID >= len(b.data) { return EditRowPage{Error: fmt.Errorf("fruit not found")} } - count, err := strconv.Atoi(request.FormValue("count")) - if err != nil { - return EditRowPage{Error: err, Row: b.data[fruitID]} - } row := b.data[fruitID] - row.Value = count + row.Value = form.Value return EditRowPage{Error: nil, Row: row} } diff --git a/example/template_routes.go b/example/template_routes.go index 6812f3b..12a79ba 100644 --- a/example/template_routes.go +++ b/example/template_routes.go @@ -10,31 +10,39 @@ import ( ) type RoutesReceiver interface { - SubmitFormEditRow(request *http.Request, fruitID int) EditRowPage + SubmitFormEditRow(fruitID int, form EditRow) EditRowPage GetFormEditRow(fruitID int) EditRowPage List(_ context.Context) []Row } func routes(mux *http.ServeMux, receiver RoutesReceiver) { - mux.HandleFunc("PATCH /fruits/{fruit}", func(response http.ResponseWriter, request *http.Request) { - fruitParsed, err := strconv.Atoi(request.PathValue("fruit")) + mux.HandleFunc("PATCH /fruits/{id}", func(response http.ResponseWriter, request *http.Request) { + idParsed, err := strconv.Atoi(request.PathValue("id")) if err != nil { http.Error(response, err.Error(), http.StatusBadRequest) return } - fruit := fruitParsed - data := receiver.SubmitFormEditRow(request, fruit) - execute(response, request, true, "PATCH /fruits/{fruit} SubmitFormEditRow(request, fruit)", http.StatusOK, data) + id := idParsed + request.ParseForm() + var form EditRow + ValueParsed, err := strconv.Atoi(request.FormValue("count")) + if err != nil { + http.Error(response, err.Error(), http.StatusBadRequest) + return + } + form.Value = ValueParsed + data := receiver.SubmitFormEditRow(id, form) + execute(response, request, true, "PATCH /fruits/{id} SubmitFormEditRow(id, form)", http.StatusOK, data) }) - mux.HandleFunc("GET /fruits/{fruit}/edit", func(response http.ResponseWriter, request *http.Request) { - fruitParsed, err := strconv.Atoi(request.PathValue("fruit")) + mux.HandleFunc("GET /fruits/{id}/edit", func(response http.ResponseWriter, request *http.Request) { + idParsed, err := strconv.Atoi(request.PathValue("id")) if err != nil { http.Error(response, err.Error(), http.StatusBadRequest) return } - fruit := fruitParsed - data := receiver.GetFormEditRow(fruit) - execute(response, request, true, "GET /fruits/{fruit}/edit GetFormEditRow(fruit)", http.StatusOK, data) + id := idParsed + data := receiver.GetFormEditRow(id) + execute(response, request, true, "GET /fruits/{id}/edit GetFormEditRow(id)", http.StatusOK, data) }) mux.HandleFunc("GET /help", func(response http.ResponseWriter, request *http.Request) { execute(response, request, true, "GET /help", http.StatusOK, request)