-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtag.go
194 lines (161 loc) · 5.33 KB
/
tag.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
package kanka
import (
"bytes"
"encoding/json"
"fmt"
"time"
"github.com/Henry-Sarabia/blank"
)
// Tag contains information about a specific tag.
// For more information, visit: https://kanka.io/en-US/docs/1.0/tags
type Tag struct {
SimpleTag
ID int `json:"id"`
ImageFull string `json:"image_full"`
ImageThumb string `json:"image_thumb"`
HasCustomImage bool `json:"has_custom_image"`
EntityID int `json:"entity_id"`
Entities []int `json:"entities"`
CreatedAt time.Time `json:"created_at"`
CreatedBy int `json:"created_by"`
UpdatedAt time.Time `json:"updated_at"`
UpdatedBy int `json:"updated_by"`
Attributes Attributes `json:"attributes"`
EntityEvents EntityEvents `json:"entity_events"`
EntityFiles EntityFiles `json:"entity_files"`
EntityNotes EntityNotes `json:"entity_notes"`
Relations Relations `json:"relations"`
Inventory Inventory `json:"inventory"`
}
// SimpleTag contains only the simple information about a tag.
// SimpleTag is primarily used to create new tags for posting to Kanka.
type SimpleTag struct {
Name string `json:"name"`
Entry string `json:"entry,omitempty"`
Type string `json:"type,omitempty"`
TagID int `json:"tag_id,omitempty"`
Color string `json:"colour,omitempty"`
Tags []int `json:"tags,omitempty"`
IsPrivate bool `json:"is_private,omitempty"`
Image string `json:"image,omitempty"`
ImageURL string `json:"image_url,omitempty"`
}
// MarshalJSON marshals the SimpleTag into its JSON-encoded form if it
// has the required populated fields.
func (st SimpleTag) MarshalJSON() ([]byte, error) {
if blank.Is(st.Name) {
return nil, fmt.Errorf("cannot marshal SimpleTag into JSON with a missing Name")
}
type alias SimpleTag
return json.Marshal(alias(st))
}
// TagService handles communication with the Tag endpoint.
type TagService service
// Index returns the list of all Tags in the Campaign associated with campID.
// If a non-nil time is provided, Index will only return Tags that have
// been changed since that time.
func (ts *TagService) Index(campID int, sync *time.Time) ([]*Tag, error) {
end, err := EndpointCampaign.id(campID)
if err != nil {
return nil, fmt.Errorf("invalid Campaign ID: %w", err)
}
end = end.concat(ts.end)
if sync != nil {
end = end.sync(*sync)
}
var wrap struct {
Data []*Tag `json:"data"`
}
err = ts.client.get(end, &wrap)
if err != nil {
return nil, fmt.Errorf("cannot get Tag Index from Campaign (ID: %d): %w", campID, err)
}
return wrap.Data, nil
}
// Get returns the Tag associated with tagID from the Campaign
// associated with campID.
func (ts *TagService) Get(campID int, tagID int) (*Tag, error) {
end, err := EndpointCampaign.id(campID)
if err != nil {
return nil, fmt.Errorf("invalid Campaign ID: %w", err)
}
end = end.concat(ts.end)
end, err = end.id(tagID)
if err != nil {
return nil, fmt.Errorf("invalid Tag ID: %w", err)
}
var wrap struct {
Data *Tag `json:"data"`
}
err = ts.client.get(end, &wrap)
if err != nil {
return nil, fmt.Errorf("cannot get Tag (ID: %d) from Campaign (ID: %d): %w", tagID, campID, err)
}
return wrap.Data, nil
}
// Create creates a new Tag in the Campaign associated with campID using
// the provided SimpleTag data.
// Create returns the newly created Tag.
func (ts *TagService) Create(campID int, tag SimpleTag) (*Tag, error) {
end, err := EndpointCampaign.id(campID)
if err != nil {
return nil, fmt.Errorf("invalid Campaign ID: %w", err)
}
end = end.concat(ts.end)
b, err := json.Marshal(tag)
if err != nil {
return nil, fmt.Errorf("cannot marshal SimpleTag (Name: %s): %w", tag.Name, err)
}
var wrap struct {
Data *Tag `json:"data"`
}
err = ts.client.post(end, bytes.NewReader(b), &wrap)
if err != nil {
return nil, fmt.Errorf("cannot create Tag (Name: %s) for Campaign (ID: %d): %w", tag.Name, campID, err)
}
return wrap.Data, nil
}
// Update updates an existing Tag associated with tagID from the
// Campaign associated with campID using the provided SimpleTag data.
// Update returns the newly updated Tag.
func (ts *TagService) Update(campID int, tagID int, tag SimpleTag) (*Tag, error) {
end, err := EndpointCampaign.id(campID)
if err != nil {
return nil, fmt.Errorf("invalid Campaign ID: %w", err)
}
end = end.concat(ts.end)
end, err = end.id(tagID)
if err != nil {
return nil, fmt.Errorf("invalid Tag ID: %w", err)
}
b, err := json.Marshal(tag)
if err != nil {
return nil, fmt.Errorf("cannot marshal SimpleTag (Name: %s): %w", tag.Name, err)
}
var wrap struct {
Data *Tag `json:"data"`
}
err = ts.client.put(end, bytes.NewReader(b), &wrap)
if err != nil {
return nil, fmt.Errorf("cannot update Tag (Name: %s) for Campaign (ID: %d): '%w'", tag.Name, campID, err)
}
return wrap.Data, nil
}
// Delete deletes an existing Tag associated with tagID from the
// Campaign associated with campID.
func (ts *TagService) Delete(campID int, tagID int) error {
end, err := EndpointCampaign.id(campID)
if err != nil {
return fmt.Errorf("invalid Campaign ID: %w", err)
}
end = end.concat(ts.end)
end, err = end.id(tagID)
if err != nil {
return fmt.Errorf("invalid Tag ID: %w", err)
}
err = ts.client.delete(end)
if err != nil {
return fmt.Errorf("cannot delete Tag (ID: %d) for Campaign (ID: %d): %w", tagID, campID, err)
}
return nil
}