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
This commit is contained in:
Hayden 2023-03-06 21:18:58 -09:00 committed by GitHub
parent 2665b666f1
commit 23b5892aef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
100 changed files with 11437 additions and 2075 deletions

View file

@ -0,0 +1,74 @@
package adapters
import (
"net/http"
"github.com/hay-kot/homebox/backend/pkgs/server"
)
// Action is a function that adapts a function to the server.Handler interface.
// It decodes the request body into a value of type T and passes it to the function f.
// The function f is expected to return a value of type Y and an error.
//
// Example:
//
// type Body struct {
// Foo string `json:"foo"`
// }
//
// fn := func(ctx context.Context, b Body) (any, error) {
// // do something with b
// return nil, nil
// }
//
// r.Post("/foo", adapters.Action(fn, http.StatusCreated))
func Action[T any, Y any](f AdapterFunc[T, Y], ok int) server.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) error {
v, err := decode[T](r)
if err != nil {
return err
}
res, err := f(r.Context(), v)
if err != nil {
return err
}
return server.Respond(w, ok, res)
}
}
// ActionID functions the same as Action, but it also decodes a UUID from the URL path.
//
// Example:
//
// type Body struct {
// Foo string `json:"foo"`
// }
//
// fn := func(ctx context.Context, ID uuid.UUID, b Body) (any, error) {
// // do something with ID and b
// return nil, nil
// }
//
// r.Post("/foo/{id}", adapters.ActionID(fn, http.StatusCreated))
func ActionID[T any, Y any](param string, f IDFunc[T, Y], ok int) server.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) error {
ID, err := routeUUID(r, param)
if err != nil {
return err
}
v, err := decode[T](r)
if err != nil {
return err
}
res, err := f(r.Context(), ID, v)
if err != nil {
return err
}
return server.Respond(w, ok, res)
}
}

View file

@ -0,0 +1,10 @@
package adapters
import (
"context"
"github.com/google/uuid"
)
type AdapterFunc[T any, Y any] func(context.Context, T) (Y, error)
type IDFunc[T any, Y any] func(context.Context, uuid.UUID, T) (Y, error)

View file

@ -0,0 +1,62 @@
package adapters
import (
"context"
"net/http"
"github.com/google/uuid"
"github.com/hay-kot/homebox/backend/pkgs/server"
)
type CommandFunc[T any] func(context.Context) (T, error)
type CommandIDFunc[T any] func(context.Context, uuid.UUID) (T, error)
// Command is an HandlerAdapter that returns a server.HandlerFunc that
// The command adapters are used to handle commands that do not accept a body
// or a query. You can think of them as a way to handle RPC style Rest Endpoints.
//
// Example:
//
// fn := func(ctx context.Context) (interface{}, error) {
// // do something
// return nil, nil
// }
//
// r.Get("/foo", adapters.Command(fn, http.NoContent))
func Command[T any](f CommandFunc[T], ok int) server.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) error {
res, err := f(r.Context())
if err != nil {
return err
}
return server.Respond(w, ok, res)
}
}
// CommandID is the same as the Command adapter but it accepts a UUID as a parameter
// in the URL. The parameter name is passed as the first argument.
//
// Example:
//
// fn := func(ctx context.Context, id uuid.UUID) (interface{}, error) {
// // do something
// return nil, nil
// }
//
// r.Get("/foo/{id}", adapters.CommandID("id", fn, http.NoContent))
func CommandID[T any](param string, f CommandIDFunc[T], ok int) server.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) error {
ID, err := routeUUID(r, param)
if err != nil {
return err
}
res, err := f(r.Context(), ID)
if err != nil {
return err
}
return server.Respond(w, ok, res)
}
}

View file

@ -0,0 +1,52 @@
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
}

View file

@ -0,0 +1,9 @@
/*
Package adapters offers common adapters for turing regular functions into HTTP Handlers
There are three types of adapters
- Query adapters
- Action adapters
- Command adapters
*/
package adapters

View file

@ -0,0 +1,72 @@
package adapters
import (
"net/http"
"github.com/hay-kot/homebox/backend/pkgs/server"
)
// Query is a server.Handler that decodes a query from the request and calls the provided function.
//
// Example:
//
// type Query struct {
// Foo string `schema:"foo"`
// }
//
// fn := func(ctx context.Context, q Query) (any, error) {
// // do something with q
// return nil, nil
// }
//
// r.Get("/foo", adapters.Query(fn, http.StatusOK))
func Query[T any, Y any](f AdapterFunc[T, Y], ok int) server.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) error {
q, err := decodeQuery[T](r)
if err != nil {
return err
}
res, err := f(r.Context(), q)
if err != nil {
return err
}
return server.Respond(w, ok, res)
}
}
// QueryID is a server.Handler that decodes a query and an ID from the request and calls the provided function.
//
// Example:
//
// type Query struct {
// Foo string `schema:"foo"`
// }
//
// fn := func(ctx context.Context, ID uuid.UUID, q Query) (any, error) {
// // do something with ID and q
// return nil, nil
// }
//
// r.Get("/foo/{id}", adapters.QueryID(fn, http.StatusOK))
func QueryID[T any, Y any](param string, f IDFunc[T, Y], ok int) server.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) error {
ID, err := routeUUID(r, param)
if err != nil {
return err
}
q, err := decodeQuery[T](r)
if err != nil {
return err
}
res, err := f(r.Context(), ID, q)
if err != nil {
return err
}
return server.Respond(w, ok, res)
}
}

View file

@ -33,6 +33,8 @@ func Errors(log zerolog.Logger) server.Middleware {
Error: err.Error(),
}
case validate.IsFieldError(err):
code = http.StatusUnprocessableEntity
fieldErrors := err.(validate.FieldErrors)
resp.Error = "Validation Error"
resp.Fields = map[string]string{}
@ -43,14 +45,18 @@ func Errors(log zerolog.Logger) server.Middleware {
case validate.IsRequestError(err):
requestError := err.(*validate.RequestError)
resp.Error = requestError.Error()
code = requestError.Status
if requestError.Status == 0 {
code = http.StatusBadRequest
} else {
code = requestError.Status
}
case ent.IsNotFound(err):
resp.Error = "Not Found"
code = http.StatusNotFound
default:
resp.Error = "Unknown Error"
code = http.StatusInternalServerError
}
if err := server.Respond(w, code, resp); err != nil {