validate using currency service

This commit is contained in:
Hayden 2024-01-05 11:40:46 -06:00
parent e647419eed
commit fa676d6351
No known key found for this signature in database
GPG key ID: 17CF79474E257545
2 changed files with 14 additions and 1 deletions

View file

@ -6,6 +6,7 @@ import (
"github.com/hay-kot/homebox/backend/internal/core/services"
"github.com/hay-kot/homebox/backend/internal/data/repo"
"github.com/hay-kot/homebox/backend/internal/sys/validate"
"github.com/hay-kot/homebox/backend/internal/web/adapters"
"github.com/hay-kot/httpkit/errchain"
)
@ -52,6 +53,14 @@ func (ctrl *V1Controller) HandleGroupGet() errchain.HandlerFunc {
func (ctrl *V1Controller) HandleGroupUpdate() errchain.HandlerFunc {
fn := func(r *http.Request, body repo.GroupUpdate) (repo.Group, error) {
auth := services.NewContext(r.Context())
ok := ctrl.svc.Currencies.IsSupported(body.Currency)
if !ok {
return repo.Group{}, validate.NewFieldErrors(
validate.NewFieldError("currency", "currency is not supported"),
)
}
return ctrl.svc.Group.UpdateGroup(auth, body)
}

View file

@ -88,7 +88,7 @@ func (fe FieldErrors) Nil() bool {
return len(fe) == 0
}
// Error implments the error interface.
// Error implements the error interface.
func (fe FieldErrors) Error() string {
d, err := json.Marshal(fe)
if err != nil {
@ -101,6 +101,10 @@ func NewFieldErrors(errs ...FieldError) FieldErrors {
return errs
}
func NewFieldError(field, reason string) FieldError {
return FieldError{Field: field, Error: reason}
}
func IsFieldError(err error) bool {
v := FieldErrors{}
return errors.As(err, &v)