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