-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrelation.go
229 lines (183 loc) · 6.59 KB
/
relation.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package kanka
import (
"bytes"
"encoding/json"
"fmt"
"time"
"github.com/Henry-Sarabia/blank"
)
// Relation contains information about a specific relation.
// For more information, visit: https://kanka.io/en-US/docs/1.0/relations
// Relation represents a relationship between two entities.
type Relation struct {
SimpleRelation
ID int `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// SimpleRelation contains only the simple information about a relation.
// SimpleRelation is primarily used to create new relations for posting to Kanka.
type SimpleRelation struct {
Relation string `json:"relation"`
OwnerID int `json:"owner_id"`
TargetID int `json:"target_id"`
Attitude int `json:"attitude"`
TwoWay bool `json:"two_way,omitempty"`
IsPrivate bool `json:"is_private,omitempty"`
}
// For more information, visit: https://kanka.io/en-US/docs/1.0/relations#create-relation
const (
relationLengthMax int = 255
attitudeMin int = -100
attitudeMax int = 100
)
// MarshalJSON marshals the SimpleRelation into its JSON-encoded form if it
// has the required populated fields.
func (sr SimpleRelation) MarshalJSON() ([]byte, error) {
if blank.Is(sr.Relation) {
return nil, fmt.Errorf("cannot marshal SimpleRelation into JSON with a missing Relation")
}
if len(sr.Relation) > relationLengthMax {
return nil, fmt.Errorf("length of Relation string must not exceed %d characters", relationLengthMax)
}
if sr.Attitude < attitudeMin || sr.Attitude > attitudeMax {
return nil, fmt.Errorf("value of Attitude must be between %d and %d", attitudeMin, attitudeMax)
}
type alias SimpleRelation
return json.Marshal(alias(sr))
}
// Relations wraps a list of relationships.
// Relations exists to satisfy the API's JSON structure.
type Relations struct {
Data []Relation `json:"data"`
Sync time.Time `json:"sync"`
}
// RelationService handles communication with the Relation endpoint.
type RelationService service
// Index returns the list of all Relations for the entity associated with
// entID in the Campaign associated with campID.
// If a non-nil time is provided, Index will only return Relations that have
// been changed since that time.
func (rs *RelationService) Index(campID int, entID int, sync *time.Time) ([]*Relation, error) {
var err error
end := EndpointCampaign
if end, err = end.id(campID); err != nil {
return nil, fmt.Errorf("invalid Campaign ID: %w", err)
}
end = end.concat(endpointEntity)
if end, err = end.id(entID); err != nil {
return nil, fmt.Errorf("invalid Entity ID: %w", err)
}
end = end.concat(rs.end)
if sync != nil {
end = end.sync(*sync)
}
var wrap struct {
Data []*Relation `json:"data"`
}
if err = rs.client.get(end, &wrap); err != nil {
return nil, fmt.Errorf("cannot get Relation Index from Campaign (ID: %d): %w", campID, err)
}
return wrap.Data, nil
}
// Get returns the Relation associated with relID for the entity associated
// with entID from the Campaign associated with campID.
func (rs *RelationService) Get(campID int, entID int, relID int) (*Relation, error) {
var err error
end := EndpointCampaign
if end, err = end.id(campID); err != nil {
return nil, fmt.Errorf("invalid Campaign ID: %w", err)
}
end = end.concat(endpointEntity)
if end, err = end.id(entID); err != nil {
return nil, fmt.Errorf("invalid Entity ID: %w", err)
}
end = end.concat(rs.end)
if end, err = end.id(relID); err != nil {
return nil, fmt.Errorf("invalid Relation ID: %w", err)
}
var wrap struct {
Data *Relation `json:"data"`
}
if err = rs.client.get(end, &wrap); err != nil {
return nil, fmt.Errorf("cannot get Relation (ID: %d) from Campaign (ID: %d): %w", relID, campID, err)
}
return wrap.Data, nil
}
// Create creates a new Relation for the entity associated with entID in the
// Campaign associated with campID using the provided SimpleRelation data.
// Create returns the newly created Relation.
func (rs *RelationService) Create(campID int, entID int, rel SimpleRelation) (*Relation, error) {
var err error
end := EndpointCampaign
if end, err = end.id(campID); err != nil {
return nil, fmt.Errorf("invalid Campaign ID: %w", err)
}
end = end.concat(endpointEntity)
if end, err = end.id(entID); err != nil {
return nil, fmt.Errorf("invalid Entity ID: %w", err)
}
end = end.concat(rs.end)
b, err := json.Marshal(rel)
if err != nil {
return nil, fmt.Errorf("cannot marshal SimpleRelation (Relation: %s): %w", rel.Relation, err)
}
var wrap struct {
Data *Relation `json:"data"`
}
if err = rs.client.post(end, bytes.NewReader(b), &wrap); err != nil {
return nil, fmt.Errorf("cannot create Relation (Relation: %s) for Campaign (ID: %d): %w", rel.Relation, campID, err)
}
return wrap.Data, nil
}
// Update updates an existing Relation associated with relID for the entity
// associated with entID from the Campaign associated with campID using the
// provided SimpleRelation data.
// Update returns the newly updated Relation.
func (rs *RelationService) Update(campID int, entID int, relID int, rel SimpleRelation) (*Relation, error) {
var err error
end := EndpointCampaign
if end, err = end.id(campID); err != nil {
return nil, fmt.Errorf("invalid Campaign ID: %w", err)
}
end = end.concat(endpointEntity)
if end, err = end.id(entID); err != nil {
return nil, fmt.Errorf("invalid Entity ID: %w", err)
}
end = end.concat(rs.end)
if end, err = end.id(relID); err != nil {
return nil, fmt.Errorf("invalid Relation ID: %w", err)
}
b, err := json.Marshal(rel)
if err != nil {
return nil, fmt.Errorf("cannot marshal SimpleRelation (Relation: %s): %w", rel.Relation, err)
}
var wrap struct {
Data *Relation `json:"data"`
}
if err = rs.client.put(end, bytes.NewReader(b), &wrap); err != nil {
return nil, fmt.Errorf("cannot update Relation (Relation: %s) for Campaign (ID: %d): '%w'", rel.Relation, campID, err)
}
return wrap.Data, nil
}
// Delete deletes an existing Relation associated with relID from the
// Campaign associated with campID.
func (rs *RelationService) Delete(campID int, entID int, relID int) error {
var err error
end := EndpointCampaign
if end, err = end.id(campID); err != nil {
return fmt.Errorf("invalid Campaign ID: %w", err)
}
end = end.concat(endpointEntity)
if end, err = end.id(entID); err != nil {
return fmt.Errorf("invalid Entity ID: %w", err)
}
end = end.concat(rs.end)
if end, err = end.id(relID); err != nil {
return fmt.Errorf("invalid Relation ID: %w", err)
}
if err = rs.client.delete(end); err != nil {
return fmt.Errorf("cannot delete Relation (ID: %d) for Campaign (ID: %d): %w", relID, campID, err)
}
return nil
}