-
Notifications
You must be signed in to change notification settings - Fork 1
/
event.go
618 lines (545 loc) · 18.3 KB
/
event.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
package brewerydb
import (
"fmt"
"net/http"
)
// EventService provides access to the BreweryDB Event API.
// Use Client.Event.
type EventService struct {
c *Client
}
// EventType specifies the type of the Event.
type EventType string
// Types of Events.
const (
EventFestival EventType = "festival"
EventCompetition = "competition"
EventFestivalCompetition = "festival_competition"
EventTasting = "tasting"
EventBeerRelease = "beer_release"
EventSeminar = "seminar"
EventMeetup = "meetup"
EventOther = "other"
)
// Event represents a community event related to Beer/Breweries.
type Event struct {
ID string `url:"-"`
Name string `url:"name"`
Type EventType `url:"type"`
StartDate string `url:"startDate"` // YYYY-MM-DD
EndDate string `url:"endDate"` // YYYY-MM-DD
Description string `url:"description,omitempty"`
Year string `url:"year,omitempty"`
Time string `url:"time,omitempty"`
Price string `url:"price,omitempty"`
VenueName string `url:"venueName,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"`
CountryISOCode string `url:"countryIsoCode"` // Required
Phone string `url:"phone,omitempty"`
Website string `url:"website,omitempty"`
Longitude float64 `url:"longitude,omitempty"`
Latitude float64 `url:"latitude,omitempty"`
Image string `url:"image"` // base64. Only used for adding/updating Events.
Images Images `url:"-"`
Status string `url:"-"`
StatusDisplay string `url:"-"`
Country Country `url:"-"`
CreateDate string `url:"-"`
UpdateDate string `url:"-"`
}
// EventOrder specifies the ordering of an EventList.
type EventOrder string
// EventList ordering options.
const (
EventOrderWebsite EventOrder = "website"
EventOrderYear = "year"
EventOrderStartDate = "startDate"
EventOrderEndDate = "endDate"
EventOrderLocality = "locality"
EventOrderRegion = "region"
EventOrderCountryIsoCode = "countryIsoCode"
EventOrderStatus = "status"
EventOrderCreateDate = "createDate"
EventOrderUpdateDate = "updateDate"
)
// EventList represents a single "page" containing a slice of Events.
type EventList struct {
CurrentPage int
NumberOfPages int
TotalResults int
Events []Event `json:"data"`
}
// EventListRequest contains options for specifying the kinds of Events desired.
// Non-Premium users must set one of the following: year, name, type, locality, region
type EventListRequest struct {
Page int `url:"p"`
IDs string `url:"ids,omitempty"`
Year int `url:"year,omitempty"`
Name string `url:"name,omitempty"`
Type string `url:"type,omitempty"` // Key of the type of event, comma separated for multiple types
Locality string `url:"locality,omitempty"` // e.g. US city
Region string `url:"region,omitempty"` // e.g. US state
CountryISOCode string `url:"countryIsoCode,omitempty"`
Since int `url:"since,omitempty"` // Unix timestamp
Status string `url:"status,omitempty"`
HasImages YesNo `url:"hasImages,omitempty"`
Order EventOrder `url:"order,omitempty"`
Sort ListSort `url:"sort,omitempty"`
}
// List returns an EventList containing a "page" of Events.
// For non-premium members, one of Year, Name, Type, Locality or Region must be set.
func (es *EventService) List(q *EventListRequest) (el EventList, err error) {
// GET: /events
var req *http.Request
req, err = es.c.NewRequest("GET", "/events", q)
if err != nil {
return
}
err = es.c.Do(req, &el)
return
}
// Get retrieves a single event with the given eventID.
func (es *EventService) Get(eventID string) (e Event, err error) {
// GET: /event/:eventID
var req *http.Request
req, err = es.c.NewRequest("GET", "/event/"+eventID, nil)
if err != nil {
return
}
resp := struct {
Status string
Data Event
Message string
}{}
err = es.c.Do(req, &resp)
return resp.Data, err
}
// Add adds an Event to the BreweryDB and returns its new ID.
// The following **must** be set in the Event:
//
// - Name
// - Type
// - StartDate (YYYY-MM-DD)
// - EndDate (YYYY-MM-DD)
func (es *EventService) Add(e *Event) (string, error) {
// POST: /events
if e == nil {
return "", fmt.Errorf("nil Event")
}
req, err := es.c.NewRequest("POST", "/events", e)
if err != nil {
return "", err
}
resp := struct {
Status string
Data struct {
ID string
}
Message string
}{}
err = es.c.Do(req, &resp)
return resp.Data.ID, err
}
// Update updates the Event with the given eventID to match the given Event.
func (es *EventService) Update(eventID string, e *Event) error {
// PUT: /event/:eventID
if e == nil {
return fmt.Errorf("nil Event")
}
req, err := es.c.NewRequest("PUT", "/event/"+eventID, e)
if err != nil {
return err
}
// TODO: return any response?
return es.c.Do(req, nil)
}
// Delete removes the Event with the given eventID.
func (es *EventService) Delete(eventID string) error {
// DELETE: /event/:eventID
req, err := es.c.NewRequest("DELETE", "/event/"+eventID, nil)
if err != nil {
return err
}
return es.c.Do(req, nil)
}
// AwardCategory represents a category of award for an Event.
type AwardCategory struct {
ID int `url:"-"`
Name string `url:"name"` // required for adding/updating AwardCategories
Description string `url:"description"`
Image string `url:"image"` // base64
CreateDate string `url:"-"`
UpdateDate string `url:"-"`
}
// ListAwardCategories returns a slice of all AwardCategories for the given Event.
func (es *EventService) ListAwardCategories(eventID string) (al []AwardCategory, err error) {
// GET: /event/:eventId/awardcategories
var req *http.Request
req, err = es.c.NewRequest("GET", "/event/"+eventID+"/awardcategories", nil)
if err != nil {
return
}
resp := struct {
Status string
Data []AwardCategory
Message string
}{}
err = es.c.Do(req, &resp)
return resp.Data, nil
}
// GetAwardCategory retrieves the specified AwardCategory for the given Event.
func (es *EventService) GetAwardCategory(eventID string, awardCategoryID int) (a AwardCategory, err error) {
// GET: /event/:eventId/awardcategory/:awardcategoryId
var req *http.Request
req, err = es.c.NewRequest("GET", fmt.Sprintf("/event/%s/awardcategory/%d", eventID, awardCategoryID), nil)
if err != nil {
return
}
resp := struct {
Status string
Data AwardCategory
Message string
}{}
err = es.c.Do(req, &resp)
return resp.Data, err
}
// AddAwardCategory adds a new AwardCategory to the given Event.
func (es *EventService) AddAwardCategory(eventID string, a *AwardCategory) error {
// POST: /event/:eventId/awardcategories
if a == nil {
return fmt.Errorf("nil AwardCategory")
}
req, err := es.c.NewRequest("POST", "/event/"+eventID+"/awardcategories", a)
if err != nil {
return err
}
return es.c.Do(req, nil)
}
// UpdateAwardCategory updates an AwardCategory for the given Event.
func (es *EventService) UpdateAwardCategory(eventID string, a *AwardCategory) error {
// PUT: /event/:eventId/awardcategory/:awardcategoryId
if a == nil {
return fmt.Errorf("nil AwardCategory")
}
req, err := es.c.NewRequest("PUT", fmt.Sprintf("/event/%s/awardcategory/%d", eventID, a.ID), a)
if err != nil {
return err
}
return es.c.Do(req, nil)
}
// DeleteAwardCategory removes an AwardCategory from the given Event.
func (es *EventService) DeleteAwardCategory(eventID string, awardCategoryID int) error {
// DELETE: /event/:eventId/awardcategory/:awardcategoryId
req, err := es.c.NewRequest("DELETE", fmt.Sprintf("/event/%s/awardcategory/%d", eventID, awardCategoryID), nil)
if err != nil {
return err
}
return es.c.Do(req, nil)
}
// AwardPlace represents an award location.
type AwardPlace struct {
ID int `url:"-"`
Name string `url:"name"` // required for adding/updating AwardPlaces
Description string `url:"description"`
Image string `url:"image"` // base64
CreateDate string `url:"-"`
UpdateDate string `url:"-"`
}
// ListAwardPlaces returns a slice of all AwardPlaces for the given Event.
func (es *EventService) ListAwardPlaces(eventID string) (al []AwardPlace, err error) {
// GET: /event/:eventId/awardplaces
var req *http.Request
req, err = es.c.NewRequest("GET", "/event/"+eventID+"/awardplaces", nil)
if err != nil {
return
}
resp := struct {
Status string
Data []AwardPlace
Message string
}{}
err = es.c.Do(req, &resp)
return resp.Data, nil
}
// GetAwardPlace retrieves the specified AwardPlace for the given Event.
func (es *EventService) GetAwardPlace(eventID string, awardPlaceID int) (a AwardPlace, err error) {
// GET: /event/:eventId/awardplace/:awardplaceId
var req *http.Request
req, err = es.c.NewRequest("GET", fmt.Sprintf("/event/%s/awardplace/%d", eventID, awardPlaceID), nil)
if err != nil {
return
}
resp := struct {
Status string
Data AwardPlace
Message string
}{}
err = es.c.Do(req, &resp)
return resp.Data, err
}
// AddAwardPlace adds a new AwardPlace to the given Event.
func (es *EventService) AddAwardPlace(eventID string, a *AwardPlace) error {
// POST: /event/:eventId/awardplaces
if a == nil {
return fmt.Errorf("nil AwardPlace")
}
req, err := es.c.NewRequest("POST", "/event/"+eventID+"/awardplaces", a)
if err != nil {
return err
}
return es.c.Do(req, nil)
}
// UpdateAwardPlace updates an AwardPlace for the given Event.
func (es *EventService) UpdateAwardPlace(eventID string, a *AwardPlace) error {
// PUT: /event/:eventId/awardplace/:awardplaceId
if a == nil {
return fmt.Errorf("nil AwardPlace")
}
req, err := es.c.NewRequest("PUT", fmt.Sprintf("/event/%s/awardplace/%d", eventID, a.ID), a)
if err != nil {
return err
}
return es.c.Do(req, nil)
}
// DeleteAwardPlace removes an AwardPlace from the given Event.
func (es *EventService) DeleteAwardPlace(eventID string, awardPlaceID int) error {
// DELETE: /event/:eventId/awardplace/:awardplaceId
req, err := es.c.NewRequest("DELETE", fmt.Sprintf("/event/%s/awardplace/%d", eventID, awardPlaceID), nil)
if err != nil {
return err
}
return es.c.Do(req, nil)
}
// EventBeersRequest contains parameters for specifying desired
// Beers for a given Event.
type EventBeersRequest struct {
Page int `url:"p, omitempty"`
OnlyWinners YesNo `url:"onlyWinners,omitempty"`
AwardCategoryID int `url:"awardcategoryId,omitempty"`
AwardPlaceID int `url:"awardplaceId,omitempty"`
}
// ListBeers returns a page of Beers for the given Event.
func (es *EventService) ListBeers(eventID string, q *EventBeersRequest) (bl BeerList, err error) {
// GET: /event/:eventID/beers
var req *http.Request
req, err = es.c.NewRequest("GET", "/event/"+eventID+"/beers", q)
if err != nil {
return
}
err = es.c.Do(req, &bl)
return
}
// GetBeer retrieves the Beer with the given ID for the given Event.
func (es *EventService) GetBeer(eventID, beerID string) (b Beer, err error) {
// GET: /event/:eventId/beer/:beerId
var req *http.Request
req, err = es.c.NewRequest("GET", "/event/"+eventID+"/beer/"+beerID, nil)
if err != nil {
return
}
eventBeerResp := struct {
Status string
Data Beer
Message string
}{}
err = es.c.Do(req, &eventBeerResp)
return eventBeerResp.Data, err
}
// EventChangeBeerRequest contains parameters for changing or adding
// a new Beer to an Event.
type EventChangeBeerRequest struct {
IsPouring YesNo `url:"isPouring,omitempty"`
AwardCategoryID int `url:"awardcategoryId,omitempty"`
AwardPlaceID int `url:"awardplaceId,omitempty"`
}
type eventAddBeerRequest struct {
BeerID string `url:"beerId"`
EventChangeBeerRequest
}
// AddBeer adds the Beer with the given ID to the given Event.
func (es *EventService) AddBeer(eventID, beerID string, q *EventChangeBeerRequest) error {
// POST: /event/:eventId/beers
var params *eventAddBeerRequest
if q != nil {
params = &eventAddBeerRequest{beerID, *q}
} else {
params = &eventAddBeerRequest{BeerID: beerID}
}
req, err := es.c.NewRequest("POST", "/event/"+eventID+"/beers", params)
if err != nil {
return err
}
return es.c.Do(req, nil)
}
// UpdateBeer updates the Beer with the given ID for the given Event.
func (es *EventService) UpdateBeer(eventID, beerID string, q *EventChangeBeerRequest) error {
// PUT: /event/:eventId/beer/:beerId
if q == nil {
q = &EventChangeBeerRequest{}
}
req, err := es.c.NewRequest("PUT", "/event/"+eventID+"/beer/"+beerID, q)
if err != nil {
return err
}
return es.c.Do(req, nil)
}
// DeleteBeer removes the Beer with the given ID from the given Event.
func (es *EventService) DeleteBeer(eventID, beerID string) error {
// DELETE: /event/:eventId/beer/:beerId
req, err := es.c.NewRequest("DELETE", "/event/"+eventID+"/beer/"+beerID, nil)
if err != nil {
return err
}
return es.c.Do(req, nil)
}
// EventBreweriesRequest contains parameters for specifying desired
// Breweries for a given Event.
type EventBreweriesRequest struct {
Page int `url:"p,omitempty"`
OnlyWinners YesNo `url:"onlyWinners,omitempty"`
AwardCategoryID int `url:"awardcategoryId,omitempty"`
AwardPlaceID int `url:"awardplaceId,omitempty"`
}
// ListBreweries returns a page of Breweries for the given Event.
func (es *EventService) ListBreweries(eventID string, q *EventBreweriesRequest) (bl BreweryList, err error) {
// GET: /event/:eventID/breweries
var req *http.Request
req, err = es.c.NewRequest("GET", "/event/"+eventID+"/breweries", q)
if err != nil {
return
}
err = es.c.Do(req, &bl)
return
}
// GetBrewery retrieves the Brewery with the given ID for the given Event.
func (es *EventService) GetBrewery(eventID, breweryID string) (b Brewery, err error) {
// GET: /event/:eventID/brewery/:breweryID
var req *http.Request
req, err = es.c.NewRequest("GET", "/event/"+eventID+"/brewery/"+breweryID, nil)
if err != nil {
return
}
eventBreweryResp := struct {
Status string
Data Brewery
Message string
}{}
err = es.c.Do(req, &eventBreweryResp)
return eventBreweryResp.Data, err
}
// EventChangeBreweryRequest contains parameters for changing or adding
// a new Brewery to an Event.
type EventChangeBreweryRequest struct {
AwardCategoryID int `url:"awardcategoryId,omitempty"`
AwardPlaceID int `url:"awardplaceId,omitempty"`
}
// TODO: test the encoding of embedded structs:
type eventAddBreweryRequest struct {
BreweryID string `url:"breweryId"`
EventChangeBreweryRequest
}
// AddBrewery adds the Brewery with the given ID to the given Event.
func (es *EventService) AddBrewery(eventID, breweryID string, q *EventChangeBreweryRequest) error {
// POST: /event/:eventID/brewery/:breweryID
var params *eventAddBreweryRequest
if q != nil {
params = &eventAddBreweryRequest{breweryID, *q}
} else {
params = &eventAddBreweryRequest{BreweryID: breweryID}
}
req, err := es.c.NewRequest("POST", "/event/"+eventID+"/breweries", params)
if err != nil {
return err
}
return es.c.Do(req, nil)
}
// UpdateBrewery updates the Brewery with the given ID for the given Event.
func (es *EventService) UpdateBrewery(eventID, breweryID string, q *EventChangeBreweryRequest) error {
// PUT: /event/:eventID/brewery/:breweryID
if q == nil {
q = &EventChangeBreweryRequest{}
}
req, err := es.c.NewRequest("PUT", "/event/"+eventID+"/brewery/"+breweryID, q)
if err != nil {
return err
}
return es.c.Do(req, nil)
}
// DeleteBrewery removes the Brewery with the given ID from the given Event.
func (es *EventService) DeleteBrewery(eventID, breweryID string) error {
// DELETE: /event/:eventID/brewery/:breweryID
req, err := es.c.NewRequest("DELETE", "/event/"+eventID+"/brewery/"+breweryID, nil)
if err != nil {
return err
}
return es.c.Do(req, nil)
}
// ListSocialAccounts returns a slice of all social media accounts associated with the given Event.
func (es *EventService) ListSocialAccounts(eventID string) (sl []SocialAccount, err error) {
// GET: /event/:eventId/socialaccounts
var req *http.Request
req, err = es.c.NewRequest("GET", "/event/"+eventID+"/socialaccounts", nil)
if err != nil {
return
}
resp := struct {
Status string
Data []SocialAccount
Message string
}{}
err = es.c.Do(req, &resp)
return resp.Data, err
}
// GetSocialAccount retrieves the SocialAccount with the given ID for the given Event.
func (es *EventService) GetSocialAccount(eventID string, socialAccountID int) (s SocialAccount, err error) {
// GET: /event/:eventId/socialaccount/:socialaccountId
var req *http.Request
req, err = es.c.NewRequest("GET", fmt.Sprintf("/event/%s/socialaccount/%d", eventID, socialAccountID), nil)
if err != nil {
return
}
resp := struct {
Status string
Data SocialAccount
Message string
}{}
err = es.c.Do(req, &resp)
return resp.Data, err
}
// AddSocialAccount adds a new SocialAccount to the given Event.
func (es *EventService) AddSocialAccount(eventID string, s *SocialAccount) error {
// POST: /event/:eventId/socialaccounts
if s == nil {
return fmt.Errorf("nil SocialAccount")
}
req, err := es.c.NewRequest("POST", "/event/"+eventID+"/socialaccounts", s)
if err != nil {
return err
}
return es.c.Do(req, nil)
}
// UpdateSocialAccount updates a SocialAccount for the given Event.
func (es *EventService) UpdateSocialAccount(eventID string, s *SocialAccount) error {
// PUT: /event/:eventId/socialaccount/:socialaccountId
if s == nil {
return fmt.Errorf("nil SocialAccount")
}
req, err := es.c.NewRequest("PUT", fmt.Sprintf("/event/%s/socialaccount/%d", eventID, s.ID), s)
if err != nil {
return err
}
return es.c.Do(req, nil)
}
// DeleteSocialAccount removes a SocialAccount from the given Event.
func (es *EventService) DeleteSocialAccount(eventID string, socialAccountID int) error {
// DELETE: /event/:eventId/socialaccount/:socialaccountId
req, err := es.c.NewRequest("DELETE", fmt.Sprintf("/event/%s/socialaccount/%d", eventID, socialAccountID), nil)
if err != nil {
return err
}
return es.c.Do(req, nil)
}