Added ogent for entity code gen.
This commit is contained in:
parent
9a92c4aad6
commit
196d34cc1f
29 changed files with 13445 additions and 12 deletions
|
|
@ -13,6 +13,7 @@ import (
|
|||
"entgo.io/ent/entc/load"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/mikestefanello/pagoda/ent"
|
||||
"github.com/mikestefanello/pagoda/ent/ogent"
|
||||
"github.com/mikestefanello/pagoda/ent/passwordtoken"
|
||||
"github.com/mikestefanello/pagoda/ent/user"
|
||||
"github.com/mikestefanello/pagoda/pkg/msg"
|
||||
|
|
@ -29,6 +30,7 @@ const entityIDContextKey = "admin:entity_id"
|
|||
type Admin struct {
|
||||
orm *ent.Client
|
||||
graph *gen.Graph
|
||||
ogent *ogent.OgentHandler
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
|
@ -38,6 +40,7 @@ func init() {
|
|||
func (h *Admin) Init(c *services.Container) error {
|
||||
h.graph = c.Graph
|
||||
h.orm = c.ORM
|
||||
h.ogent = ogent.NewOgentHandler(h.orm)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
@ -116,6 +119,12 @@ func (h *Admin) EntityAdd(p AdminEntityPlugin) echo.HandlerFunc {
|
|||
|
||||
func (h *Admin) EntityAddSubmit(p AdminEntityPlugin) echo.HandlerFunc {
|
||||
return func(ctx echo.Context) error {
|
||||
var v ogent.CreatePasswordTokenReq // TODO type
|
||||
err := ctx.Bind(&v)
|
||||
if err != nil {
|
||||
return fail(err, fmt.Sprintf("failed to bind create password token request body"))
|
||||
}
|
||||
fmt.Printf("%+v", v)
|
||||
return h.EntityAdd(p)(ctx)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,13 @@ import (
|
|||
)
|
||||
|
||||
func JS(r *ui.Request) Node {
|
||||
return Group{
|
||||
Script(Src("https://unpkg.com/htmx.org@2.0.0/dist/htmx.min.js")),
|
||||
Script(Src("https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js"), Defer()),
|
||||
}
|
||||
}
|
||||
|
||||
func HtmxListeners(r *ui.Request) Node {
|
||||
const htmxErr = `
|
||||
document.body.addEventListener('htmx:beforeSwap', function(evt) {
|
||||
if (evt.detail.xhr.status >= 400){
|
||||
|
|
@ -28,8 +35,6 @@ func JS(r *ui.Request) Node {
|
|||
`
|
||||
|
||||
return Group{
|
||||
Script(Src("https://unpkg.com/htmx.org@2.0.0/dist/htmx.min.js")),
|
||||
Script(Src("https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js"), Defer()),
|
||||
Script(Raw(htmxErr)),
|
||||
Iff(len(r.CSRF) > 0, func() Node {
|
||||
return Script(Raw(fmt.Sprintf(htmxCSRF, r.CSRF)))
|
||||
|
|
|
|||
|
|
@ -36,6 +36,7 @@ func Admin(r *ui.Request, content Node) Node {
|
|||
),
|
||||
),
|
||||
),
|
||||
HtmxListeners(r),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,9 @@ package pages
|
|||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
|
||||
"entgo.io/ent/entc/load"
|
||||
"entgo.io/ent/schema/field"
|
||||
|
|
@ -40,19 +43,29 @@ func AdminEntityAdd(ctx echo.Context, schema *load.Schema) error {
|
|||
|
||||
nodes := make(Group, 0)
|
||||
|
||||
label := func(name string) string {
|
||||
if len(name) == 0 {
|
||||
return name
|
||||
}
|
||||
text := []rune(strings.ReplaceAll(name, "_", " "))
|
||||
text[0] = unicode.ToUpper(text[0])
|
||||
return string(text)
|
||||
}
|
||||
|
||||
for _, f := range schema.Fields {
|
||||
switch f.Info.Type {
|
||||
case field.TypeString:
|
||||
nodes = append(nodes, InputField(InputFieldParams{
|
||||
Name: f.Name,
|
||||
InputType: "text",
|
||||
Label: f.Name,
|
||||
Label: label(f.Name),
|
||||
}))
|
||||
case field.TypeTime:
|
||||
nodes = append(nodes, InputField(InputFieldParams{
|
||||
Name: f.Name,
|
||||
InputType: "datetime",
|
||||
Label: f.Name,
|
||||
InputType: "text",
|
||||
Label: label(f.Name),
|
||||
Help: fmt.Sprintf("Use the following format: %s", time.Now().Format(time.RFC3339)),
|
||||
}))
|
||||
case field.TypeBool:
|
||||
nodes = append(nodes, P(Textf("%s not supported", f.Name)))
|
||||
|
|
@ -68,16 +81,19 @@ func AdminEntityAdd(ctx echo.Context, schema *load.Schema) error {
|
|||
nodes = append(nodes, InputField(InputFieldParams{
|
||||
Name: e.Name,
|
||||
InputType: "number",
|
||||
Label: e.Name,
|
||||
Label: label(e.Name),
|
||||
}))
|
||||
}
|
||||
|
||||
nodes = append(nodes, ControlGroup(
|
||||
FormButton("is-primary", "Submit"),
|
||||
ButtonLink("/", "is-secondary", "Cancel"),
|
||||
ButtonLink("/", "is-secondary", "Cancel"), // todo
|
||||
), CSRF(r))
|
||||
|
||||
return r.Render(layouts.Admin, Form(Method(http.MethodPost), nodes))
|
||||
return r.Render(layouts.Admin, Form(
|
||||
Method(http.MethodPost),
|
||||
nodes,
|
||||
))
|
||||
}
|
||||
|
||||
type AdminEntityListParams struct {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue