This repository was archived by the owner on Nov 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparty.go
266 lines (243 loc) · 6.76 KB
/
party.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
package main
import (
"context"
"database/sql"
"errors"
"fmt"
"log"
"darlinggo.co/pan"
"github.com/coreos/go-oidc"
"github.com/pborman/uuid"
)
var (
ErrMagicWordNotFound = errors.New("magic word not found")
)
type Dependencies struct {
db *sql.DB
log *log.Logger
verifier *oidc.IDTokenVerifier
}
type Party struct {
ID string `json:"ID"`
LeadID string `json:"lead,omitempty"`
Name string `json:"name,omitempty"`
SortValue string `json:"sortValue"`
Address string `json:"address,omitempty"`
MagicWord string `json:"codeWord,omitempty"`
}
func (p Party) GetSQLTableName() string {
return "parties"
}
func (p Party) getUpsertAction() string {
columns := pan.Columns(p)
for pos, column := range columns {
columns[pos] = fmt.Sprintf("%s = EXCLUDED.%s", column, column)
}
return columns.String()
}
func (deps Dependencies) CreateParties(ctx context.Context, parties []Party) ([]Party, error) {
tabler := make([]pan.SQLTableNamer, 0, len(parties))
for _, party := range parties {
if party.ID == "" {
party.ID = uuid.New()
}
tabler = append(tabler, party)
}
query := pan.Insert(tabler...).Flush(", ").Expression("ON CONFLICT (" + pan.Column(parties[0], "ID") + ") DO UPDATE SET").Flush(" ")
query.Expression(parties[0].getUpsertAction()).Flush(", ")
queryStr, err := query.PostgreSQLString()
if err != nil {
return nil, err
}
deps.log.Println(queryStr)
_, err = deps.db.Exec(queryStr, query.Args()...)
if err != nil {
return nil, err
}
return parties, err
}
type Person struct {
ID string `json:"ID"`
PartyID string `json:"party,omitempty"`
Name string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
GetsPlusOne bool `json:"getsPlusOne"`
PlusOneID string `json:"plusOne,omitempty"`
IsPlusOne bool `json:"isPlusOne"`
IsPlusOneOfID string `json:"isPlusOneOf,omitempty"`
Replied bool `json:"replied"`
Reply bool `json:"reply"`
DietaryRestrictions string `json:"dietaryRestrictions,omitempty"`
SongRequest string `json:"songRequest,omitempty"`
IsChild bool `json:"isChild"`
WillAccompanyID string `json:"willAccompany,omitempty"`
Hiking bool `json:"hiking"`
Kayaking bool `json:"kayaking"`
Jetski bool `json:"jetski"`
Fishing bool `json:"fishing"`
Hanford bool `json:"hanford"`
Ligo bool `json:"ligo"`
Reach bool `json:"reach"`
Bechtel bool `json:"bechtel"`
Wine bool `json:"wine"`
EscapeRoom bool `json:"escapeRoom"`
}
func (p Person) GetSQLTableName() string {
return "people"
}
func (p Person) getUpsertAction() string {
columns := pan.Columns(p)
for pos, column := range columns {
columns[pos] = fmt.Sprintf("%s = EXCLUDED.%s", column, column)
}
return columns.String()
}
func (deps Dependencies) CreatePeople(ctx context.Context, people []Person) ([]Person, error) {
tabler := make([]pan.SQLTableNamer, 0, len(people))
for _, person := range people {
if person.ID == "" {
person.ID = uuid.New()
}
tabler = append(tabler, person)
}
query := pan.Insert(tabler...).Flush(", ").Expression("ON CONFLICT (" + pan.Column(people[0], "ID") + ") DO UPDATE SET").Flush(" ")
query.Expression(people[0].getUpsertAction()).Flush(", ")
queryStr, err := query.PostgreSQLString()
if err != nil {
return nil, err
}
deps.log.Println(queryStr)
_, err = deps.db.Exec(queryStr, query.Args()...)
if err != nil {
return nil, err
}
return people, nil
}
func (deps Dependencies) ListParties(ctx context.Context) ([]Party, error) {
var p Party
query := pan.New("SELECT " + pan.Columns(p).String() + " FROM " + pan.Table(p)).OrderBy("sort_value").Flush(" ")
queryStr, err := query.PostgreSQLString()
if err != nil {
return nil, err
}
rows, err := deps.db.Query(queryStr, query.Args()...)
if err != nil {
return nil, err
}
var parties []Party
for rows.Next() {
var party Party
err := pan.Unmarshal(rows, &party)
if err != nil {
return nil, err
}
parties = append(parties, party)
}
return parties, nil
}
func (deps Dependencies) GetParties(ctx context.Context, ids []string) ([]Party, error) {
var p Party
ifIDs := make([]interface{}, 0, len(ids))
for _, id := range ids {
ifIDs = append(ifIDs, id)
}
query := pan.New("SELECT " + pan.Columns(p).String() + " FROM " + pan.Table(p)).Where()
query.In(p, "ID", ifIDs...).OrderBy("sort_value").Flush(" ")
queryStr, err := query.PostgreSQLString()
if err != nil {
return nil, err
}
rows, err := deps.db.Query(queryStr, query.Args()...)
if err != nil {
return nil, err
}
var parties []Party
for rows.Next() {
var party Party
err := pan.Unmarshal(rows, &party)
if err != nil {
return nil, err
}
parties = append(parties, party)
}
return parties, nil
}
func (deps Dependencies) GetPartyByMagicWord(ctx context.Context, word string) (Party, error) {
var p Party
query := pan.New("SELECT " + pan.Columns(p).String() + " FROM " + pan.Table(p)).Where()
query.Comparison(p, "MagicWord", "=", word).Limit(1).Flush(" ")
queryStr, err := query.PostgreSQLString()
if err != nil {
return p, err
}
rows, err := deps.db.Query(queryStr, query.Args()...)
if err != nil {
return p, err
}
var found bool
for rows.Next() {
err := pan.Unmarshal(rows, &p)
if err != nil {
return p, err
}
found = true
}
if !found {
return Party{}, ErrMagicWordNotFound
}
return p, nil
}
func (deps Dependencies) ListPeople(ctx context.Context, party string) ([]Person, error) {
var p Person
query := pan.New("SELECT " + pan.Columns(p).String() + " FROM " + pan.Table(p))
if party != "" {
query.Where()
query.Comparison(p, "PartyID", "=", party)
}
query.Flush(" ")
queryStr, err := query.PostgreSQLString()
if err != nil {
return nil, err
}
rows, err := deps.db.Query(queryStr, query.Args()...)
if err != nil {
return nil, err
}
var people []Person
for rows.Next() {
var person Person
err := pan.Unmarshal(rows, &person)
if err != nil {
return nil, err
}
people = append(people, person)
}
return people, nil
}
func (deps Dependencies) GetPeople(ctx context.Context, ids []string) ([]Person, error) {
var p Person
ifIDs := make([]interface{}, 0, len(ids))
for _, id := range ids {
ifIDs = append(ifIDs, id)
}
query := pan.New("SELECT "+pan.Columns(p).String()+" FROM "+pan.Table(p)).Where().In(p, "ID", ifIDs...).Flush(" ")
queryStr, err := query.PostgreSQLString()
if err != nil {
return nil, err
}
deps.log.Println("GetPeople query string:", queryStr)
rows, err := deps.db.Query(queryStr, query.Args()...)
if err != nil {
return nil, err
}
var people []Person
for rows.Next() {
var person Person
err := pan.Unmarshal(rows, &person)
if err != nil {
return nil, err
}
people = append(people, person)
}
return people, nil
}