Now can do all CRUD and just need search and pagination.
This commit is contained in:
parent
b0ba0c7e16
commit
e178ca6733
5 changed files with 62 additions and 10 deletions
11
admin/db.go
11
admin/db.go
|
@ -47,7 +47,7 @@ func Delete(table string, id int64) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func Insert(table string, value reflect.Value) error {
|
func Insert(table string, value reflect.Value) (int64, int64, error) {
|
||||||
type_of := value.Type()
|
type_of := value.Type()
|
||||||
field_num := value.NumField()
|
field_num := value.NumField()
|
||||||
var columns []string
|
var columns []string
|
||||||
|
@ -64,9 +64,14 @@ func Insert(table string, value reflect.Value) error {
|
||||||
builder := sq.Insert(table).Columns(columns...).Values(values...)
|
builder := sq.Insert(table).Columns(columns...).Values(values...)
|
||||||
sql_query, args, err := builder.ToSql()
|
sql_query, args, err := builder.ToSql()
|
||||||
|
|
||||||
_, err = data.DB.Exec(sql_query, args...)
|
result, err := data.DB.Exec(sql_query, args...)
|
||||||
|
id, err := result.LastInsertId()
|
||||||
|
if err != nil { return -1, -1, err }
|
||||||
|
|
||||||
return err
|
count, err := result.RowsAffected()
|
||||||
|
if err != nil { return id, -1, err }
|
||||||
|
|
||||||
|
return id, count, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func Update(table string, value reflect.Value) error {
|
func Update(table string, value reflect.Value) error {
|
||||||
|
|
|
@ -3,6 +3,8 @@ package admin
|
||||||
import (
|
import (
|
||||||
"maps"
|
"maps"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"reflect"
|
||||||
|
"fmt"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"zedshaw.games/webapp/data"
|
"zedshaw.games/webapp/data"
|
||||||
. "zedshaw.games/webapp/common"
|
. "zedshaw.games/webapp/common"
|
||||||
|
@ -60,7 +62,6 @@ func GetPageSelectOne(c *fiber.Ctx) error {
|
||||||
|
|
||||||
func PostApiUpdate(c *fiber.Ctx) error {
|
func PostApiUpdate(c *fiber.Ctx) error {
|
||||||
table := c.Params("table")
|
table := c.Params("table")
|
||||||
|
|
||||||
typeOf := data.Models()[table]
|
typeOf := data.Models()[table]
|
||||||
obj, err := ReflectOnPost(typeOf, c)
|
obj, err := ReflectOnPost(typeOf, c)
|
||||||
if err != nil { return IfErrNil(err, c) }
|
if err != nil { return IfErrNil(err, c) }
|
||||||
|
@ -73,14 +74,27 @@ func PostApiUpdate(c *fiber.Ctx) error {
|
||||||
|
|
||||||
func GetPageInsert(c *fiber.Ctx) error {
|
func GetPageInsert(c *fiber.Ctx) error {
|
||||||
table := c.Params("table")
|
table := c.Params("table")
|
||||||
|
|
||||||
// NOTE: need to get a blank json for the table schema and use that to populate the template OR have a simple get that does it. Probably the former.
|
|
||||||
|
|
||||||
return c.Render("admin/table/new", fiber.Map{ "Table": table })
|
return c.Render("admin/table/new", fiber.Map{ "Table": table })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetApiInsert(c *fiber.Ctx) error {
|
||||||
|
table := c.Params("table")
|
||||||
|
typeOf := data.Models()[table]
|
||||||
|
result := reflect.New(typeOf)
|
||||||
|
return c.JSON(result.Interface())
|
||||||
|
}
|
||||||
|
|
||||||
func PostApiInsert(c *fiber.Ctx) error {
|
func PostApiInsert(c *fiber.Ctx) error {
|
||||||
return c.JSON(fiber.Map{})
|
table := c.Params("table")
|
||||||
|
|
||||||
|
typeOf := data.Models()[table]
|
||||||
|
obj, err := ReflectOnPost(typeOf, c)
|
||||||
|
if err != nil { return IfErrNil(err, c) }
|
||||||
|
|
||||||
|
id, _, err := Insert(table, obj.Elem())
|
||||||
|
if err != nil { return IfErrNil(err, c) }
|
||||||
|
|
||||||
|
return c.Redirect(fmt.Sprintf("/admin/table/%s/%d/", table, id), 303)
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteApi(c *fiber.Ctx) error {
|
func DeleteApi(c *fiber.Ctx) error {
|
||||||
|
@ -102,6 +116,7 @@ func Setup(app *fiber.App) {
|
||||||
app.Get("/admin/table/:table/", GetPageSelectAll)
|
app.Get("/admin/table/:table/", GetPageSelectAll)
|
||||||
|
|
||||||
app.Get("/admin/new/table/:table/", GetPageInsert)
|
app.Get("/admin/new/table/:table/", GetPageInsert)
|
||||||
|
app.Get("/api/admin/new/table/:table/", GetApiInsert)
|
||||||
app.Post("/api/admin/new/table/:table/", PostApiInsert)
|
app.Post("/api/admin/new/table/:table/", PostApiInsert)
|
||||||
|
|
||||||
app.Get("/api/admin/table/:table/:id/", GetApiSelectOne)
|
app.Get("/api/admin/table/:table/:id/", GetApiSelectOne)
|
||||||
|
|
|
@ -36,7 +36,7 @@ func TestAdminIndexPage(t *testing.T) {
|
||||||
_, err = data.DB.Exec(sql_query, args...)
|
_, err = data.DB.Exec(sql_query, args...)
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
|
|
||||||
err = admin.Insert(table, val)
|
id, count, err = admin.Insert(table, val)
|
||||||
assert.NoError(err, email)
|
assert.NoError(err, email)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,12 +7,13 @@
|
||||||
<h1>Admin Tables</h1>
|
<h1>Admin Tables</h1>
|
||||||
|
|
||||||
<block x-data="Tables">
|
<block x-data="Tables">
|
||||||
|
<button type="button"><a href="/admin/new/table/{{ .Table }}/">New</a></button>
|
||||||
<table>
|
<table>
|
||||||
<template x-for="item in theData">
|
<template x-for="item in theData">
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
<a x-bind:href="'/admin/table/{{ .Table }}/' + item.id + '/'">
|
<a x-bind:href="'/admin/table/{{ .Table }}/' + item.id + '/'">
|
||||||
^
|
#
|
||||||
</a>
|
</a>
|
||||||
</td>
|
</td>
|
||||||
<template x-for="(value, key) in item">
|
<template x-for="(value, key) in item">
|
||||||
|
|
31
views/admin/table/new.html
Normal file
31
views/admin/table/new.html
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
<script>
|
||||||
|
let Data = new GetJson("/api/admin/new/table/{{ .Table }}/");
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<blockstart>
|
||||||
|
<h1>Admin Tables</h1>
|
||||||
|
|
||||||
|
<block x-init="item = await Data.oneThing()" x-data="{item: {}}">
|
||||||
|
<form method="POST" action="/api/admin/new/table/{{ .Table }}/">
|
||||||
|
<card>
|
||||||
|
<top><h2>{{ .Table }} : New</h2></top>
|
||||||
|
<middle>
|
||||||
|
<template x-for="(value, key) in item">
|
||||||
|
<div>
|
||||||
|
<label x-text="key" x-bind:for="key"></label>
|
||||||
|
<input x-bind:name="key" x-text="value" x-model="item[key]" x-bind:id="key" />
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
</middle>
|
||||||
|
<bottom>
|
||||||
|
<button-group>
|
||||||
|
<button type="button"><a href="/admin/table/{{ .Table }}/">Back</a></button>
|
||||||
|
<button type="submit">Insert</button>
|
||||||
|
<button type="button">Clear</button>
|
||||||
|
</button-group>
|
||||||
|
</bottom>
|
||||||
|
</card>
|
||||||
|
</form>
|
||||||
|
</block>
|
||||||
|
|
||||||
|
</blockstart>
|
Loading…
Add table
Add a link
Reference in a new issue