homebox/backend/internal/web/adapters/decoders.go
Hayden 23b5892aef
feat: Notifiers CRUD (#337)
* introduce scaffold for new models

* wip: shoutrrr wrapper (may remove)

* update schema files

* gen: ent code

* gen: migrations

* go mod tidy

* add group_id to notifier

* db migration

* new mapper helpers

* notifier repo

* introduce experimental adapter pattern for hdlrs

* refactor adapters to fit more common use cases

* new routes for notifiers

* update errors to fix validation panic

* go tidy

* reverse checkbox label display

* wip: notifiers UI

* use badges instead of text

* improve documentation

* add scaffold schema reference

* remove notifier service

* refactor schema folder

* support group edges via scaffold

* delete test file

* include link to API docs

* audit and update documentation + improve format

* refactor schema edges

* refactor

* add custom validator

* set validate + order fields by name

* fix failing tests
2023-03-06 21:18:58 -09:00

52 lines
906 B
Go

package adapters
import (
"net/http"
"github.com/go-chi/chi/v5"
"github.com/google/uuid"
"github.com/gorilla/schema"
"github.com/hay-kot/homebox/backend/internal/sys/validate"
"github.com/hay-kot/homebox/backend/pkgs/server"
)
var queryDecoder = schema.NewDecoder()
func decodeQuery[T any](r *http.Request) (T, error) {
var v T
err := queryDecoder.Decode(&v, r.URL.Query())
if err != nil {
return v, err
}
err = validate.Check(v)
if err != nil {
return v, err
}
return v, nil
}
func decode[T any](r *http.Request) (T, error) {
var v T
err := server.Decode(r, &v)
if err != nil {
return v, err
}
err = validate.Check(v)
if err != nil {
return v, err
}
return v, nil
}
func routeUUID(r *http.Request, key string) (uuid.UUID, error) {
ID, err := uuid.Parse(chi.URLParam(r, key))
if err != nil {
return uuid.Nil, validate.NewRouteKeyError(key)
}
return ID, nil
}