Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
validate using currency service
Browse files Browse the repository at this point in the history
  • Loading branch information
hay-kot committed Jan 5, 2024
1 parent e647419 commit fa676d6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
9 changes: 9 additions & 0 deletions backend/app/api/handlers/v1/v1_ctrl_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
}

Expand Down
6 changes: 5 additions & 1 deletion backend/internal/sys/validate/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit fa676d6

Please sign in to comment.