-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmappoint.go
121 lines (100 loc) · 3.67 KB
/
mappoint.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
package kanka
import (
"bytes"
"encoding/json"
"fmt"
"time"
"github.com/Henry-Sarabia/blank"
)
// MapPoint contains information about a specific map point.
// For more information, visit: https://kanka.io/en-US/docs/1.0/locations#location-map-points
type MapPoint struct {
SimpleMapPoint
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// SimpleMapPoint contains only the simple information about a map point.
// SimpleMapPoint is primarily used to create new map points for posting to Kanka.
type SimpleMapPoint struct {
LocationID int `json:"location_id"`
TargetEntityID int `json:"target_entity_id,omitempty"`
Name string `json:"name,omitempty"`
AxisX int `json:"axis_x"`
AxisY int `json:"axis_y"`
Color string `json:"colour"`
Icon string `json:"icon"`
Shape string `json:"shape"`
Size string `json:"size"`
}
// MarshalJSON marshals the SimpleMapPoint into its JSON-encoded form if it
// has the required populated fields.
func (sm SimpleMapPoint) MarshalJSON() ([]byte, error) {
if blank.Is(sm.Color) {
return nil, fmt.Errorf("cannot marshal SimpleMapPoint into JSON with a missing Color")
}
if blank.Is(sm.Icon) {
return nil, fmt.Errorf("cannot marshal SimpleMapPoint into JSON with a missing Icon")
}
if blank.Is(sm.Shape) {
return nil, fmt.Errorf("cannot marshal SimpleMapPoint into JSON with a missing Shape")
}
if blank.Is(sm.Size) {
return nil, fmt.Errorf("cannot marshal SimpleMapPoint into JSON with a missing Size")
}
type alias SimpleMapPoint
return json.Marshal(alias(sm))
}
// MapPointService handles communication with the MapPoint endpoint.
type MapPointService service
// Index returns the list of all MapPoints for the location associated with
// locID in the Campaign associated with campID.
// If a non-nil time is provided, Index will only return MapPoints that have
// been changed since that time.
func (ms *MapPointService) Index(campID int, locID int, sync *time.Time) ([]*MapPoint, 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(EndpointLocation)
if end, err = end.id(locID); err != nil {
return nil, fmt.Errorf("invalid Location ID: %w", err)
}
end = end.concat(ms.end)
if sync != nil {
end = end.sync(*sync)
}
var wrap struct {
Data []*MapPoint `json:"data"`
}
if err = ms.client.get(end, &wrap); err != nil {
return nil, fmt.Errorf("cannot get MapPoint Index from Campaign (ID: %d): %w", campID, err)
}
return wrap.Data, nil
}
// Create creates a new MapPoint for the location associated with locID in the
// Campaign associated with campID using the provided SimpleMapPoint data.
// Create returns the newly created MapPoint.
func (ms *MapPointService) Create(campID int, locID int, mp SimpleMapPoint) (*MapPoint, 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(EndpointLocation)
if end, err = end.id(locID); err != nil {
return nil, fmt.Errorf("invalid Location ID: %w", err)
}
end = end.concat(ms.end)
b, err := json.Marshal(mp)
if err != nil {
return nil, fmt.Errorf("cannot marshal SimpleMapPoint (Name: %s, TargetEntityID: %d): %w", mp.Name, mp.TargetEntityID, err)
}
var wrap struct {
Data *MapPoint `json:"data"`
}
if err = ms.client.post(end, bytes.NewReader(b), &wrap); err != nil {
return nil, fmt.Errorf("cannot create MapPoint (Name: %s, TargetEntityID: %d) for Campaign (ID: %d): %w", mp.Name, mp.TargetEntityID, campID, err)
}
return wrap.Data, nil
}