admin: flesh out apiGetTableData and add table creation
This commit is contained in:
parent
882e7b14c3
commit
75eb476232
4 changed files with 128 additions and 41 deletions
106
admin/admin.go
106
admin/admin.go
|
@ -1,43 +1,27 @@
|
|||
package admin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"nilfm.cc/git/felt/admin/util"
|
||||
"nilfm.cc/git/felt/models"
|
||||
"nilfm.cc/git/felt/mongodb"
|
||||
"nilfm.cc/git/felt/admin/util"
|
||||
"nilfm.cc/git/quartzgun/auth"
|
||||
. "nilfm.cc/git/quartzgun/middleware"
|
||||
"nilfm.cc/git/quartzgun/renderer"
|
||||
"nilfm.cc/git/quartzgun/router"
|
||||
"strings"
|
||||
. "nilfm.cc/git/quartzgun/util"
|
||||
)
|
||||
|
||||
func apiGetTableList(next http.Handler, udb auth.UserStore) http.Handler {
|
||||
handlerFunc := func(w http.ResponseWriter, req *http.Request) {
|
||||
|
||||
// get username from
|
||||
user := util.GetUserFromToken(req)
|
||||
|
||||
rawTableData, err := udb.GetData(user, "tables")
|
||||
self, err := util.GetTablesByUser(user, udb)
|
||||
if err != nil {
|
||||
// handle error - return 404 or 500?
|
||||
w.WriteHeader(404)
|
||||
} else {
|
||||
AddContextValue(req, "tableList", self)
|
||||
}
|
||||
|
||||
// split rawTableData - tableName,passCode;tableName,passCode;
|
||||
tables := strings.Split(rawTableData.(string), ";")
|
||||
self := []models.TableKey{}
|
||||
for _, t := range tables {
|
||||
parts := strings.Split(t, ",")
|
||||
if len(parts) == 2 {
|
||||
self = append(self, models.TableKey{
|
||||
Name: parts[0],
|
||||
Passcode: parts[1],
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
*req = *req.WithContext(context.WithValue(req.Context(), "tableList", self))
|
||||
next.ServeHTTP(w, req)
|
||||
}
|
||||
|
||||
|
@ -47,23 +31,91 @@ func apiGetTableList(next http.Handler, udb auth.UserStore) http.Handler {
|
|||
func apiGetTableData(next http.Handler, udb auth.UserStore, dbAdapter mongodb.DbAdapter) http.Handler {
|
||||
handlerFunc := func(w http.ResponseWriter, req *http.Request) {
|
||||
|
||||
// tableName := req.Context().Value("Slug")
|
||||
// get table entities from mongodb and store the data object in the context
|
||||
tableName := req.Context().Value("Slug")
|
||||
tablePass := req.Form["passcode"][0]
|
||||
|
||||
tableKey := models.TableKey{
|
||||
Name: tableName.(string),
|
||||
Passcode: tablePass,
|
||||
}
|
||||
|
||||
if dbAdapter.CheckTable(tableKey) {
|
||||
mapUrl, _ := dbAdapter.GetMapImageUrl(tableKey)
|
||||
auxMessage, _ := dbAdapter.GetAuxMessage(tableKey)
|
||||
availableTokens, _ := dbAdapter.GetTokens(tableKey, true)
|
||||
activeTokens, _ := dbAdapter.GetTokens(tableKey, false)
|
||||
|
||||
AddContextValue(req, "tableData", models.Table{
|
||||
Name: tableName.(string),
|
||||
Passcode: tablePass,
|
||||
MapImageUrl: mapUrl,
|
||||
Tokens: activeTokens,
|
||||
AvailableTokens: availableTokens,
|
||||
AuxMessage: auxMessage,
|
||||
})
|
||||
} else {
|
||||
w.WriteHeader(404)
|
||||
}
|
||||
|
||||
next.ServeHTTP(w, req)
|
||||
}
|
||||
|
||||
return http.HandlerFunc(handlerFunc)
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
|
||||
// table name is primary key so w edon't need to check
|
||||
err := dbAdapter.CreateTable(tableKey)
|
||||
|
||||
if err != nil {
|
||||
AddContextValue(req, "result", err.Error())
|
||||
// TODO: parse error and change the status
|
||||
w.WriteHeader(500)
|
||||
} else {
|
||||
user := util.GetUserFromToken(req)
|
||||
tables, err := util.GetTablesByUser(user, udb)
|
||||
tables = append(tables, tableKey)
|
||||
err = util.SetTablesForUser(user, tables, udb)
|
||||
if err != nil {
|
||||
w.WriteHeader(500)
|
||||
} else {
|
||||
w.WriteHeader(201)
|
||||
}
|
||||
}
|
||||
next.ServeHTTP(w, req)
|
||||
}
|
||||
|
||||
return http.HandlerFunc(handlerFunc)
|
||||
}
|
||||
|
||||
func apiDestroyTable(next http.Handler, udb auth.UserStore, dbAdapter mongodb.DbAdapter) http.Handler {
|
||||
handlerFunc := func(w http.ResponseWriter, req *http.Request) {
|
||||
// check table actually belongs to this user
|
||||
// if it does, try to destroy it
|
||||
}
|
||||
|
||||
return http.HandlerFunc(handlerFunc)
|
||||
}
|
||||
|
||||
func CreateAdminInterface(udb auth.UserStore, dbAdapter mongodb.DbAdapter) http.Handler {
|
||||
// create quartzgun router
|
||||
rtr := &router.Router{}
|
||||
|
||||
scopes := map[string]string{}
|
||||
|
||||
rtr.Post("api/auth", Provision(udb, 84))
|
||||
rtr.Get(`api/table/`, Validate(apiGetTableList(renderer.JSON("tableList"), udb), udb, scopes))
|
||||
rtr.Get(`api/table/(?P<Slug>\S+)`, Validate(apiGetTableData(renderer.JSON("tableData"), udb, dbAdapter), udb, scopes))
|
||||
rtr.Post("/api/auth/", Provision(udb, 84))
|
||||
rtr.Get("/api/table/", Validate(apiGetTableList(renderer.JSON("tableList"), udb), udb, scopes))
|
||||
rtr.Get(`/api/table/(?P<Slug>\S+)`, Validate(apiGetTableData(renderer.JSON("tableData"), udb, dbAdapter), udb, scopes))
|
||||
rtr.Post("/api/table/", Validate(apiCreateTable(renderer.JSON("result"), udb, dbAdapter), udb, scopes))
|
||||
|
||||
return http.HandlerFunc(rtr.ServeHTTP)
|
||||
}
|
||||
|
|
|
@ -3,6 +3,8 @@ package util
|
|||
import (
|
||||
"encoding/base64"
|
||||
"net/http"
|
||||
"nilfm.cc/git/felt/models"
|
||||
"nilfm.cc/git/quartzgun/auth"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
@ -20,3 +22,36 @@ func GetUserFromToken(req *http.Request) string {
|
|||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func GetTablesByUser(user string, udb auth.UserStore) ([]models.TableKey, error) {
|
||||
|
||||
rawTableData, err := udb.GetData(user, "tables")
|
||||
if err != nil {
|
||||
return []models.TableKey{}, err
|
||||
}
|
||||
|
||||
// split rawTableData - tableName,passCode;tableName,passCode;
|
||||
tables := strings.Split(rawTableData.(string), ";")
|
||||
self := []models.TableKey{}
|
||||
for _, t := range tables {
|
||||
parts := strings.Split(t, ",")
|
||||
if len(parts) == 2 {
|
||||
self = append(self, models.TableKey{
|
||||
Name: parts[0],
|
||||
Passcode: parts[1],
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return self, nil
|
||||
}
|
||||
|
||||
func SetTablesForUser(user string, tables []models.TableKey, udb auth.UserStore) error {
|
||||
rawTableData := ""
|
||||
|
||||
for _, t := range tables {
|
||||
rawTableData += t.Name + "," + t.Passcode + ";"
|
||||
}
|
||||
rawTableData = strings.TrimSuffix(rawTableData, ";")
|
||||
return udb.SetData(user, "tables", rawTableData)
|
||||
}
|
||||
|
|
|
@ -11,8 +11,8 @@ import (
|
|||
"nilfm.cc/git/felt/admin"
|
||||
"nilfm.cc/git/felt/models"
|
||||
"nilfm.cc/git/felt/mongodb"
|
||||
"nilfm.cc/git/quartzgun/cookie"
|
||||
"nilfm.cc/git/quartzgun/auth"
|
||||
"nilfm.cc/git/quartzgun/cookie"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue