mirror of
https://github.com/hay-kot/homebox.git
synced 2025-07-23 11:00:28 +00:00
refactor adapters to fit more common use cases
This commit is contained in:
parent
b0a9c510ad
commit
d79d0b45bf
4 changed files with 135 additions and 84 deletions
39
backend/internal/web/adapters/command.go
Normal file
39
backend/internal/web/adapters/command.go
Normal file
|
@ -0,0 +1,39 @@
|
|||
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)
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue