@@ -24,6 +24,7 @@ package driver
24
24
25
25
import (
26
26
"context"
27
+ "fmt"
27
28
"path"
28
29
)
29
30
@@ -48,6 +49,11 @@ type edgeCollection struct {
48
49
conn Connection
49
50
}
50
51
52
+ type edgeDocument struct {
53
+ From DocumentID `json:"_from,omitempty"`
54
+ To DocumentID `json:"_to,omitempty"`
55
+ }
56
+
51
57
// relPath creates the relative path to this edge collection (`_db/<db-name>/_api/gharial/<graph-name>/edge/<collection-name>`)
52
58
func (c * edgeCollection ) relPath () string {
53
59
escapedName := pathEscape (c .name )
@@ -59,9 +65,138 @@ func (c *edgeCollection) Name() string {
59
65
return c .name
60
66
}
61
67
68
+ // ReadEdge reads a single edge with given key from this edge collection.
69
+ // The document data is stored into result, the document meta data is returned.
70
+ // If no document exists with given key, a NotFoundError is returned.
71
+ func (c * edgeCollection ) ReadEdge (ctx context.Context , key string , result interface {}) (EdgeMeta , error ) {
72
+ if err := validateKey (key ); err != nil {
73
+ return EdgeMeta {}, WithStack (err )
74
+ }
75
+ escapedKey := pathEscape (key )
76
+ req , err := c .conn .NewRequest ("GET" , path .Join (c .relPath (), escapedKey ))
77
+ if err != nil {
78
+ return EdgeMeta {}, WithStack (err )
79
+ }
80
+ applyContextSettings (ctx , req )
81
+ resp , err := c .conn .Do (ctx , req )
82
+ if err != nil {
83
+ return EdgeMeta {}, WithStack (err )
84
+ }
85
+ if err := resp .CheckStatus (200 ); err != nil {
86
+ return EdgeMeta {}, WithStack (err )
87
+ }
88
+ // Parse metadata
89
+ var meta EdgeMeta
90
+ if err := resp .ParseBody ("edge" , & meta ); err != nil {
91
+ return EdgeMeta {}, WithStack (err )
92
+ }
93
+ return meta , nil
94
+ }
95
+
62
96
// CreateEdge creates a new edge in this edge collection.
63
97
func (c * edgeCollection ) CreateEdge (ctx context.Context , from , to DocumentID , document interface {}) (DocumentMeta , error ) {
64
- return DocumentMeta {}, nil
98
+ if err := from .Validate (); err != nil {
99
+ return DocumentMeta {}, WithStack (InvalidArgumentError {Message : fmt .Sprintf ("from invalid: %v" , err )})
100
+ }
101
+ if err := to .Validate (); err != nil {
102
+ return DocumentMeta {}, WithStack (InvalidArgumentError {Message : fmt .Sprintf ("to invalid: %v" , err )})
103
+ }
104
+ req , err := c .conn .NewRequest ("POST" , c .relPath ())
105
+ if err != nil {
106
+ return DocumentMeta {}, WithStack (err )
107
+ }
108
+ if _ , err := req .SetBody (edgeDocument {To : to , From : from }, document ); err != nil {
109
+ return DocumentMeta {}, WithStack (err )
110
+ }
111
+ applyContextSettings (ctx , req )
112
+ resp , err := c .conn .Do (ctx , req )
113
+ if err != nil {
114
+ return DocumentMeta {}, WithStack (err )
115
+ }
116
+ if err := resp .CheckStatus (201 , 202 ); err != nil {
117
+ return DocumentMeta {}, WithStack (err )
118
+ }
119
+ // Parse metadata
120
+ var meta DocumentMeta
121
+ if err := resp .ParseBody ("edge" , & meta ); err != nil {
122
+ return DocumentMeta {}, WithStack (err )
123
+ }
124
+ return meta , nil
125
+ }
126
+
127
+ // UpdateEdge updates a single edge with given key in the collection.
128
+ // To & from are allowed to be empty. If they are empty, they are not updated.
129
+ // The document meta data is returned.
130
+ // If no document exists with given key, a NotFoundError is returned.
131
+ func (c * edgeCollection ) UpdateEdge (ctx context.Context , key string , from , to DocumentID , update interface {}) (DocumentMeta , error ) {
132
+ if err := from .ValidateOrEmpty (); err != nil {
133
+ return DocumentMeta {}, WithStack (InvalidArgumentError {Message : fmt .Sprintf ("from invalid: %v" , err )})
134
+ }
135
+ if err := to .ValidateOrEmpty (); err != nil {
136
+ return DocumentMeta {}, WithStack (InvalidArgumentError {Message : fmt .Sprintf ("to invalid: %v" , err )})
137
+ }
138
+ var edgeDoc * edgeDocument
139
+ if ! from .IsEmpty () || ! to .IsEmpty () {
140
+ edgeDoc = & edgeDocument {
141
+ From : from ,
142
+ To : to ,
143
+ }
144
+ }
145
+ req , err := c .conn .NewRequest ("PATCH" , c .relPath ())
146
+ if err != nil {
147
+ return DocumentMeta {}, WithStack (err )
148
+ }
149
+ if _ , err := req .SetBody (edgeDoc , update ); err != nil {
150
+ return DocumentMeta {}, WithStack (err )
151
+ }
152
+ applyContextSettings (ctx , req )
153
+ resp , err := c .conn .Do (ctx , req )
154
+ if err != nil {
155
+ return DocumentMeta {}, WithStack (err )
156
+ }
157
+ if err := resp .CheckStatus (200 , 202 ); err != nil {
158
+ return DocumentMeta {}, WithStack (err )
159
+ }
160
+ // Parse metadata
161
+ var meta DocumentMeta
162
+ if err := resp .ParseBody ("edge" , & meta ); err != nil {
163
+ return DocumentMeta {}, WithStack (err )
164
+ }
165
+ return meta , nil
166
+ }
167
+
168
+ func (c * edgeCollection ) ReplaceEdge (ctx context.Context , key string , from , to DocumentID , update interface {}) (DocumentMeta , error ) {
169
+ if err := from .Validate (); err != nil {
170
+ return DocumentMeta {}, WithStack (InvalidArgumentError {Message : fmt .Sprintf ("from invalid: %v" , err )})
171
+ }
172
+ if err := to .Validate (); err != nil {
173
+ return DocumentMeta {}, WithStack (InvalidArgumentError {Message : fmt .Sprintf ("to invalid: %v" , err )})
174
+ }
175
+ edgeDoc := edgeDocument {
176
+ From : from ,
177
+ To : to ,
178
+ }
179
+ req , err := c .conn .NewRequest ("PUT" , c .relPath ())
180
+ if err != nil {
181
+ return DocumentMeta {}, WithStack (err )
182
+ }
183
+ if _ , err := req .SetBody (edgeDoc , update ); err != nil {
184
+ return DocumentMeta {}, WithStack (err )
185
+ }
186
+ applyContextSettings (ctx , req )
187
+ resp , err := c .conn .Do (ctx , req )
188
+ if err != nil {
189
+ return DocumentMeta {}, WithStack (err )
190
+ }
191
+ if err := resp .CheckStatus (201 , 202 ); err != nil {
192
+ return DocumentMeta {}, WithStack (err )
193
+ }
194
+ // Parse metadata
195
+ var meta DocumentMeta
196
+ if err := resp .ParseBody ("edge" , & meta ); err != nil {
197
+ return DocumentMeta {}, WithStack (err )
198
+ }
199
+ return meta , nil
65
200
}
66
201
67
202
// Remove the edge collection from the graph.
0 commit comments