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
|
||||
}
|
||||
|
||||
func Insert(table string, value reflect.Value) error {
|
||||
func Insert(table string, value reflect.Value) (int64, int64, error) {
|
||||
type_of := value.Type()
|
||||
field_num := value.NumField()
|
||||
var columns []string
|
||||
|
@ -64,9 +64,14 @@ func Insert(table string, value reflect.Value) error {
|
|||
builder := sq.Insert(table).Columns(columns...).Values(values...)
|
||||
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 {
|
||||
|
|
|
@ -3,6 +3,8 @@ package admin
|
|||
import (
|
||||
"maps"
|
||||
"strconv"
|
||||
"reflect"
|
||||
"fmt"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"zedshaw.games/webapp/data"
|
||||
. "zedshaw.games/webapp/common"
|
||||
|
@ -60,7 +62,6 @@ func GetPageSelectOne(c *fiber.Ctx) error {
|
|||
|
||||
func PostApiUpdate(c *fiber.Ctx) error {
|
||||
table := c.Params("table")
|
||||
|
||||
typeOf := data.Models()[table]
|
||||
obj, err := ReflectOnPost(typeOf, c)
|
||||
if err != nil { return IfErrNil(err, c) }
|
||||
|
@ -73,14 +74,27 @@ func PostApiUpdate(c *fiber.Ctx) error {
|
|||
|
||||
func GetPageInsert(c *fiber.Ctx) error {
|
||||
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 })
|
||||
}
|
||||
|
||||
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 {
|
||||
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 {
|
||||
|
@ -102,6 +116,7 @@ func Setup(app *fiber.App) {
|
|||
app.Get("/admin/table/:table/", GetPageSelectAll)
|
||||
|
||||
app.Get("/admin/new/table/:table/", GetPageInsert)
|
||||
app.Get("/api/admin/new/table/:table/", GetApiInsert)
|
||||
app.Post("/api/admin/new/table/:table/", PostApiInsert)
|
||||
|
||||
app.Get("/api/admin/table/:table/:id/", GetApiSelectOne)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue