diff --git a/html/handler.go b/html/handler.go index 51cd6d02..e2a58772 100644 --- a/html/handler.go +++ b/html/handler.go @@ -5,9 +5,12 @@ import ( "net/http" nhttp "github.com/Gleipnir-Technology/nidus-sync/http" + "github.com/gorilla/schema" "github.com/rs/zerolog/log" ) +var decoder = schema.NewDecoder() + type handlerFunctionGet[T any] func(context.Context, *http.Request) (*Response[T], *nhttp.ErrorWithStatus) func MakeGet[T any](f handlerFunctionGet[T]) http.HandlerFunc { @@ -25,3 +28,30 @@ func MakeGet[T any](f handlerFunctionGet[T]) http.HandlerFunc { }) } } + +type handlerFunctionPost[T any] func(context.Context, *http.Request, T) (string, *nhttp.ErrorWithStatus) + +func MakePost[T any](f handlerFunctionPost[T]) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + err := r.ParseForm() + if err != nil { + RespondError(w, "Failed to parse form", err, http.StatusBadRequest) + return + } + + var content T + + err = decoder.Decode(&content, r.PostForm) + if err != nil { + RespondError(w, "Failed to decode form", err, http.StatusBadRequest) + return + } + ctx := r.Context() + path, e := f(ctx, r, content) + if e != nil { + http.Error(w, e.Error(), e.Status) + return + } + http.Redirect(w, r, path, http.StatusFound) + } +} diff --git a/html/template/rmo/mailer/confirm.html b/html/template/rmo/mailer/appointment-confirmed.html similarity index 100% rename from html/template/rmo/mailer/confirm.html rename to html/template/rmo/mailer/appointment-confirmed.html diff --git a/html/template/rmo/mailer/root.html b/html/template/rmo/mailer/root.html index a5085ffb..2ea2d571 100644 --- a/html/template/rmo/mailer/root.html +++ b/html/template/rmo/mailer/root.html @@ -62,12 +62,14 @@
- - Correct - + +