-
Notifications
You must be signed in to change notification settings - Fork 1
/
location.go
188 lines (170 loc) · 6.67 KB
/
location.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
package brewerydb
import "fmt"
import "net/http"
// LocationService provides access to the BreweryDB Location API.
// Use Client.Location.
//
// See: http://www.brewerydb.com/developers/docs-endpoint/location_index
type LocationService struct {
c *Client
}
// Country represents a country found on Earth.
type Country struct {
ISOCode string
Name string
DisplayName string
ISOThree string
NumberCode int
URLTitle string
CreateDate string
// UpdateDate string
}
// Location represents a the location of a Brewery or similar institution.
type Location struct {
ID string `url:"-"`
Name string `url:"name,omitempty"`
StreetAddress string `url:"streetAddress,omitempty"`
ExtendedAddress string `url:"extendedAddress,omitempty"`
Locality string `url:"locality,omitempty"`
Region string `url:"region,omitempty"`
PostalCode string `url:"postalCode,omitempty"`
Phone string `url:"phone,omitempty"`
Website string `url:"website,omitempty"`
HoursOfOperation string `url:"-"`
HoursOfOperationExplicit []string `url:"hoursOfOperationExplicit,omitempty"`
HoursOfOperationNotes string `url:"hoursOfOperationNotes,omitempty"`
TourInfo string `url:"tourInfo,omitempty"`
TimezoneID string `url:"timezoneId,omitempty"`
Latitude float64 `url:"latitude,omitempty"`
Longitude float64 `url:"longitude,omitempty"`
IsPrimary YesNo `url:"isPrimary,omitempty"`
InPlanning YesNo `url:"inPlanning,omitempty"`
IsClosed YesNo `url:"isClosed,omitempty"`
OpenToPublic YesNo `url:"openToPublic,omitempty"`
LocationType LocationType `url:"locationType,omitempty"`
LocationTypeDisplay string `url:"-"`
CountryISOCode string `url:"countryIsoCode"` // Required for UpdateLocation
Country Country `url:"-"`
CreateDate string `url:"-"`
UpdateDate string `url:"-"`
YearOpened string `url:"-"`
YearClosed string `url:"-"`
BreweryID string `url:"-"`
Brewery Brewery `url:"-"`
}
// LocationType represents the specific type of the Location.
type LocationType string
// Types of Locations.
const (
LocationMicro LocationType = "micro"
LocationMacro = "macro"
LocationNano = "nano"
LocationBrewpub = "brewpub"
LocationProduction = "production"
LocationOffice = "office"
LocationTasting = "tasting"
LocationRestaurant = "restaurant"
LocationCidery = "cidery"
LocationMeadery = "meadery"
)
// LocationOrder specifies the ordering of a LocationList.
type LocationOrder string
// LocationList ordering options.
const (
LocationOrderName LocationOrder = "name"
LocationOrderBreweryname = "breweryName"
Locality = "locality"
LocationOrderRegion = "region"
LocationOrderPostalCode = "postalCode"
LocationOrderIsPrimary = "isPrimary"
LocationOrderInPlanning = "inPlanning"
LocationOrderIsClosed = "isClosed"
LocationOrderLocationType = "locationType"
LocationOrderCountryIsoCode = "countryIsoCode"
LocationOrderStatus = "status"
LocationOrderCreateDate = "createDate"
LocationOrderUpdateDate = "updateDate"
)
// LocationListRequest contains options for specifying Locations.
type LocationListRequest struct {
Page int `url:"p,omitempty"`
IDs string `url:"ids,omitempty"`
Locality string `url:"locality,omitempty"`
Region string `url:"region,omitempty"`
PostalCode string `url:"postalCode,omitempty"`
IsPrimary YesNo `url:"isPrimary,omitempty"`
InPlanning YesNo `url:"inPlanning,omitempty"`
IsClosed YesNo `url:"isClosed,omitempty"`
LocationType LocationType `url:"locationType,omitempty"`
CountryISOCode string `url:"countryIsoCode,omitempty"`
Since int `url:"since,omitempty"`
Status string `url:"status,omitempty"`
Order LocationOrder `url:"order,omitempty"`
Sort ListSort `url:"sort,omitempty"`
}
// LocationList represents a "page" containing a slice of Locations.
type LocationList struct {
NumberOfPages int
CurrentPage int
TotalResults int
Locations []Location `json:"data"`
}
// List retrieves a list of Locations matching the given request.
// For non-premium users, one of Locality, PostalCode, Region must be set.
//
// See: http://www.brewerydb.com/developers/docs-endpoint/location_index#1
func (ls *LocationService) List(q *LocationListRequest) (ll LocationList, err error) {
// GET: /locations
var req *http.Request
req, err = ls.c.NewRequest("GET", "/locations", q)
if err != nil {
return
}
err = ls.c.Do(req, &ll)
return
}
// Get retrieves the Location with the given ID.
//
// See: http://www.brewerydb.com/developers/docs-endpoint/location_index#2
func (ls *LocationService) Get(locID string) (l Location, err error) {
// GET: /location/:locationID
var req *http.Request
req, err = ls.c.NewRequest("GET", "/location/"+locID, nil)
if err != nil {
return
}
resp := struct {
Status string
Data Location
Message string
}{}
err = ls.c.Do(req, &resp)
return resp.Data, err
}
// Update updates the Location having the given ID to match the given Location.
// The CountryISOCode of the given Location *must* be set.
//
// See: http://www.brewerydb.com/developers/docs-endpoint/location_index#3
func (ls *LocationService) Update(locID string, l *Location) error {
// PUT: /location/:locationID
if l == nil {
return fmt.Errorf("nil Location")
}
req, err := ls.c.NewRequest("PUT", "/location/"+locID, l)
if err != nil {
return err
}
// TODO: return any response?
return ls.c.Do(req, nil)
}
// Delete removes the Location with the given ID from the BreweryDB.
//
// See: http://www.brewerydb.com/developers/docs-endpoint/location_index#4
func (ls *LocationService) Delete(locID string) error {
// DELETE: /location/:locationID
req, err := ls.c.NewRequest("DELETE", "/location/"+locID, nil)
if err != nil {
return err
}
return ls.c.Do(req, nil)
}