Handling of data types
This commit is contained in:
parent
9d50b97436
commit
8988720c0e
6 changed files with 331 additions and 18 deletions
108
webserver.go
108
webserver.go
|
|
@ -47,6 +47,8 @@ func initWebserver() (err error) {
|
|||
http.HandleFunc("/nodes/connect", actionNodeConnect)
|
||||
http.HandleFunc("/types/{typeID}", actionType)
|
||||
http.HandleFunc("/types/", actionTypesAll)
|
||||
http.HandleFunc("/types/create", actionTypeCreate)
|
||||
http.HandleFunc("/types/update/{typeID}", actionTypeUpdate)
|
||||
http.HandleFunc("/connection/update/{connID}", actionConnectionUpdate)
|
||||
http.HandleFunc("/connection/delete/{connID}", actionConnectionDelete)
|
||||
|
||||
|
|
@ -364,6 +366,7 @@ func actionNodeConnect(w http.ResponseWriter, r *http.Request) { // {{{
|
|||
w.Write(j)
|
||||
|
||||
} // }}}
|
||||
|
||||
func actionType(w http.ResponseWriter, r *http.Request) { // {{{
|
||||
typeID := 0
|
||||
typeIDStr := r.PathValue("typeID")
|
||||
|
|
@ -376,7 +379,15 @@ func actionType(w http.ResponseWriter, r *http.Request) { // {{{
|
|||
return
|
||||
}
|
||||
|
||||
j, _ := json.Marshal(typ)
|
||||
res := struct {
|
||||
OK bool
|
||||
Type NodeType
|
||||
}{
|
||||
true,
|
||||
typ,
|
||||
}
|
||||
|
||||
j, _ := json.Marshal(res)
|
||||
w.Write(j)
|
||||
} // }}}
|
||||
func actionTypesAll(w http.ResponseWriter, r *http.Request) { // {{{
|
||||
|
|
@ -398,6 +409,101 @@ func actionTypesAll(w http.ResponseWriter, r *http.Request) { // {{{
|
|||
j, _ := json.Marshal(out)
|
||||
w.Write(j)
|
||||
} // }}}
|
||||
func actionTypeCreate(w http.ResponseWriter, r *http.Request) { // {{{
|
||||
var req struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
body, _ := io.ReadAll(r.Body)
|
||||
err := json.Unmarshal(body, &req)
|
||||
if err != nil {
|
||||
err = werr.Wrap(err)
|
||||
httpError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
var newSchema string
|
||||
if newSchema, err = ValidateType(req.Name, "{}"); err != nil {
|
||||
err = werr.Wrap(err)
|
||||
httpError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
nodeType, err := CreateType(req.Name, newSchema)
|
||||
if err != nil {
|
||||
|
||||
if wrapped, ok := err.(*werr.Error); ok {
|
||||
pqErr, ok := wrapped.Wrapped.(*pq.Error)
|
||||
if ok && pqErr.Code == "23505" {
|
||||
err = errors.New("This type already exist.")
|
||||
} else {
|
||||
err = werr.Wrap(err)
|
||||
}
|
||||
} else {
|
||||
err = werr.Wrap(err)
|
||||
}
|
||||
httpError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
res := struct {
|
||||
OK bool
|
||||
Type NodeType
|
||||
}{
|
||||
true,
|
||||
nodeType,
|
||||
}
|
||||
|
||||
j, _ := json.Marshal(res)
|
||||
w.Write(j)
|
||||
} // }}}
|
||||
func actionTypeUpdate(w http.ResponseWriter, r *http.Request) { // {{{
|
||||
typeID := 0
|
||||
typeIDStr := r.PathValue("typeID")
|
||||
typeID, _ = strconv.Atoi(typeIDStr)
|
||||
|
||||
var req struct {
|
||||
Name string
|
||||
Schema string
|
||||
}
|
||||
|
||||
body, _ := io.ReadAll(r.Body)
|
||||
err := json.Unmarshal(body, &req)
|
||||
if err != nil {
|
||||
err = werr.Wrap(err)
|
||||
httpError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
var fixedSchema string
|
||||
if fixedSchema, err = ValidateType(req.Name, req.Schema); err != nil {
|
||||
if wErr, ok := err.(*werr.Error); ok {
|
||||
if jsonErr, ok := wErr.Wrapped.(*json.SyntaxError); ok {
|
||||
err = jsonErr
|
||||
} else {
|
||||
err = werr.Wrap(err)
|
||||
}
|
||||
} else {
|
||||
err = werr.Wrap(err)
|
||||
}
|
||||
httpError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
err = UpdateType(typeID, req.Name, fixedSchema)
|
||||
if err != nil {
|
||||
err = werr.Wrap(err)
|
||||
httpError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
res := struct {
|
||||
OK bool
|
||||
}{true}
|
||||
j, _ := json.Marshal(res)
|
||||
w.Write(j)
|
||||
} // }}}
|
||||
|
||||
func actionConnectionUpdate(w http.ResponseWriter, r *http.Request) { // {{{
|
||||
connIDStr := r.PathValue("connID")
|
||||
connID, err := strconv.Atoi(connIDStr)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue