-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtreedata.go
95 lines (76 loc) · 2.27 KB
/
treedata.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package treedata
import (
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"webserv/common"
)
// ***********************************************************
// This JsonData should agree with Angular-side interface
type JsonData struct {
Data string `json:"data"`
} // *********************************************************
// Angular-side interface in src/app/services/nodeservice.ts
// export interface JsonData {
// data: string;
// }
// ***********************************************************
// Small-case non-exported local identifier
type tData struct {
Jdata JsonData
Pgx *common.Pgx
}
// Constructor pattern using factory method
func TData(pgx *common.Pgx) *tData {
t := new(tData)
t.Pgx = pgx
return t
}
// Controller for url "/api/postjsonstring"
func (t *tData) PostJsonData(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
var err error
var unmarshalTypeError *json.UnmarshalTypeError
decoder := json.NewDecoder(r.Body)
if err = decoder.Decode(&t.Jdata); err != nil {
if errors.As(err, &unmarshalTypeError) {
jsonResponse(w, http.StatusBadRequest, "Error wrong data type: "+unmarshalTypeError.Field)
} else {
jsonResponse(w, http.StatusBadRequest, "Error: "+err.Error())
}
return
}
// Save json data to db
if err = t.saveJsonData(); err != nil {
errmsg := "Postgresql exec error:" + err.Error()
log.Print(errmsg)
jsonResponse(w, http.StatusInternalServerError, errmsg)
return
}
jsonResponse(w, http.StatusOK, "Success")
return
}
log.Print("http.NotFound")
http.NotFound(w, r)
}
// Save json data to db
func (t *tData) saveJsonData() error {
// Print the data from the client
log.Println("jsonData:", t.Jdata.Data)
// SQL statement to call the stored-function
sql := "select tree_insert($1)"
// Call the Postgresql stored-function
if _, err := t.Pgx.Con.Exec(t.Pgx.Ctx, sql, t.Jdata.Data); err != nil {
return err
}
return nil
}
func jsonResponse(w http.ResponseWriter, statusCode int, errorMsg string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
// For production, use generic "Bad request or data error".
// Detailed error message is not advised in production.
w.Write([]byte(fmt.Sprintf(`{"message": "%s"}`, errorMsg) + "\n"))
}