First stab at a form submit off an alpine.js form. Honestly not great. It's a lot of code already for even less functionality.

main
Zed A. Shaw 2 days ago
parent e4153fd74a
commit 3fd8169d83
  1. 2
      admin/db.go
  2. 11
      admin/handlers.go
  3. 2
      data/models.go
  4. 2
      pages/layouts/main.html
  5. 1
      static/js/code.js
  6. 52
      views/admin/table/view.html
  7. 2
      views/layouts/main.html

@ -85,7 +85,7 @@ func Update(table string, value reflect.Value) error {
builder = builder.Set(tag, field.Interface())
}
builder.Where(sq.Eq{"id": value.FieldByName("Id").Interface()})
builder = builder.Where(sq.Eq{"id": value.FieldByName("Id").Interface()})
sql_query, args, err := builder.ToSql()
fmt.Println("UPDATE QUERY", sql_query, args)

@ -3,6 +3,8 @@ package admin
import (
"maps"
"strconv"
"fmt"
"reflect"
"github.com/gofiber/fiber/v2"
"zedshaw.games/webapp/data"
. "zedshaw.games/webapp/common"
@ -59,7 +61,14 @@ func GetPageSelectOne(c *fiber.Ctx) error {
}
func PostApiUpdate(c *fiber.Ctx) error {
return c.JSON(fiber.Map{})
user, err := ReceivePost[data.User](c)
if err != nil { return IfErrNil(err, c) }
fmt.Println("post received", user)
err = Update(c.Params("table"), reflect.ValueOf(user).Elem())
if err != nil { return IfErrNil(err, c) }
return c.JSON(fiber.Map{"status": "ok"})
}
func PutApiInsert(c *fiber.Ctx) error {

@ -8,7 +8,7 @@ type Login struct {
}
type User struct {
Id int `db:"id" json:"id" validate:"isdefault"`
Id int `db:"id" json:"id" validate:"numeric"`
Username string `db:"username" validate:"required,max=30"`
Email string `db:"email" validate:"required,email,max=128"`
Password string `db:"password" validate:"required,min=8,max=64"`

@ -6,7 +6,7 @@
<meta name="viewport" content="initial-scale=1.0" />
<meta name="author" content="Zed A. Shaw" />
<meta name="description" content="My Go learning project, which is a Twitch support thing." />
<link rel="icon" href="favicon.ico" type="image/x-icon" />
<link rel="icon" href="/favicon.ico" type="image/x-icon" />
<link rel="stylesheet" href="/global.css">
<link rel="stylesheet" href="/color.css">
<link rel="stylesheet" href="/blockstart.css">

@ -1,4 +1,3 @@
class GetJson {
constructor(url) {
this.url = url;

@ -1,31 +1,43 @@
<script>
let Data = new GetJson("/api/admin/table/{{ .Table }}/{{ .Id }}/");
const PostForm = async (url, data) => {
console.log("DATA", JSON.stringify(data));
return await fetch(url, {
method: "POST",
mode: "same-origin",
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json'
}
});
}
</script>
<blockstart>
<h1>Admin Tables</h1>
<block x-init="item = await Data.oneThing()" x-data="{item: {}}">
<form method="POST" action="/api/admin/table/{{ .Table }}/{{ .Id }}/">
<card>
<top><h1>{{ .Table }} : {{ .Id }}</h1></top>
<middle>
<template x-for="(value, key) in item">
<div>
<label x-text="key" x-bind:for="key"></label>
<input x-text="value" x-bind:value="value" x-bind:id="key" />
</div>
</template>
</middle>
<bottom>
<button-group>
<button type="button">Back</button>
<button type="submit">Update</button>
<button type="button">Delete</button>
</button-group>
</bottom>
</card>
</form>
<form method="POST" action="/api/admin/table/{{ .Table }}/{{ .Id }}/">
<card>
<top><h1>{{ .Table }} : {{ .Id }}</h1></top>
<middle>
<template x-for="(value, key) in item">
<div>
<label x-text="key" x-bind:for="key"></label>
<input x-text="value" x-model="item[key]" x-bind:id="key" />
</div>
</template>
</middle>
<bottom>
<button-group>
<button type="button">Back</button>
<button type="button" @click.prevent="PostForm('/api/admin/table/{{ .Table }}/{{ .Id }}/', item)">Update</button>
<button type="button" @click.prevent="console.log('DELETE', item.id)">Delete</button>
</button-group>
</bottom>
</card>
</form>
</block>
</blockstart>

@ -6,7 +6,7 @@
<meta name="viewport" content="initial-scale=1.0" />
<meta name="author" content="Zed A. Shaw" />
<meta name="description" content="My Go learning project, which is a Twitch support thing." />
<link rel="icon" href="favicon.ico" type="image/x-icon" />
<link rel="icon" href="/favicon.ico" type="image/x-icon" />
<link rel="stylesheet" href="/global.css">
<link rel="stylesheet" href="/color.css">
<link rel="stylesheet" href="/blockstart.css">

Loading…
Cancel
Save