diff --git a/admin/admin.go b/admin/admin.go index b5285f8..3fdbde3 100644 --- a/admin/admin.go +++ b/admin/admin.go @@ -1,6 +1,7 @@ package admin import ( + "encoding/json" "html/template" "net/http" "nilfm.cc/git/felt/admin/util" @@ -66,16 +67,16 @@ func apiGetTableData(next http.Handler, udb auth.UserStore, dbAdapter mongodb.Db func apiCreateTable(next http.Handler, udb auth.UserStore, dbAdapter mongodb.DbAdapter) http.Handler { handlerFunc := func(w http.ResponseWriter, req *http.Request) { - tableName := req.Context().Value("Slug") - tablePass := req.Form["passcode"][0] - - tableKey := models.TableKey{ - Name: tableName.(string), - Passcode: tablePass, - } + tableKey := models.TableKey{} + err := json.NewDecoder(req.Body).Decode(&tableKey) + if err != nil { + w.WriteHeader(400) + next.ServeHTTP(w, req) + return + } // table name is primary key so w edon't need to check - err := dbAdapter.CreateTable(tableKey) + err = dbAdapter.CreateTable(tableKey) if err != nil { AddContextValue(req, "result", err.Error()) diff --git a/go.mod b/go.mod index 1cbe4e4..142673f 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( go.mongodb.org/mongo-driver v1.11.0 golang.org/x/time v0.1.0 nhooyr.io/websocket v1.8.7 - nilfm.cc/git/quartzgun v0.2.2 + nilfm.cc/git/quartzgun v0.2.1 ) require ( diff --git a/go.sum b/go.sum index efdb6e2..ddfa125 100644 --- a/go.sum +++ b/go.sum @@ -105,3 +105,5 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= nhooyr.io/websocket v1.8.7 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g= nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0= +nilfm.cc/git/quartzgun v0.2.1 h1:R2Mi07c+nzaZL+x0atPXBoPoOXvDiLKqi3lcl81T6BA= +nilfm.cc/git/quartzgun v0.2.1/go.mod h1:/DDvt1DtzNuUf3HHaP29WMei/kkdaRW+ySmEzybvVto= diff --git a/models/models.go b/models/models.go index 4dcc435..f0fe47b 100644 --- a/models/models.go +++ b/models/models.go @@ -5,8 +5,8 @@ import ( ) type TableKey struct { - Name string - Passcode string + Name string `json:name` + Passcode string `json:passcode` } type DiceRoll struct { diff --git a/static/admin.js b/static/admin.js index 4ecd62c..fcc8855 100644 --- a/static/admin.js +++ b/static/admin.js @@ -1,17 +1,31 @@ let adminToken = null; +const adminWrapper = document.getElementById("adminWrapper"); +const adminZone = document.getElementById("adminZone"); +const createTableForm = document.getElementById("createTableForm"); +const newTableName = document.getElementById("newTableName"); +const newTablePass = document.getElementById("newTablePass"); async function getTables() { try { const headers = new Headers(); - self.set('Authorization', 'Bearer ' + adminToken.access_token); + headers.set('Authorization', 'Bearer ' + adminToken.access_token); const res = await fetch('/admin/api/table/', { method: 'GET', headers: headers }); if (res.ok) { + const tableList = await res.json(); + let tableListHTML = "