-
Notifications
You must be signed in to change notification settings - Fork 55
/
entity.go
134 lines (124 loc) · 3.01 KB
/
entity.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
// Copyright (c) 2012-2013 Jason McVetta. This is Free Software, released under
// the terms of the GPL v3. See http://www.gnu.org/copyleft/gpl.html for details.
// Resist intellectual serfdom - the ownership of ideas is akin to slavery.
package neoism
import (
"strings"
)
// An entity is an object - either a Node or a Relationship - in a Neo4j graph
// database. An entity may optinally be assigned an arbitrary set of key:value
// properties.
type entity struct {
Db *Database
HrefSelf string `json:"self"`
HrefProperty string `json:"property"`
HrefProperties string `json:"properties"`
}
// SetProperty sets the single property key to value.
func (e *entity) SetProperty(key string, value string) error {
parts := []string{e.HrefProperties, key}
url := strings.Join(parts, "/")
ne := NeoError{}
resp, err := e.Db.Session.Put(url, &value, nil, &ne)
if err != nil {
return err
}
if resp.Status() != 204 {
return ne
}
return nil // Success!
}
// GetProperty fetches the value of property key.
func (e *entity) Property(key string) (string, error) {
var val string
parts := []string{e.HrefProperties, key}
url := strings.Join(parts, "/")
ne := NeoError{}
resp, err := e.Db.Session.Get(url, nil, &val, &ne)
if err != nil {
return val, err
}
switch resp.Status() {
case 200:
case 404:
return val, NotFound
default:
return val, ne
}
return val, nil // Success!
}
// DeleteProperty deletes property key
func (e *entity) DeleteProperty(key string) error {
parts := []string{e.HrefProperties, key}
url := strings.Join(parts, "/")
ne := NeoError{}
resp, err := e.Db.Session.Delete(url, nil, nil, &ne)
if err != nil {
return err
}
switch resp.Status() {
case 204:
return nil // Success!
case 404:
return NotFound
}
return ne
}
// Delete removes the object from the DB.
func (e *entity) Delete() error {
ne := NeoError{}
resp, err := e.Db.Session.Delete(e.HrefSelf, nil, nil, &ne)
if err != nil {
return err
}
switch resp.Status() {
case 204:
case 404:
return NotFound
case 409:
return CannotDelete
default:
return ne
}
return nil
}
// Properties fetches all properties
func (e *entity) Properties() (Props, error) {
props := Props{}
resp, err := e.Db.Session.Get(e.HrefProperties, nil, &props, nil)
if err != nil {
return props, err
}
// Status code 204 indicates no properties on this node
if resp.Status() == 204 {
props = Props{}
}
return props, nil
}
// SetProperties updates all properties, overwriting any existing properties.
func (e *entity) SetProperties(p Props) error {
ne := NeoError{}
resp, err := e.Db.Session.Put(e.HrefProperties, &p, nil, &ne)
if err != nil {
return err
}
if resp.Status() == 204 {
return nil // Success!
}
return ne
}
// DeleteProperties deletes all properties.
func (e *entity) DeleteProperties() error {
ne := NeoError{}
resp, err := e.Db.Session.Delete(e.HrefProperties, nil, nil, &ne)
if err != nil {
return err
}
switch resp.Status() {
case 204:
return nil // Success!
case 404:
return NotFound
}
return ne
}