diff --git a/handlers/pages/dashboard.templ b/handlers/pages/dashboard.templ index f6f0906..35dd42a 100644 --- a/handlers/pages/dashboard.templ +++ b/handlers/pages/dashboard.templ @@ -4,13 +4,19 @@ import ( "net/http" "errors" "log" + "strconv" "forge.capytal.company/capytalcode/project-comicverse/lib/router/rerrors" "forge.capytal.company/capytalcode/project-comicverse/lib/cookies" + "forge.capytal.company/capytalcode/project-comicverse/lib/forms" "forge.capytal.company/capytalcode/project-comicverse/templates/layouts" ) -type Dashboard struct{} +type Dashboard struct { + Message string `form:"message"` + Limit int `form:"limit"` + Optional *string `form:"optional"` +} type DashboardCookie struct { Hello string `cookie:"dashboard-cookie"` @@ -19,6 +25,11 @@ type DashboardCookie struct { } func (p *Dashboard) ServeHTTP(w http.ResponseWriter, r *http.Request) { + if err := forms.Unmarshal(r, p); err != nil { + forms.RerrUnsmarshal(err).ServeHTTP(w, r) + return + } + hasCookie := true var c DashboardCookie @@ -49,9 +60,13 @@ func (p *Dashboard) ServeHTTP(w http.ResponseWriter, r *http.Request) { templ (p *Dashboard) Component() { @layouts.Page() {
-

Hello world

-

test

-

test

+

{ p.Message }

+

{ strconv.Itoa(p.Limit) }

+ if p.Optional != nil { +

{ *p.Optional }

+ } else { +

nil

+ }
} } diff --git a/lib/forms/forms.go b/lib/forms/forms.go index cd51af8..302912c 100644 --- a/lib/forms/forms.go +++ b/lib/forms/forms.go @@ -9,6 +9,9 @@ import ( "strconv" "strings" + "forge.capytal.company/capytalcode/project-comicverse/lib/router/rerrors" +) + func Unmarshal(r *http.Request, v any) (err error) { if u, ok := v.(Unmarshaler); ok { return u.UnmarshalForm(r) @@ -91,6 +94,16 @@ func Unmarshal(r *http.Request, v any) (err error) { return nil } +func RerrUnsmarshal(err error) rerrors.RouteError { + if e, ok := err.(*ErrMissingRequiredValue); ok { + return rerrors.MissingParameters([]string{e.value}) + } else if e, ok := err.(*ErrInvalidValueType); ok { + return rerrors.BadRequest(e.Error()) + } else { + return rerrors.InternalError(err) + } +} + func setFieldValue(rv reflect.Value, v string) error { switch rv.Kind() {