Connected nodes
This commit is contained in:
parent
2b8472bcd1
commit
ca0659a368
9 changed files with 581 additions and 89 deletions
92
webserver.go
92
webserver.go
|
|
@ -4,9 +4,11 @@ import (
|
|||
// External
|
||||
"git.ahall.se/go/html_template"
|
||||
werr "git.gibonuddevalla.se/go/wrappederror"
|
||||
"github.com/lib/pq"
|
||||
|
||||
// Standard
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
|
|
@ -42,8 +44,11 @@ func initWebserver() (err error) {
|
|||
http.HandleFunc("/nodes/create", actionNodeCreate)
|
||||
http.HandleFunc("/nodes/move", actionNodeMove)
|
||||
http.HandleFunc("/nodes/search", actionNodeSearch)
|
||||
http.HandleFunc("/nodes/connect", actionNodeConnect)
|
||||
http.HandleFunc("/types/{typeID}", actionType)
|
||||
http.HandleFunc("/types/", actionTypesAll)
|
||||
http.HandleFunc("/connection/update/{connID}", actionConnectionUpdate)
|
||||
http.HandleFunc("/connection/delete/{connID}", actionConnectionDelete)
|
||||
|
||||
err = http.ListenAndServe(address, nil)
|
||||
return
|
||||
|
|
@ -250,7 +255,7 @@ func actionNodeConnections(w http.ResponseWriter, r *http.Request) { // {{{
|
|||
}
|
||||
|
||||
out := struct {
|
||||
OK bool
|
||||
OK bool
|
||||
Nodes []Node
|
||||
}{
|
||||
true,
|
||||
|
|
@ -280,7 +285,7 @@ func actionNodeMove(w http.ResponseWriter, r *http.Request) { // {{{
|
|||
}
|
||||
|
||||
out := struct {
|
||||
OK bool
|
||||
OK bool
|
||||
}{
|
||||
true,
|
||||
}
|
||||
|
|
@ -312,8 +317,8 @@ func actionNodeSearch(w http.ResponseWriter, r *http.Request) { // {{{
|
|||
}
|
||||
|
||||
out := struct {
|
||||
OK bool
|
||||
Nodes []Node
|
||||
OK bool
|
||||
Nodes []Node
|
||||
MoreExistThan int
|
||||
}{
|
||||
true,
|
||||
|
|
@ -328,6 +333,37 @@ func actionNodeSearch(w http.ResponseWriter, r *http.Request) { // {{{
|
|||
j, _ := json.Marshal(out)
|
||||
w.Write(j)
|
||||
} // }}}
|
||||
func actionNodeConnect(w http.ResponseWriter, r *http.Request) { // {{{
|
||||
var req struct {
|
||||
ParentNodeID int
|
||||
ChildNodeID int
|
||||
}
|
||||
|
||||
body, _ := io.ReadAll(r.Body)
|
||||
err := json.Unmarshal(body, &req)
|
||||
if err != nil {
|
||||
err = werr.Wrap(err)
|
||||
httpError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
err = ConnectNode(req.ParentNodeID, req.ChildNodeID)
|
||||
if err != nil {
|
||||
pqErr, ok := err.(*pq.Error)
|
||||
if ok && pqErr.Code == "23505" {
|
||||
err = errors.New("This node is already connected.")
|
||||
} else {
|
||||
err = werr.Wrap(err)
|
||||
}
|
||||
httpError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
res := struct{ OK bool }{true}
|
||||
j, _ := json.Marshal(res)
|
||||
w.Write(j)
|
||||
|
||||
} // }}}
|
||||
func actionType(w http.ResponseWriter, r *http.Request) { // {{{
|
||||
typeID := 0
|
||||
typeIDStr := r.PathValue("typeID")
|
||||
|
|
@ -362,5 +398,53 @@ func actionTypesAll(w http.ResponseWriter, r *http.Request) { // {{{
|
|||
j, _ := json.Marshal(out)
|
||||
w.Write(j)
|
||||
} // }}}
|
||||
func actionConnectionUpdate(w http.ResponseWriter, r *http.Request) { // {{{
|
||||
connIDStr := r.PathValue("connID")
|
||||
connID, err := strconv.Atoi(connIDStr)
|
||||
if err != nil {
|
||||
err = werr.Wrap(err)
|
||||
httpError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
data, _ := io.ReadAll(r.Body)
|
||||
|
||||
err = UpdateConnection(connID, data)
|
||||
if err != nil {
|
||||
httpError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
out := struct {
|
||||
OK bool
|
||||
}{
|
||||
true,
|
||||
}
|
||||
j, _ := json.Marshal(out)
|
||||
w.Write(j)
|
||||
} // }}}
|
||||
func actionConnectionDelete(w http.ResponseWriter, r *http.Request) { // {{{
|
||||
connIDStr := r.PathValue("connID")
|
||||
connID, err := strconv.Atoi(connIDStr)
|
||||
if err != nil {
|
||||
err = werr.Wrap(err)
|
||||
httpError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
err = DeleteConnection(connID)
|
||||
if err != nil {
|
||||
httpError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
out := struct {
|
||||
OK bool
|
||||
}{
|
||||
true,
|
||||
}
|
||||
j, _ := json.Marshal(out)
|
||||
w.Write(j)
|
||||
} // }}}
|
||||
|
||||
// vim: foldmethod=marker
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue