-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathevent.go
178 lines (156 loc) · 4.51 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
package amiando
import "fmt"
///////////////////////////////////////////////////////////////////////////////
// Event
type BasicEventData struct {
HostID ID `json:"hostId"`
Title string `json:"title"`
Country string `json:"country"`
Language string `json:"language"`
StartDate string `json:"selectedDate"`
EndDate string `json:"selectedEndDate"`
Timezone string `json:"timezone"`
Visibility string `json:"visibility"`
Identifier string `json:"identifier"`
Description string `json:"description"`
ShortDescription string `json:"shortDescription"`
EventType string `json:"eventType"`
OrganisatorDisplayName string `json:"organisatorDisplayName"`
PartnerEventUrl string `json:"partnerEventUrl"`
Location string `json:"location"`
LocationDescription string `json:"locationDescription"`
Street string `json:"street2"`
ZipCode string `json:"zipCode"`
City string `json:"city"`
State string `json:"state"`
CreationTime string `json:"creationTime"`
LastModified string `json:"lastModified"`
Longitude float64 `json:"longitude"`
Latitude float64 `json:"latitude"`
}
type Event struct {
ResultBase
Data BasicEventData `json:"event"`
Api *Api `json:"-"`
Identifier string `json:"-"`
InternalID ID `json:"-"`
}
func NewEvent(api *Api, identifier string) (event *Event, err error) {
event = &Event{
Api: api,
Identifier: identifier,
}
// Search for event with identifier
type Result struct {
ResultBase
Ids []ID `json:"ids"`
}
var result Result
err = event.Api.Call("event/find?identifier=%s", identifier, &result)
if err != nil {
return nil, err
}
if len(result.Ids) == 0 {
return nil, fmt.Errorf("No event found for identifier '%s'", identifier)
}
// Find event with exact match of identifier
// because API find returns all events whose identifiers include the searched one
for _, id := range result.Ids {
type Result struct {
ResultBase
Event BasicEventData `json:"event"`
}
var result Result
err = event.Api.Call("event/%v", id, &result)
if err != nil {
return nil, err
}
if result.Event.Identifier == identifier {
event.InternalID = id
break
}
}
if event.InternalID == 0 {
return nil, fmt.Errorf("No exact match found for identifier '%s'", identifier)
}
err = event.Read(event)
if err != nil {
return nil, err
}
return event, nil
}
func (self *Event) Read(out ErrorReporter) (err error) {
return self.Api.Call("event/%v", self.InternalID, out)
}
func (self *Event) PaymentIDs() (ids []ID, err error) {
type Result struct {
ResultBase
Payments []ID `json:"payments"`
}
var result Result
err = self.Api.Call("event/%v/payments", self.InternalID, &result)
if err != nil {
return nil, err
}
return result.Payments, nil
}
func (self *Event) TicketIDs() (ids []ID, err error) {
type Result struct {
ResultBase
Ids []ID `json:"ids"`
}
var result Result
err = self.Api.Call("ticket/find?eventId=%v", self.InternalID, &result)
if err != nil {
return nil, err
}
return result.Ids, nil
}
func (self *Event) EnumParticipants() (<-chan *Participant, <-chan error) {
p := make(chan *Participant, 32)
e := make(chan error, 1)
go func() {
defer close(p)
defer close(e)
paymentIDs, err := self.PaymentIDs()
if err != nil {
e <- err
return
}
for _, paymentID := range paymentIDs {
ticketIDs, err := self.Api.TicketIDsOfPayment(paymentID)
if err != nil {
e <- err
return
}
for i, ticketID := range ticketIDs {
participant := &Participant{
Event: self,
PaymentID: paymentID,
TicketID: ticketID,
}
err = self.Api.Payment(paymentID, participant)
if err != nil {
e <- err
return
}
// Save payment UserData because it will be overwritten by the ticket UserData
userData := participant.UserData
// Delete payment user data to avoid conflicts
// with ticket user-data
participant.UserData = nil
err = self.Api.Ticket(ticketID, participant)
if err != nil {
e <- err
return
}
// If there is no ticket UserData use payment UserData for the first ticket
if i == 0 && len(participant.UserData) == 0 {
participant.UserData = userData
}
p <- participant
}
}
}()
return p, e
}