From be5c1f919777290f7a586a1237b44673822bb990 Mon Sep 17 00:00:00 2001 From: mikestefanello <552328+mikestefanello@users.noreply.github.com> Date: Fri, 15 Aug 2025 09:51:06 -0400 Subject: [PATCH] Fix enums in generated admin schema code. --- ent/admin/handler.go | 12 ++++++++++++ ent/admin/schema.go | 14 +++++++++++++- ent/admin/templates/schema.tmpl | 9 +++------ ent/admin/types.go | 7 ++++++- pkg/ui/forms/admin_entity.go | 4 ++-- 5 files changed, 36 insertions(+), 10 deletions(-) diff --git a/ent/admin/handler.go b/ent/admin/handler.go index bb74b15..938de63 100644 --- a/ent/admin/handler.go +++ b/ent/admin/handler.go @@ -199,6 +199,9 @@ func (h *Handler) UserCreate(ctx echo.Context) error { if payload.CreatedAt != nil { op.SetCreatedAt(*payload.CreatedAt) } + if payload.Role != nil { + op.SetRole(*payload.Role) + } _, err := op.Save(ctx.Request().Context()) return err } @@ -222,6 +225,12 @@ func (h *Handler) UserUpdate(ctx echo.Context, id int) error { } op.SetVerified(payload.Verified) op.SetAdmin(payload.Admin) + if payload.Role == nil { + var empty user.Role + op.SetRole(empty) + } else { + op.SetRole(*payload.Role) + } _, err = op.Save(ctx.Request().Context()) return err } @@ -251,6 +260,7 @@ func (h *Handler) UserList(ctx echo.Context) (*EntityList, error) { "Verified", "Admin", "Created at", + "Role", }, Entities: make([]EntityValues, 0, len(res)), Page: page, @@ -266,6 +276,7 @@ func (h *Handler) UserList(ctx echo.Context) (*EntityList, error) { fmt.Sprint(res[i].Verified), fmt.Sprint(res[i].Admin), res[i].CreatedAt.Format(h.Config.TimeFormat), + fmt.Sprint(res[i].Role), }, }) } @@ -284,6 +295,7 @@ func (h *Handler) UserGet(ctx echo.Context, id int) (url.Values, error) { v.Set("email", entity.Email) v.Set("verified", fmt.Sprint(entity.Verified)) v.Set("admin", fmt.Sprint(entity.Admin)) + v.Set("role", fmt.Sprint(entity.Role)) return v, err } diff --git a/ent/admin/schema.go b/ent/admin/schema.go index e6ae61d..f7a207c 100644 --- a/ent/admin/schema.go +++ b/ent/admin/schema.go @@ -15,7 +15,7 @@ type FieldSchema struct { Optional bool Immutable bool Sensitive bool - Enums []Enum + Enums []string } const NamePasswordToken = "PasswordToken" @@ -98,4 +98,16 @@ var fieldsUser = []*FieldSchema{ Sensitive: false, Enums: nil, }, + { + Name: "role", + Type: field.TypeEnum, + Optional: false, + Immutable: false, + Sensitive: false, + Enums: []string{ + "read", + "write", + "admin", + }, + }, } diff --git a/ent/admin/templates/schema.tmpl b/ent/admin/templates/schema.tmpl index 37a0d09..752c631 100644 --- a/ent/admin/templates/schema.tmpl +++ b/ent/admin/templates/schema.tmpl @@ -19,7 +19,7 @@ Optional bool Immutable bool Sensitive bool - Enums []Enum + Enums []string } @@ -35,12 +35,9 @@ Immutable: {{ $f.Immutable }}, Sensitive: {{ $f.Sensitive }}, {{- if len $f.Enums }} - Enums: []Enum{ + Enums: []string{ {{- range $e := $f.Enums }} - { - Label: "{{ $e.Label }}", - Value: "{{ $e.Value }}", - }, + "{{ $e.Value }}", {{- end }} }, {{- else }} diff --git a/ent/admin/types.go b/ent/admin/types.go index 54f3a52..aa19236 100644 --- a/ent/admin/types.go +++ b/ent/admin/types.go @@ -1,7 +1,11 @@ // Code generated by ent, DO NOT EDIT. package admin -import "time" +import ( + "time" + + "github.com/mikestefanello/pagoda/ent/user" +) type PasswordToken struct { Token *string `form:"token"` @@ -24,6 +28,7 @@ type User struct { Verified bool `form:"verified"` Admin bool `form:"admin"` CreatedAt *time.Time `form:"created_at"` + Role *user.Role `form:"role"` } func (e *User) GetName() string { diff --git a/pkg/ui/forms/admin_entity.go b/pkg/ui/forms/admin_entity.go index 45939eb..cf3ea37 100644 --- a/pkg/ui/forms/admin_entity.go +++ b/pkg/ui/forms/admin_entity.go @@ -92,8 +92,8 @@ func AdminEntity(r *ui.Request, entityType admin.EntityType, values url.Values) } for _, enum := range f.Enums { options = append(options, Choice{ - Label: enum.Label, - Value: enum.Value, + Label: enum, + Value: enum, }) } nodes = append(nodes, SelectList(OptionsParams{