Clear empty optional fields.

This commit is contained in:
mikestefanello 2025-04-15 07:36:35 -04:00
parent 53113101c9
commit 8a4dd9fb3b
4 changed files with 28 additions and 9 deletions

View file

@ -96,6 +96,9 @@ func (h *Handler) PasswordTokenCreate(ctx echo.Context) error {
if payload.CreatedAt != nil {
op.SetCreatedAt(*payload.CreatedAt)
}
if payload.Abc != nil {
op.SetAbc(*payload.Abc)
}
_, err := op.Save(ctx.Request().Context())
return err
}
@ -122,6 +125,11 @@ func (h *Handler) PasswordTokenUpdate(ctx echo.Context, id int) error {
} else {
op.SetCreatedAt(*payload.CreatedAt)
}
if payload.Abc == nil {
op.ClearAbc()
} else {
op.SetAbc(*payload.Abc)
}
_, err = op.Save(ctx.Request().Context())
return err
}
@ -147,6 +155,7 @@ func (h *Handler) PasswordTokenList(ctx echo.Context) (*EntityList, error) {
Columns: []string{
"User ID",
"Created at",
"Abc",
},
Entities: make([]EntityValues, 0, len(res)),
HasNextPage: len(res) > h.Config.ItemsPerPage,
@ -158,6 +167,7 @@ func (h *Handler) PasswordTokenList(ctx echo.Context) (*EntityList, error) {
Values: []string{
fmt.Sprint(res[i].UserID),
res[i].CreatedAt.Format(h.Config.TimeFormat),
fmt.Sprint(res[i].Abc),
},
})
}
@ -174,6 +184,7 @@ func (h *Handler) PasswordTokenGet(ctx echo.Context, id int) (url.Values, error)
v := url.Values{}
v.Set("user_id", fmt.Sprint(entity.UserID))
v.Set("created_at", entity.CreatedAt.Format(time.RFC3339))
v.Set("abc", fmt.Sprint(entity.Abc))
return v, err
}
@ -285,7 +296,7 @@ func (h *Handler) getOffset(ctx echo.Context) int {
}
func (h *Handler) bind(ctx echo.Context, entity any) error {
// Remove empty field values so Echo's bind does to fail when trying to parse things like
// Remove empty field values so Echo's bind does not fail when trying to parse things like
// times, etc.
for k, v := range ctx.Request().Form {
if len(v) == 1 && len(v[0]) == 0 {

View file

@ -128,8 +128,11 @@
}
{{- else if $f.Nillable }}
op.SetNillable{{ fieldName $f.Name }}(payload.{{ fieldName $f.Name }})
if payload.{{ fieldName $f.Name }} != nil {
// TODO this is not available op.Clear{{ fieldName $f.Name }}()
{{- else if $f.Optional }}
if payload.{{ fieldName $f.Name }} == nil {
op.Clear{{ fieldName $f.Name }}()
} else {
op.Set{{ fieldName $f.Name }}(*payload.{{ fieldName $f.Name }})
}
{{- else if (fieldIsPointer $f) }}
if payload.{{ fieldName $f.Name }} == nil {
@ -230,7 +233,7 @@
}
func (h *Handler) bind(ctx echo.Context, entity any) error {
// Remove empty field values so Echo's bind does to fail when trying to parse things like
// Remove empty field values so Echo's bind does not fail when trying to parse things like
// times, etc.
for k, v := range ctx.Request().Form {
if len(v) == 1 && len(v[0]) == 0 {

View file

@ -1,12 +1,17 @@
// Code generated by ent, DO NOT EDIT.
package admin
import "time"
import (
"time"
"github.com/mikestefanello/pagoda/ent/passwordtoken"
)
type PasswordToken struct {
Hash *string `form:"hash"`
UserID int `form:"user_id"`
CreatedAt *time.Time `form:"created_at"`
Hash *string `form:"hash"`
UserID int `form:"user_id"`
CreatedAt *time.Time `form:"created_at"`
Abc *passwordtoken.Abc `form:"abc"`
}
type User struct {