Added admin handler configuration.
This commit is contained in:
parent
db4da26af9
commit
8c4e99fbd0
5 changed files with 34 additions and 78 deletions
|
|
@ -14,14 +14,14 @@ import (
|
|||
)
|
||||
|
||||
type Handler struct {
|
||||
client *ent.Client
|
||||
itemsPerPage int
|
||||
client *ent.Client
|
||||
Config HandlerConfig
|
||||
}
|
||||
|
||||
func NewHandler(client *ent.Client, itemsPerPage int) *Handler {
|
||||
func NewHandler(client *ent.Client, cfg HandlerConfig) *Handler {
|
||||
return &Handler{
|
||||
client: client,
|
||||
itemsPerPage: itemsPerPage,
|
||||
client: client,
|
||||
Config: cfg,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -124,7 +124,7 @@ func (h *Handler) PasswordTokenDelete(ctx echo.Context, id int) error {
|
|||
func (h *Handler) PasswordTokenList(ctx echo.Context) (*EntityList, error) {
|
||||
res, err := h.client.PasswordToken.
|
||||
Query().
|
||||
Limit(h.itemsPerPage + 1).
|
||||
Limit(h.Config.ItemsPerPage + 1).
|
||||
Offset(h.getOffset(ctx)).
|
||||
Order(passwordtoken.ByID(sql.OrderDesc())).
|
||||
All(ctx.Request().Context())
|
||||
|
|
@ -137,10 +137,9 @@ func (h *Handler) PasswordTokenList(ctx echo.Context) (*EntityList, error) {
|
|||
Columns: []string{
|
||||
"User id",
|
||||
"Created at",
|
||||
// "User", ?
|
||||
},
|
||||
Entities: make([]EntityValues, 0, len(res)),
|
||||
HasNextPage: len(res) > h.itemsPerPage,
|
||||
HasNextPage: len(res) > h.Config.ItemsPerPage,
|
||||
}
|
||||
|
||||
for i := 0; i <= len(res)-1; i++ {
|
||||
|
|
@ -148,8 +147,7 @@ func (h *Handler) PasswordTokenList(ctx echo.Context) (*EntityList, error) {
|
|||
ID: res[i].ID,
|
||||
Values: []string{
|
||||
fmt.Sprint(res[i].UserID),
|
||||
fmt.Sprint(res[i].CreatedAt),
|
||||
// TODO User ?
|
||||
res[i].CreatedAt.Format(h.Config.TimeFormat),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
|
@ -211,7 +209,7 @@ func (h *Handler) UserDelete(ctx echo.Context, id int) error {
|
|||
func (h *Handler) UserList(ctx echo.Context) (*EntityList, error) {
|
||||
res, err := h.client.User.
|
||||
Query().
|
||||
Limit(h.itemsPerPage + 1).
|
||||
Limit(h.Config.ItemsPerPage + 1).
|
||||
Offset(h.getOffset(ctx)).
|
||||
Order(user.ByID(sql.OrderDesc())).
|
||||
All(ctx.Request().Context())
|
||||
|
|
@ -228,7 +226,7 @@ func (h *Handler) UserList(ctx echo.Context) (*EntityList, error) {
|
|||
"Created at",
|
||||
},
|
||||
Entities: make([]EntityValues, 0, len(res)),
|
||||
HasNextPage: len(res) > h.itemsPerPage,
|
||||
HasNextPage: len(res) > h.Config.ItemsPerPage,
|
||||
}
|
||||
|
||||
for i := 0; i <= len(res)-1; i++ {
|
||||
|
|
@ -238,7 +236,7 @@ func (h *Handler) UserList(ctx echo.Context) (*EntityList, error) {
|
|||
res[i].Name,
|
||||
res[i].Email,
|
||||
fmt.Sprint(res[i].Verified),
|
||||
fmt.Sprint(res[i].CreatedAt),
|
||||
res[i].CreatedAt.Format(h.Config.TimeFormat),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
|
@ -257,9 +255,9 @@ func (h *Handler) UserGet(ctx echo.Context, id int) error {
|
|||
}
|
||||
|
||||
func (h *Handler) getOffset(ctx echo.Context) int {
|
||||
if page, err := strconv.Atoi(ctx.QueryParam("page")); err == nil {
|
||||
if page, err := strconv.Atoi(ctx.QueryParam(h.Config.PageQueryKey)); err == nil {
|
||||
if page > 1 {
|
||||
return (page - 1) * h.itemsPerPage
|
||||
return (page - 1) * h.Config.ItemsPerPage
|
||||
}
|
||||
}
|
||||
return 0
|
||||
|
|
|
|||
|
|
@ -1,34 +0,0 @@
|
|||
{{/* Tell Intellij/GoLand to enable the autocompletion based on the *gen.Graph type. */}}
|
||||
{{/* gotype: entgo.io/ent/entc/gen.Graph */}}
|
||||
|
||||
{{ define "admin/types" }}
|
||||
// Code generated by ent, DO NOT EDIT.
|
||||
package admin
|
||||
|
||||
{{ range $n := $.Nodes }}
|
||||
type {{ $n.Name }} struct {
|
||||
// Fields.
|
||||
{{- range $f := $n.Fields }}
|
||||
{{ fieldName $f.Name }} {{ $f.Type }} `form:"{{ $f.Name }}"`
|
||||
{{- end }}
|
||||
// Edges.
|
||||
{{- range $e := $n.Edges }}
|
||||
{{- if not $e.Inverse}}
|
||||
// {{ fieldName $e.Name }} int `form:"{{ $e.Name }}"`
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
}
|
||||
{{ end }}
|
||||
|
||||
type EntityList struct {
|
||||
Columns []string
|
||||
Entities []EntityValues
|
||||
HasNextPage bool
|
||||
}
|
||||
|
||||
type EntityValues struct {
|
||||
ID int
|
||||
Values []string
|
||||
}
|
||||
|
||||
{{ end }}
|
||||
|
|
@ -21,13 +21,13 @@
|
|||
|
||||
type Handler struct {
|
||||
client *{{ $pkg }}.Client
|
||||
itemsPerPage int
|
||||
Config HandlerConfig
|
||||
}
|
||||
|
||||
func NewHandler(client *{{ $pkg }}.Client, itemsPerPage int) *Handler {
|
||||
func NewHandler(client *{{ $pkg }}.Client, cfg HandlerConfig) *Handler {
|
||||
return &Handler{
|
||||
client: client,
|
||||
itemsPerPage: itemsPerPage,
|
||||
Config: cfg,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -141,7 +141,7 @@
|
|||
func (h *Handler) {{ $n.Name }}List(ctx echo.Context) (*EntityList, error) {
|
||||
res, err := h.client.{{ $n.Name }}.
|
||||
Query().
|
||||
Limit(h.itemsPerPage+1).
|
||||
Limit(h.Config.ItemsPerPage+1).
|
||||
Offset(h.getOffset(ctx)).
|
||||
Order({{ $n.Package }}.ByID(sql.OrderDesc())).
|
||||
All(ctx.Request().Context())
|
||||
|
|
@ -157,14 +157,9 @@
|
|||
"{{ fieldLabel $f.Name }}",
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- range $e := $n.Edges }}
|
||||
{{- if not $e.Inverse}}
|
||||
// "{{ fieldLabel $e.Name }}", ?
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
},
|
||||
Entities: make([]EntityValues, 0, len(res)),
|
||||
HasNextPage: len(res) > h.itemsPerPage,
|
||||
HasNextPage: len(res) > h.Config.ItemsPerPage,
|
||||
}
|
||||
|
||||
for i := 0; i <= len(res)-1; i++ {
|
||||
|
|
@ -175,16 +170,13 @@
|
|||
{{- if not $f.Sensitive }}
|
||||
{{- if eq $f.Type.String "string" }}
|
||||
res[i].{{ fieldName $f.Name }},
|
||||
{{- else if eq $f.Type.String "time.Time" }}
|
||||
res[i].{{ fieldName $f.Name }}.Format(h.Config.TimeFormat),
|
||||
{{- else }}
|
||||
fmt.Sprint(res[i].{{ fieldName $f.Name }}),
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- range $e := $n.Edges }}
|
||||
{{- if not $e.Inverse}}
|
||||
// TODO {{ fieldName $e.Name }} ?
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
},
|
||||
})
|
||||
}
|
||||
|
|
@ -204,9 +196,9 @@
|
|||
{{ end }}
|
||||
|
||||
func (h *Handler) getOffset(ctx echo.Context) int {
|
||||
if page, err := strconv.Atoi(ctx.QueryParam("page")); err == nil {
|
||||
if page, err := strconv.Atoi(ctx.QueryParam(h.Config.PageQueryKey)); err == nil {
|
||||
if page > 1 {
|
||||
return (page-1) * h.itemsPerPage
|
||||
return (page-1) * h.Config.ItemsPerPage
|
||||
}
|
||||
}
|
||||
return 0
|
||||
|
|
|
|||
|
|
@ -7,16 +7,9 @@
|
|||
|
||||
{{ range $n := $.Nodes }}
|
||||
type {{ $n.Name }} struct {
|
||||
// Fields.
|
||||
{{- range $f := $n.Fields }}
|
||||
{{ fieldName $f.Name }} {{ $f.Type }} `form:"{{ $f.Name }}"`
|
||||
{{- end }}
|
||||
// Edges.
|
||||
{{- range $e := $n.Edges }}
|
||||
{{- if not $e.Inverse}}
|
||||
// {{ fieldName $e.Name }} int `form:"{{ $e.Name }}"`
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
}
|
||||
{{ end }}
|
||||
|
||||
|
|
@ -31,4 +24,10 @@
|
|||
Values []string
|
||||
}
|
||||
|
||||
type HandlerConfig struct {
|
||||
ItemsPerPage int
|
||||
PageQueryKey string
|
||||
TimeFormat string
|
||||
}
|
||||
|
||||
{{ end }}
|
||||
|
|
@ -4,22 +4,17 @@ package admin
|
|||
import "time"
|
||||
|
||||
type PasswordToken struct {
|
||||
// Fields.
|
||||
Hash string `form:"hash"`
|
||||
UserID int `form:"user_id"`
|
||||
CreatedAt time.Time `form:"created_at"`
|
||||
// Edges.
|
||||
// User int `form:"user"`
|
||||
}
|
||||
|
||||
type User struct {
|
||||
// Fields.
|
||||
Name string `form:"name"`
|
||||
Email string `form:"email"`
|
||||
Password string `form:"password"`
|
||||
Verified bool `form:"verified"`
|
||||
CreatedAt time.Time `form:"created_at"`
|
||||
// Edges.
|
||||
}
|
||||
|
||||
type EntityList struct {
|
||||
|
|
@ -32,3 +27,9 @@ type EntityValues struct {
|
|||
ID int
|
||||
Values []string
|
||||
}
|
||||
|
||||
type HandlerConfig struct {
|
||||
ItemsPerPage int
|
||||
PageQueryKey string
|
||||
TimeFormat string
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue