Generate ent schema in admin code. (#127)
This commit is contained in:
parent
67a97832a5
commit
9e6d9fd063
13 changed files with 303 additions and 142 deletions
|
|
@ -30,55 +30,55 @@ func NewHandler(client *ent.Client, cfg HandlerConfig) *Handler {
|
|||
}
|
||||
}
|
||||
|
||||
func (h *Handler) Create(ctx echo.Context, entityType string) error {
|
||||
switch entityType {
|
||||
case "PasswordToken":
|
||||
func (h *Handler) Create(ctx echo.Context, entityType EntityType) error {
|
||||
switch entityType.(type) {
|
||||
case *PasswordToken:
|
||||
return h.PasswordTokenCreate(ctx)
|
||||
case "User":
|
||||
case *User:
|
||||
return h.UserCreate(ctx)
|
||||
default:
|
||||
return fmt.Errorf("unsupported entity type: %s", entityType)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) Get(ctx echo.Context, entityType string, id int) (url.Values, error) {
|
||||
switch entityType {
|
||||
case "PasswordToken":
|
||||
func (h *Handler) Get(ctx echo.Context, entityType EntityType, id int) (url.Values, error) {
|
||||
switch entityType.(type) {
|
||||
case *PasswordToken:
|
||||
return h.PasswordTokenGet(ctx, id)
|
||||
case "User":
|
||||
case *User:
|
||||
return h.UserGet(ctx, id)
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported entity type: %s", entityType)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) Delete(ctx echo.Context, entityType string, id int) error {
|
||||
switch entityType {
|
||||
case "PasswordToken":
|
||||
func (h *Handler) Delete(ctx echo.Context, entityType EntityType, id int) error {
|
||||
switch entityType.(type) {
|
||||
case *PasswordToken:
|
||||
return h.PasswordTokenDelete(ctx, id)
|
||||
case "User":
|
||||
case *User:
|
||||
return h.UserDelete(ctx, id)
|
||||
default:
|
||||
return fmt.Errorf("unsupported entity type: %s", entityType)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) Update(ctx echo.Context, entityType string, id int) error {
|
||||
switch entityType {
|
||||
case "PasswordToken":
|
||||
func (h *Handler) Update(ctx echo.Context, entityType EntityType, id int) error {
|
||||
switch entityType.(type) {
|
||||
case *PasswordToken:
|
||||
return h.PasswordTokenUpdate(ctx, id)
|
||||
case "User":
|
||||
case *User:
|
||||
return h.UserUpdate(ctx, id)
|
||||
default:
|
||||
return fmt.Errorf("unsupported entity type: %s", entityType)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) List(ctx echo.Context, entityType string) (*EntityList, error) {
|
||||
switch entityType {
|
||||
case "PasswordToken":
|
||||
func (h *Handler) List(ctx echo.Context, entityType EntityType) (*EntityList, error) {
|
||||
switch entityType.(type) {
|
||||
case *PasswordToken:
|
||||
return h.PasswordTokenList(ctx)
|
||||
case "User":
|
||||
case *User:
|
||||
return h.UserList(ctx)
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported entity type: %s", entityType)
|
||||
|
|
|
|||
101
ent/admin/schema.go
Normal file
101
ent/admin/schema.go
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
// Code generated by ent, DO NOT EDIT.
|
||||
package admin
|
||||
|
||||
import (
|
||||
"entgo.io/ent/schema/field"
|
||||
)
|
||||
|
||||
type Enum struct {
|
||||
Label, Value string
|
||||
}
|
||||
|
||||
type FieldSchema struct {
|
||||
Name string
|
||||
Type field.Type
|
||||
Optional bool
|
||||
Immutable bool
|
||||
Sensitive bool
|
||||
Enums []Enum
|
||||
}
|
||||
|
||||
const NamePasswordToken = "PasswordToken"
|
||||
|
||||
var fieldsPasswordToken = []*FieldSchema{
|
||||
{
|
||||
Name: "token",
|
||||
Type: field.TypeString,
|
||||
Optional: false,
|
||||
Immutable: false,
|
||||
Sensitive: true,
|
||||
Enums: nil,
|
||||
},
|
||||
{
|
||||
Name: "user_id",
|
||||
Type: field.TypeInt,
|
||||
Optional: false,
|
||||
Immutable: false,
|
||||
Sensitive: false,
|
||||
Enums: nil,
|
||||
},
|
||||
{
|
||||
Name: "created_at",
|
||||
Type: field.TypeTime,
|
||||
Optional: false,
|
||||
Immutable: false,
|
||||
Sensitive: false,
|
||||
Enums: nil,
|
||||
},
|
||||
}
|
||||
|
||||
const NameUser = "User"
|
||||
|
||||
var fieldsUser = []*FieldSchema{
|
||||
{
|
||||
Name: "name",
|
||||
Type: field.TypeString,
|
||||
Optional: false,
|
||||
Immutable: false,
|
||||
Sensitive: false,
|
||||
Enums: nil,
|
||||
},
|
||||
{
|
||||
Name: "email",
|
||||
Type: field.TypeString,
|
||||
Optional: false,
|
||||
Immutable: false,
|
||||
Sensitive: false,
|
||||
Enums: nil,
|
||||
},
|
||||
{
|
||||
Name: "password",
|
||||
Type: field.TypeString,
|
||||
Optional: false,
|
||||
Immutable: false,
|
||||
Sensitive: true,
|
||||
Enums: nil,
|
||||
},
|
||||
{
|
||||
Name: "verified",
|
||||
Type: field.TypeBool,
|
||||
Optional: false,
|
||||
Immutable: false,
|
||||
Sensitive: false,
|
||||
Enums: nil,
|
||||
},
|
||||
{
|
||||
Name: "admin",
|
||||
Type: field.TypeBool,
|
||||
Optional: false,
|
||||
Immutable: false,
|
||||
Sensitive: false,
|
||||
Enums: nil,
|
||||
},
|
||||
{
|
||||
Name: "created_at",
|
||||
Type: field.TypeTime,
|
||||
Optional: false,
|
||||
Immutable: true,
|
||||
Sensitive: false,
|
||||
Enums: nil,
|
||||
},
|
||||
}
|
||||
|
|
@ -35,10 +35,10 @@
|
|||
}
|
||||
}
|
||||
|
||||
func (h *Handler) Create(ctx echo.Context, entityType string) error {
|
||||
switch entityType {
|
||||
func (h *Handler) Create(ctx echo.Context, entityType EntityType) error {
|
||||
switch entityType.(type) {
|
||||
{{- range $n := $.Nodes }}
|
||||
case "{{ $n.Name }}":
|
||||
case *{{ $n.Name }}:
|
||||
return h.{{ $n.Name }}Create(ctx)
|
||||
{{- end }}
|
||||
default:
|
||||
|
|
@ -46,10 +46,10 @@
|
|||
}
|
||||
}
|
||||
|
||||
func (h *Handler) Get(ctx echo.Context, entityType string, id int) (url.Values, error) {
|
||||
switch entityType {
|
||||
func (h *Handler) Get(ctx echo.Context, entityType EntityType, id int) (url.Values, error) {
|
||||
switch entityType.(type) {
|
||||
{{- range $n := $.Nodes }}
|
||||
case "{{ $n.Name }}":
|
||||
case *{{ $n.Name }}:
|
||||
return h.{{ $n.Name }}Get(ctx, id)
|
||||
{{- end }}
|
||||
default:
|
||||
|
|
@ -57,10 +57,10 @@
|
|||
}
|
||||
}
|
||||
|
||||
func (h *Handler) Delete(ctx echo.Context, entityType string, id int) error {
|
||||
switch entityType {
|
||||
func (h *Handler) Delete(ctx echo.Context, entityType EntityType, id int) error {
|
||||
switch entityType.(type) {
|
||||
{{- range $n := $.Nodes }}
|
||||
case "{{ $n.Name }}":
|
||||
case *{{ $n.Name }}:
|
||||
return h.{{ $n.Name }}Delete(ctx, id)
|
||||
{{- end }}
|
||||
default:
|
||||
|
|
@ -68,10 +68,10 @@
|
|||
}
|
||||
}
|
||||
|
||||
func (h *Handler) Update(ctx echo.Context, entityType string, id int) error {
|
||||
switch entityType {
|
||||
func (h *Handler) Update(ctx echo.Context, entityType EntityType, id int) error {
|
||||
switch entityType.(type) {
|
||||
{{- range $n := $.Nodes }}
|
||||
case "{{ $n.Name }}":
|
||||
case *{{ $n.Name }}:
|
||||
return h.{{ $n.Name }}Update(ctx, id)
|
||||
{{- end }}
|
||||
default:
|
||||
|
|
@ -79,10 +79,10 @@
|
|||
}
|
||||
}
|
||||
|
||||
func (h *Handler) List(ctx echo.Context, entityType string) (*EntityList, error) {
|
||||
switch entityType {
|
||||
func (h *Handler) List(ctx echo.Context, entityType EntityType) (*EntityList, error) {
|
||||
switch entityType.(type) {
|
||||
{{- range $n := $.Nodes }}
|
||||
case "{{ $n.Name }}":
|
||||
case *{{ $n.Name }}:
|
||||
return h.{{ $n.Name }}List(ctx)
|
||||
{{- end }}
|
||||
default:
|
||||
|
|
|
|||
54
ent/admin/templates/schema.tmpl
Normal file
54
ent/admin/templates/schema.tmpl
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
{{/* Tell Intellij/GoLand to enable the autocompletion based on the *gen.Graph type. */}}
|
||||
{{/* gotype: entgo.io/ent/entc/gen.Graph */}}
|
||||
|
||||
{{ define "admin/schema" }}
|
||||
// Code generated by ent, DO NOT EDIT.
|
||||
package admin
|
||||
|
||||
import (
|
||||
"entgo.io/ent/schema/field"
|
||||
)
|
||||
|
||||
type Enum struct {
|
||||
Label, Value string
|
||||
}
|
||||
|
||||
type FieldSchema struct {
|
||||
Name string
|
||||
Type field.Type
|
||||
Optional bool
|
||||
Immutable bool
|
||||
Sensitive bool
|
||||
Enums []Enum
|
||||
}
|
||||
|
||||
|
||||
{{- range $n := $.Nodes }}
|
||||
const Name{{ $n.Name }} = "{{ $n.Name }}"
|
||||
|
||||
var fields{{ $n.Name }} = []*FieldSchema{
|
||||
{{- range $f := $n.Fields }}
|
||||
{
|
||||
Name: "{{ $f.Name }}",
|
||||
Type: field.{{ $f.Type.Type.ConstName }},
|
||||
Optional: {{ $f.Optional }},
|
||||
Immutable: {{ $f.Immutable }},
|
||||
Sensitive: {{ $f.Sensitive }},
|
||||
{{- if len $f.Enums }}
|
||||
Enums: []Enum{
|
||||
{{- range $e := $f.Enums }}
|
||||
{
|
||||
Label: "{{ $e.Label }}",
|
||||
Value: "{{ $e.Value }}",
|
||||
},
|
||||
{{- end }}
|
||||
},
|
||||
{{- else }}
|
||||
Enums: nil,
|
||||
{{- end }}
|
||||
},
|
||||
{{- end }}
|
||||
}
|
||||
{{ end }}
|
||||
|
||||
{{ end }}
|
||||
|
|
@ -11,8 +11,27 @@
|
|||
{{ fieldName $f.Name }} {{ if (fieldIsPointer $f) }}*{{ end }}{{ $f.Type }} `form:"{{ $f.Name }}"`
|
||||
{{- end }}
|
||||
}
|
||||
|
||||
func (e *{{ $n.Name }}) GetName() string {
|
||||
return Name{{ $n.Name }}
|
||||
}
|
||||
|
||||
func (e *{{ $n.Name }}) GetSchema() []*FieldSchema {
|
||||
return fields{{ $n.Name }}
|
||||
}
|
||||
{{ end }}
|
||||
|
||||
type EntityType interface {
|
||||
GetName() string
|
||||
GetSchema() []*FieldSchema
|
||||
}
|
||||
|
||||
var entityTypes = []EntityType{
|
||||
{{- range $n := $.Nodes }}
|
||||
&{{ $n.Name }}{},
|
||||
{{- end }}
|
||||
}
|
||||
|
||||
type EntityList struct {
|
||||
Columns []string
|
||||
Entities []EntityValues
|
||||
|
|
@ -31,12 +50,7 @@
|
|||
TimeFormat string
|
||||
}
|
||||
|
||||
func GetEntityTypeNames() []string {
|
||||
return []string{
|
||||
{{- range $n := $.Nodes }}
|
||||
"{{ $n.Name }}",
|
||||
{{- end }}
|
||||
}
|
||||
func GetEntityTypes() []EntityType {
|
||||
return entityTypes
|
||||
}
|
||||
|
||||
{{ end }}
|
||||
|
|
@ -9,6 +9,14 @@ type PasswordToken struct {
|
|||
CreatedAt *time.Time `form:"created_at"`
|
||||
}
|
||||
|
||||
func (e *PasswordToken) GetName() string {
|
||||
return NamePasswordToken
|
||||
}
|
||||
|
||||
func (e *PasswordToken) GetSchema() []*FieldSchema {
|
||||
return fieldsPasswordToken
|
||||
}
|
||||
|
||||
type User struct {
|
||||
Name string `form:"name"`
|
||||
Email string `form:"email"`
|
||||
|
|
@ -18,6 +26,24 @@ type User struct {
|
|||
CreatedAt *time.Time `form:"created_at"`
|
||||
}
|
||||
|
||||
func (e *User) GetName() string {
|
||||
return NameUser
|
||||
}
|
||||
|
||||
func (e *User) GetSchema() []*FieldSchema {
|
||||
return fieldsUser
|
||||
}
|
||||
|
||||
type EntityType interface {
|
||||
GetName() string
|
||||
GetSchema() []*FieldSchema
|
||||
}
|
||||
|
||||
var entityTypes = []EntityType{
|
||||
&PasswordToken{},
|
||||
&User{},
|
||||
}
|
||||
|
||||
type EntityList struct {
|
||||
Columns []string
|
||||
Entities []EntityValues
|
||||
|
|
@ -36,9 +62,6 @@ type HandlerConfig struct {
|
|||
TimeFormat string
|
||||
}
|
||||
|
||||
func GetEntityTypeNames() []string {
|
||||
return []string{
|
||||
"PasswordToken",
|
||||
"User",
|
||||
}
|
||||
func GetEntityTypes() []EntityType {
|
||||
return entityTypes
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue