Have a few of the basic admin pages working after switching to the API+View pattern.
This commit is contained in:
parent
fa7f886a59
commit
2b40d6fdc3
9 changed files with 188 additions and 29 deletions
10
admin/db.go
10
admin/db.go
|
@ -8,8 +8,8 @@ import (
|
|||
sq "github.com/Masterminds/squirrel"
|
||||
)
|
||||
|
||||
func SelectTable(table string, the_type reflect.Type, limit uint64, offset uint64) ([]reflect.Value, error) {
|
||||
var results []reflect.Value
|
||||
func SelectTable(table string, the_type reflect.Type, limit uint64, offset uint64) ([]interface{}, error) {
|
||||
var results []interface{}
|
||||
sql_query, args, err := sq.Select("*").Limit(limit).Offset(offset).From(table).ToSql()
|
||||
if err != nil { return results, err }
|
||||
|
||||
|
@ -18,11 +18,11 @@ func SelectTable(table string, the_type reflect.Type, limit uint64, offset uint6
|
|||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
the_data := reflect.New(the_type)
|
||||
err = rows.StructScan(the_data.Interface())
|
||||
the_data := reflect.New(the_type).Interface()
|
||||
err = rows.StructScan(the_data)
|
||||
if err != nil { return results, err }
|
||||
|
||||
results = append(results, the_data.Elem())
|
||||
results = append(results, the_data)
|
||||
}
|
||||
|
||||
return results, rows.Err()
|
||||
|
|
|
@ -2,36 +2,85 @@ package admin
|
|||
|
||||
import (
|
||||
"maps"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"zedshaw.games/webapp/data"
|
||||
"zedshaw.games/webapp/api"
|
||||
)
|
||||
|
||||
func GetPageIndex(c *fiber.Ctx) error {
|
||||
func GetApiTableIndex(c *fiber.Ctx) error {
|
||||
var tables []string
|
||||
|
||||
for k := range maps.Keys(data.Models()) {
|
||||
tables = append(tables, k)
|
||||
}
|
||||
|
||||
return c.Render("admin/index", fiber.Map{"Tables": tables})
|
||||
return c.JSON(tables)
|
||||
}
|
||||
|
||||
func GetPageTableIndex(c *fiber.Ctx) error {
|
||||
func GetApiSelectAll(c *fiber.Ctx) error {
|
||||
table := c.Params("table")
|
||||
if table == "" { return c.Redirect("/admin/") }
|
||||
|
||||
fmt.Println("table: ", table)
|
||||
type_is := data.Models()[table]
|
||||
|
||||
result, err := SelectTable(table, type_is, 20, 0);
|
||||
if err != nil { return api.IfErrNil(err, c) }
|
||||
|
||||
return c.Render("admin/table/index", fiber.Map{"Table": result})
|
||||
return c.JSON(result)
|
||||
}
|
||||
|
||||
func GetPageSelectAll(c *fiber.Ctx) error {
|
||||
return c.Render("admin/table/contents", fiber.Map{"Table": c.Params("table")})
|
||||
}
|
||||
|
||||
func GetApiSelectOne(c *fiber.Ctx) error {
|
||||
table := c.Params("table")
|
||||
id, err := strconv.ParseInt(c.Params("id"), 10, 64)
|
||||
if err != nil { return api.IfErrNil(err, c) }
|
||||
|
||||
type_is := data.Models()[table]
|
||||
|
||||
result, err := Get(table, type_is, id)
|
||||
if err != nil { return api.IfErrNil(err, c) }
|
||||
|
||||
return c.JSON(result.Interface())
|
||||
}
|
||||
|
||||
func GetPageSelectOne(c *fiber.Ctx) error {
|
||||
table := c.Params("table")
|
||||
id, err := strconv.ParseInt(c.Params("id"), 10, 64)
|
||||
if err != nil { return api.IfErrNil(err, c) }
|
||||
|
||||
return c.Render("admin/table/view", fiber.Map{
|
||||
"Table": table,
|
||||
"Id": id,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
func PostApiUpdate(c *fiber.Ctx) error {
|
||||
return c.JSON(fiber.Map{})
|
||||
}
|
||||
|
||||
func PutApiInsert(c *fiber.Ctx) error {
|
||||
return c.JSON(fiber.Map{})
|
||||
}
|
||||
|
||||
func DeleteApi(c *fiber.Ctx) error {
|
||||
return c.JSON(fiber.Map{})
|
||||
}
|
||||
|
||||
func Setup(app *fiber.App) {
|
||||
app.Get("/admin/", GetPageIndex);
|
||||
app.Get("/admin/table/:table/", GetPageTableIndex);
|
||||
app.Get("/api/admin/table/", GetApiTableIndex)
|
||||
|
||||
app.Get("/api/admin/table/:table/", GetApiSelectAll)
|
||||
app.Get("/admin/table/:table/", GetPageSelectAll)
|
||||
|
||||
app.Get("/api/admin/table/:table/:id/", GetApiSelectOne)
|
||||
app.Get("/admin/table/:table/:id/", GetPageSelectOne)
|
||||
|
||||
app.Post("/api/admin/table/:table/:id/", PostApiUpdate)
|
||||
app.Put("/api/admin/table/:table/:id/", PutApiInsert)
|
||||
app.Delete("/api/admin/table/:table/:id/", DeleteApi)
|
||||
}
|
||||
|
|
19
pages/admin/table/index.html
Normal file
19
pages/admin/table/index.html
Normal file
|
@ -0,0 +1,19 @@
|
|||
<script>
|
||||
let Rows = new GetJson("/api/admin/table/")
|
||||
console.log("Rows", Rows)
|
||||
</script>
|
||||
|
||||
<blockstart>
|
||||
<h1>Admin Rows</h1>
|
||||
|
||||
<block x-data="Rows">
|
||||
<ul>
|
||||
<template x-for="item in theData">
|
||||
<li><a x-bind:href="'/admin/table/' + item + '/'">
|
||||
<span x-text="item"></span>
|
||||
</a></li>
|
||||
</template>
|
||||
</ul>
|
||||
</block>
|
||||
|
||||
</blockstart>
|
63
public/admin/table/index.html
Normal file
63
public/admin/table/index.html
Normal file
|
@ -0,0 +1,63 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<link rel="stylesheet" href="/global.css">
|
||||
<link rel="stylesheet" href="/color.css">
|
||||
<link rel="stylesheet" href="/blockstart.css">
|
||||
<script defer src="/js/alpine.js"></script>
|
||||
<script src="/js/code.js"></script>
|
||||
<title>ZedShaw.games</title>
|
||||
</head>
|
||||
<body data-testid="admin-table-index-page">
|
||||
<header>
|
||||
<blockstart style="background-color: var(--value0)">
|
||||
<block style="--value: 0; --text: 9" class="horizontal">
|
||||
<a id="home" href="/">🏡</a>
|
||||
<a id="live" href="/live/">Live</a>
|
||||
<a id="stream" href="/stream/">Streams</a>
|
||||
<a id="game" href="/game/">Games</a>
|
||||
<a id="register" href="/register/">Register</a>
|
||||
<a id="login" href="/login/">Login</a>
|
||||
</block>
|
||||
</blockstart>
|
||||
</header>
|
||||
|
||||
<script>
|
||||
let Tables = new GetJson("/api/admin/table/")
|
||||
console.log("Tables", Tables)
|
||||
</script>
|
||||
|
||||
<blockstart>
|
||||
<h1>Admin Tables</h1>
|
||||
|
||||
<block x-data="Tables">
|
||||
<ul>
|
||||
<template x-for="item in theData">
|
||||
<li><a x-bind:href="'/admin/table/' + item + '/'">
|
||||
<span x-text="item"></span>
|
||||
</a></li>
|
||||
</template>
|
||||
</ul>
|
||||
</block>
|
||||
|
||||
</blockstart>
|
||||
|
||||
|
||||
<footer>
|
||||
<blockstart style="--value: 0; --text: 9">
|
||||
<block class="horizontal">
|
||||
<shape style="--w: 200px; --h: 250px">Zed Pic</shape>
|
||||
<div>
|
||||
<h3>About Me</h3>
|
||||
<p>Blah blah about me.</p>
|
||||
</div>
|
||||
<div>
|
||||
<h3>Other Projects</h3>
|
||||
<p>Some other links to stuff.</p>
|
||||
</div>
|
||||
</block>
|
||||
</blockstart>
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
|
@ -45,9 +45,9 @@ func TestAdminIndexPage(t *testing.T) {
|
|||
|
||||
for i, row := range all_rows { fmt.Println("row", i, row) }
|
||||
assert.Equal(20, len(all_rows))
|
||||
first_row := all_rows[0]
|
||||
first_row := all_rows[0].(*data.User)
|
||||
|
||||
result, err := admin.Get(table, model, first_row.FieldByName("Id").Int())
|
||||
result, err := admin.Get(table, model, int64(first_row.Id))
|
||||
if err != nil { fmt.Println("ERROR", err) }
|
||||
fmt.Println("TABLE: ", result)
|
||||
|
||||
|
|
|
@ -1,13 +0,0 @@
|
|||
<blockstart>
|
||||
|
||||
<h1>Admin Tables</h1>
|
||||
|
||||
<block>
|
||||
{{range $index, $element := .Tables}}
|
||||
<ul>
|
||||
<li><a href="/admin/table/{{ $element }}/">{{ $element }}</a></li>
|
||||
</ul>
|
||||
{{ end }}
|
||||
</block>
|
||||
|
||||
</blockstart>
|
20
views/admin/table/contents.html
Normal file
20
views/admin/table/contents.html
Normal file
|
@ -0,0 +1,20 @@
|
|||
<script>
|
||||
let Tables = new GetJson("/api/admin/table/{{ .Table }}/")
|
||||
console.log("Tables", Tables)
|
||||
</script>
|
||||
|
||||
<blockstart>
|
||||
<h1>Admin Tables</h1>
|
||||
|
||||
<block x-data="Tables">
|
||||
<ul>
|
||||
<template x-for="item in theData">
|
||||
<table>
|
||||
<td><a x-bind:href="'/admin/table/{{ .Table }}/' + item.id + '/'" x-text="item.id"></td>
|
||||
<td x-text="item.Email"></td>
|
||||
</table>
|
||||
</template>
|
||||
</ul>
|
||||
</block>
|
||||
|
||||
</blockstart>
|
15
views/admin/table/view.html
Normal file
15
views/admin/table/view.html
Normal file
|
@ -0,0 +1,15 @@
|
|||
<script>
|
||||
let Data = new GetJson("/api/admin/table/{{ .Table }}/{{ .Id }}/")
|
||||
console.log("Data", Data)
|
||||
</script>
|
||||
|
||||
<blockstart>
|
||||
<h1>Admin Tables</h1>
|
||||
|
||||
<block x-data="Data">
|
||||
<ul>
|
||||
<h1 x-text="item.id"></h1>
|
||||
</ul>
|
||||
</block>
|
||||
|
||||
</blockstart>
|
|
@ -34,8 +34,14 @@ func RenderPages(pages_path string, target string, layout string) {
|
|||
prefixed_path := append([]string{target}, split_path...)
|
||||
|
||||
target_path := filepath.Join(prefixed_path...)
|
||||
_, err := os.Stat(target_path)
|
||||
|
||||
// compare time stamps and skip if not newer
|
||||
if os.IsNotExist(err) {
|
||||
log.Println("MAKING: ", "MAKE THE DAMN PATH NO FILE")
|
||||
// os.MkdirAll(target_path, 0750)
|
||||
}
|
||||
|
||||
// TODO: compare time stamps and skip if not newer
|
||||
|
||||
out, err := os.OpenFile(target_path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
|
||||
if err != nil { return Fail(err, "writing file %s", target_path) }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue