-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocument.go
103 lines (76 loc) · 2.71 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
package gocouchlib
import (
"net/http"
)
type Document struct {
Id string `json:"_id,omitempty"`
Rev string `json:"_rev,omitempty"`
Deleted bool `json:"_deleted,omitempty"`
Json JsonObj
Db *Database `json:"-"`
}
func (doc *Document) dbEndpoint() string {
return doc.Db.Server.FullUrl() + "/" + doc.Db.DbName
}
func (doc *Document) docEndpoint(params map[string]string) string {
queryString := "?"
if params != nil {
for key, val := range params {
queryString += key + "=" + val + "&"
}
}
return doc.dbEndpoint() + "/" + doc.Id + queryString
}
func (doc *Document) Exists() (bool, *CouchResponse) {
headers := make(http.Header)
if doc.Rev != "" {
headers.Add("If-None-Match", "\""+doc.Rev+"\"")
}
couchResp, _ := httpClient.Head(doc.docEndpoint(nil), headers)
// Set doc.Rev using the ETag on the response if it is currently empty
if (couchResp.StatusCode == http.StatusOK || couchResp.StatusCode == http.StatusNotModified) && doc.Rev == "" && couchResp.Headers.Get("ETag") != "" {
doc.Rev = TrimEtag(couchResp.Headers.Get("ETag"))
}
return (couchResp.StatusCode == http.StatusOK || couchResp.StatusCode == http.StatusNotModified), couchResp
}
func (doc *Document) Get() (JsonObj, *CouchResponse) {
headers := make(http.Header)
if doc.Rev != "" {
headers.Add("If-None-Match", "\""+doc.Rev+"\"")
}
couchResp, _ := httpClient.Get(doc.docEndpoint(nil), headers)
var json JsonObj = nil
if couchResp.StatusCode == http.StatusOK || couchResp.StatusCode == http.StatusNotModified {
json = couchResp.Json
doc.Json = couchResp.Json
doc.Rev = TrimEtag(couchResp.Headers.Get("ETag"))
}
return json, couchResp
}
func (doc *Document) Delete() (bool, *CouchResponse) {
var params map[string]string
params = make(map[string]string)
params["rev"] = doc.Rev
couchResp, _ := httpClient.Delete(doc.docEndpoint(params))
if couchResp.StatusCode == http.StatusOK || couchResp.StatusCode == http.StatusAccepted {
doc.Rev = TrimEtag(couchResp.Headers.Get("ETag"))
doc.Deleted = true
}
return couchResp.StatusCode == http.StatusOK || couchResp.StatusCode == http.StatusAccepted, couchResp
}
func (doc *Document) Save() (bool, *CouchResponse) {
var couchResp *CouchResponse
if doc.Id == "" {
couchResp, _ = httpClient.Post(doc.dbEndpoint(), doc.Json)
} else {
headers := make(http.Header)
if doc.Rev != "" {
headers.Add("If-Match", "\""+doc.Rev+"\"")
}
couchResp, _ = httpClient.Put(doc.docEndpoint(nil), doc.Json, headers)
}
if couchResp.StatusCode == http.StatusCreated || couchResp.StatusCode == http.StatusAccepted {
doc.Rev = TrimEtag(couchResp.Headers.Get("ETag"))
}
return couchResp.StatusCode == http.StatusCreated || couchResp.StatusCode == http.StatusAccepted, couchResp
}