forked from HouzuoGuo/tiedot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
document.go
138 lines (133 loc) · 3.16 KB
/
document.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* Document management handlers. */
package v3
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
)
func Insert(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "must-revalidate")
w.Header().Set("Content-Type", "text/plain")
var col, doc string
if !Require(w, r, "col", &col) {
return
}
if !Require(w, r, "doc", &doc) {
return
}
V3Sync.RLock()
defer V3Sync.RUnlock()
dbcol := V3DB.Use(col)
if dbcol == nil {
http.Error(w, fmt.Sprintf("Collection '%s' does not exist.", col), 400)
return
}
var jsonDoc map[string]interface{}
if err := json.Unmarshal([]byte(doc), &jsonDoc); err != nil {
http.Error(w, fmt.Sprintf("'%v' is not valid JSON document.", doc), 400)
return
}
id, err := dbcol.Insert(jsonDoc)
if err != nil {
http.Error(w, fmt.Sprint(err), 500)
return
}
w.WriteHeader(201)
w.Write([]byte(fmt.Sprint(id)))
}
func Get(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "must-revalidate")
w.Header().Set("Content-Type", "application/json")
var col, id string
if !Require(w, r, "col", &col) {
return
}
if !Require(w, r, "id", &id) {
return
}
V3Sync.RLock()
defer V3Sync.RUnlock()
dbcol := V3DB.Use(col)
if dbcol == nil {
http.Error(w, fmt.Sprintf("Collection '%s' does not exist.", col), 400)
return
}
docID, err := strconv.ParseUint(id, 10, 64)
if err != nil {
http.Error(w, fmt.Sprintf("Invalid document ID '%v'.", id), 400)
return
}
var doc interface{}
_, err = dbcol.Read(docID, &doc)
if doc == nil {
http.Error(w, fmt.Sprintf("No such document ID %d.", docID), 404)
return
}
resp, err := json.Marshal(doc)
if err != nil {
http.Error(w, fmt.Sprint(err), 500)
return
}
w.Write(resp)
}
func Update(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "must-revalidate")
w.Header().Set("Content-Type", "text/plain")
var col, id, doc string
if !Require(w, r, "col", &col) {
return
}
if !Require(w, r, "id", &id) {
return
}
if !Require(w, r, "doc", &doc) {
return
}
V3Sync.RLock()
defer V3Sync.RUnlock()
dbcol := V3DB.Use(col)
if dbcol == nil {
http.Error(w, fmt.Sprintf("Collection '%s' does not exist.", col), 400)
return
}
docID, err := strconv.ParseUint(id, 10, 64)
if err != nil {
http.Error(w, fmt.Sprintf("Invalid document ID '%v'.", id), 400)
return
}
var newDoc map[string]interface{}
if err := json.Unmarshal([]byte(doc), &newDoc); err != nil {
http.Error(w, fmt.Sprintf("'%v' is not valid JSON document.", newDoc), 400)
return
}
err = dbcol.Update(docID, newDoc)
if err != nil {
http.Error(w, fmt.Sprint(err), 500)
return
}
}
func Delete(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "must-revalidate")
w.Header().Set("Content-Type", "text/plain")
var col, id string
if !Require(w, r, "col", &col) {
return
}
if !Require(w, r, "id", &id) {
return
}
V3Sync.RLock()
defer V3Sync.RUnlock()
dbcol := V3DB.Use(col)
if dbcol == nil {
http.Error(w, fmt.Sprintf("Collection '%s' does not exist.", col), 400)
return
}
docID, err := strconv.ParseUint(id, 10, 64)
if err != nil {
http.Error(w, fmt.Sprintf("Invalid document ID '%v'.", id), 400)
return
}
dbcol.Delete(docID)
}