/
schema.go
2031 lines (1883 loc) · 45.4 KB
/
schema.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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"database/sql"
"github.com/stripe/stripe-go"
"github.com/stripe/stripe-go/customer"
"time"
"log"
"io/ioutil"
"encoding/base64"
"syscall"
"strings"
"code.google.com/p/go-uuid/uuid"
"os"
)
type SMS struct {
To string
Message string
}
/*
* Trucks and meuns
*/
type Truck struct {
// Raw fields
Id int64
Owner int64
Name string
Location_lat string
Location_lon string
Open_from time.Time
Open_until time.Time
Phone string
Description string
// Go fields
Distance float64
Menus []*Menu
Open_now bool
}
type Menu struct {
// Raw fields
Id int64
Truck_id int64
Name string
Description string
Flagship bool
// Go fields
Items []*MenuItem
}
type MenuItem struct {
// Raw fields
Id int64
Truck_id int64
Menu_id int64
Name string
Price float64
Description string
Pic_url string
ListOptions []*MenuItemOption
ToggleOptions []*MenuToggleOption
}
type MenuToggleOption struct {
Id int64
Item_id int64
Name string
Price_modifier float64
}
type MenuItemOption struct {
Id int64
Item_id int64
Name string
Values []*MenuItemOptionItem
}
type MenuItemOptionItem struct {
Id int64
Option_id int64
Option_name string
Price_modifier float64
}
type Order struct {
// Raw fields
Id int64
User_id int64
Truck_id int64
Date time.Time
Pickup_time time.Time
Phone string
// Go fields
Items []*OrderItem
}
type OrderItem struct {
// Raw fields
Id int64
Order_id int64
Item_id int64
Quantity int64
// ID numbers into the toggle / list option tables
ToggleOptions []int64
ListOptionValues []int64
}
// For trucks
type PaymentToken struct {
Id int64
User_id int64
Name string
stripe_key string
Token string
Created time.Time
}
type UserData struct {
// Raw fields
Id int64
Email string
First_name string
Last_name string
password_hash string
password_salt string
password_reset_key string
Phone string
Verified bool
// Go fields
orders []*Order
Session_token string
}
type Session struct {
User *UserData
Expires time.Time
}
// The Dinner Structs
type GuestData struct {
Id int64
First_name string
Last_name string
Facebook_id string
Facebook_long_token string
Prof_pic string
Bio string
// Go fields
Session_token string
Is_host bool
Phone string
Email string
Phone_verified bool
Email_verified bool
Last4s []int64
}
type FacebookResp struct {
Id string
Email string
First_name string
Last_name string
}
type KitchenSession struct {
Guest *GuestData
Expires time.Time
}
type Meal struct {
Id int64
Host_id int64
Price float64
Title string
Description string
Processed int64
Published bool
// go fields
Pics []*Pic
Popups []*Popup
}
type StripeToken struct {
Id int64
Guest_id int64
Stripe_token string
Last4 int64
}
type HostData struct {
Id int64
Guest_id int64
Address string
City string
State string
Stripe_user_id string
Stripe_access_token string
Stripe_refresh_token string
Bio string
}
type AttendeeData struct {
Guest *GuestData
Seats int64
}
type PopupBooking struct {
Id int64
Guest_id int64
Popup_id int64
Seats int64
Last4 int64
Nudge_count int64
Meal_price float64
Last_nudge time.Time
}
type Review struct {
Id int64
Guest_id int64
Rating int64
Comment string
Popup_id int64
Date time.Time
Tip_percent int64
Suggestion string
}
type Pic struct {
Name string
Caption string
}
func GetUserById(db *sql.DB, id int64) (*UserData, error) {
row := db.QueryRow(`SELECT Id, Email, First_name, Last_name,
Password_salt, Password_hash,
Password_reset_key, Phone, Verified
FROM User WHERE Id = ?`, id)
return readUserLine(row)
}
func GetGuestByFbId(db *sql.DB, fb_id string) (*GuestData, error) {
row := db.QueryRow(`SELECT Id, First_name, Last_name,
Facebook_id, Facebook_long_token, Prof_pic, Bio
FROM Guest WHERE Facebook_id = ?`, fb_id)
return readGuestLine(row)
}
func GetGuestById(db *sql.DB, id int64) (*GuestData, error) {
row := db.QueryRow(`SELECT Id, First_name, Last_name,
Facebook_id, Facebook_long_token, Prof_pic, Bio
FROM Guest WHERE Id = ?`, id)
return readGuestLine(row)
}
func GetGuestByEmail(db *sql.DB, email string) (*GuestData, error) {
email_row := db.QueryRow(`SELECT Guest_id FROM GuestEmail WHERE Email = ?`, email)
guest_id := 0
err := email_row.Scan(&guest_id)
if err != nil {
log.Println(err)
return nil, err
}
log.Println(guest_id)
return GetGuestById(db, int64(guest_id))
}
func GetGuestByHostId(db *sql.DB, host_id int64) (*GuestData, error) {
guest_id_in_host_table := db.QueryRow(`SELECT Guest_id FROM Host WHERE Id = ?`, host_id)
guest_id := 0
err := guest_id_in_host_table.Scan(&guest_id)
if err != nil {
log.Println(err)
return nil, err
}
return GetGuestById(db, int64(guest_id))
}
func GetFacebookPic(fb_id string) string {
return "https://graph.facebook.com/" + fb_id + "/picture?width=200&height=200"
}
func GetPhonePinForGuest(db *sql.DB, guest_id int64) (int64, error) {
pin := 0
row := db.QueryRow("SELECT Pin FROM GuestPhone WHERE Guest_id = ?", guest_id)
err := row.Scan(&pin)
return int64(pin), err
}
func GetPhoneForGuest(db *sql.DB, guest_id int64) (string, error) {
phone := "0"
row := db.QueryRow("SELECT Phone FROM GuestPhone WHERE Guest_id = ?", guest_id)
err := row.Scan(&phone)
return phone, err
}
func VerifyPhoneForGuest(db *sql.DB, guest_id int64) (error) {
_, err := db.Exec(`UPDATE GuestPhone SET Verified = 1 WHERE Guest_id = ?` , guest_id)
return err
}
func GetPhoneStatus(db *sql.DB, guest_id int64) (bool, error) {
verified := 0
row := db.QueryRow("SELECT Verified FROM GuestPhone WHERE Guest_id = ?", guest_id)
err := row.Scan(&verified)
return (verified == 1), err
}
func GetEmailForGuest(db *sql.DB, guest_id int64) (string, error) {
email := ""
row := db.QueryRow("SELECT Email FROM GuestEmail WHERE Guest_id = ?", guest_id)
err := row.Scan(&email)
return email, err
}
func GetEmailCodeForGuest(db *sql.DB, guest_id int64) (string, error) {
code := ""
row := db.QueryRow("SELECT Code FROM GuestEmail WHERE Guest_id = ?", guest_id)
err := row.Scan(&code)
return code, err
}
func GetEmailStatus(db *sql.DB, guest_id int64) (bool, error) {
verified := 0
row := db.QueryRow("SELECT Verified FROM GuestEmail WHERE Guest_id = ?", guest_id)
err := row.Scan(&verified)
return (verified == 1), err
}
func VerifyEmailForGuest(db *sql.DB, guest_id int64) (error) {
_, err := db.Exec(`UPDATE GuestEmail SET Verified = 1 WHERE Guest_id = ?` , guest_id)
return err
}
func RecordFollowHost(db *sql.DB, guest_id, host_id int64) error {
_, err := db.Exec(`INSERT INTO HostFollowers
(Guest_id, Host_id)
VALUES
(?, ?)`,
guest_id, host_id,)
return err
}
func GetGuestFollowsHost(db *sql.DB, guest_id, host_id int64)(bool) {
row := db.QueryRow("SELECT Id FROM HostFollowers WHERE Guest_id = ? AND Host_id = ?",
guest_id,
host_id)
follow_id := 0
log.Println("About to scan row")
if err := row.Scan(&follow_id); err == sql.ErrNoRows {
return false
}
log.Println(follow_id)
return true
}
func GetHostByGuestId(db *sql.DB, guest_id int64) (*HostData, error) {
log.Println(guest_id)
row := db.QueryRow(`SELECT Id, Guest_id, Address, City, State,
Stripe_user_id, Stripe_access_token, Stripe_refresh_token, Bio
FROM Host WHERE Guest_id = ?`, guest_id)
return readHostLine(row)
}
func GetHostById(db *sql.DB, id int64) (*HostData, error) {
row := db.QueryRow(`SELECT Id, Guest_id, Address, City, State,
Stripe_user_id, Stripe_access_token, Stripe_refresh_token, Bio
FROM Host WHERE Id = ?`, id)
return readHostLine(row)
}
func GetHostBySession(db *sql.DB, session_manager *SessionManager, session_id string) (*HostData, error) {
session, err := session_manager.GetGuestSession(session_id)
if err != nil {
return nil, err
}
return GetHostByGuestId(db, session.Guest.Id)
}
func GetNewHostStatus(db *sql.DB, host_id int64) (bool, error) {
total_revenue := float64(0)
meals, err := GetMealsForHost(db, host_id)
if err != nil {
log.Println(err)
return false, err
}
for _, meal := range meals {
// for each meal: get all popups
for _, popup := range meal.Popups { // for each popup, get the bookings
bookings, err := GetBookingsForPopup(db, popup.Id)
if err != nil {
log.Println(err)
return false, err
}
for _, booking := range bookings { // for each booking, add the revenue contributed to the total
if booking.Last4 == 0 {
continue
}
total_revenue += float64(booking.Seats) * meal.Price
if total_revenue > 400 {
return false, nil
}
}
}
}
return true, nil
}
func GetUserByEmail(db *sql.DB, email string) (*UserData, error) {
row := db.QueryRow(`SELECT Id, Email, First_name, Last_name,
Password_salt, Password_hash,
Password_reset_key, Phone, Verified
FROM User WHERE Email = ?`, email)
return readUserLine(row)
}
func GetUserByPhone(db *sql.DB, phone string) (*UserData, error) {
row := db.QueryRow(`SELECT Id, Email, First_name, Last_name,
Password_salt, Password_hash,
Password_reset_key, Phone, Verified
FROM User WHERE Phone = ?`, phone)
return readUserLine(row)
}
func GetMealById(db *sql.DB, id int64) (*Meal, error) {
row := db.QueryRow(`SELECT Id, Host_id, Price, Title,
Description, Processed, Published
FROM Meal
WHERE Id = ?`, id)
return readMealLine(row)
}
func GetMealByPopupId(db *sql.DB, popup_id int64) (*Meal, error) {
popup, err := GetPopupById(db, popup_id)
if err != nil {
return nil, err
}
return GetMealById(db, popup.Meal_id)
}
func GetMealsForHost(db *sql.DB, host_id int64) ([]*Meal, error) {
rows, err := db.Query(`SELECT Id, Host_id, Price, Title, Description, Processed, Published
FROM Meal
WHERE Host_id = ?
ORDER BY Id DESC`,
host_id)
if err != nil {
log.Println(err)
log.Println(host_id)
return nil, err
}
defer rows.Close()
meals, err := read_meal_rows(db, rows)
if err != nil {
return nil, err
}
for _, meal := range meals {
meal.Popups, err = GetPopupsForMeal(db, meal.Id)
if err != nil {
log.Println("Problem is with popups")
return nil, err
}
}
return meals, nil
}
func read_meal_rows(db *sql.DB, rows *sql.Rows) ([]*Meal, error) {
meals := make([]*Meal, 0)
for rows.Next() {
meal := new(Meal)
if err := rows.Scan(
&meal.Id,
&meal.Host_id,
&meal.Price,
&meal.Title,
&meal.Description,
&meal.Processed,
&meal.Published,
); err != nil {
return nil, err
}
meals = append(meals, meal)
}
return meals, nil
}
// avoids double-charging if both qa and prod are running chronjobs
func SetPopupProcessed(db *sql.DB, popup_id int64) error{
_, err := db.Exec(`
UPDATE Popup
SET Processed = 1
WHERE Id = ?
`,
popup_id,
)
if err != nil {
log.Println(err)
}
return err
}
func GetMealReviewByGuestIdAndPopupId(db *sql.DB, guest_id int64, meal_id int64) (meal_review *Review, err error) {
row := db.QueryRow(`SELECT Id, Guest_id, Rating, Comment, Popup_id, Date, Tip_percent
FROM HostReview
WHERE Guest_id = ? AND Meal_id = ?`, guest_id, meal_id)
return readReviewLine(row)
}
func GetUpcomingAttendingMealsForGuest(db *sql.DB, guest_id int64) ([]*Meal_read, error) {
meal_reads := make([]*Meal_read, 0)
log.Println("GetUpcomingAttendingMealsForGuest start")
attending_popup_ids, err := GetAttendingPopupIdsForGuest(db, guest_id)
if err != nil {
return nil, err
}
if (len(attending_popup_ids) == 0){
return meal_reads, nil
}
in_clause := "in (?" + strings.Repeat(",?", len(attending_popup_ids)-1) + ")"
popup_rows, err := db.Query(`SELECT Id, Meal_id, Starts, Rsvp_by, Address, City, State, Capacity, Processed
FROM Popup
WHERE Starts > DATE(NOW()) AND Id ` + in_clause, attending_popup_ids...)
if err != nil {
return nil, err
}
popups, err := read_popup_rows(popup_rows, db)
if err != nil {
return nil, err
}
for _, popup := range popups {
meal_read, err := GetMealCardDataById(db, popup.Meal_id)
if err != nil {
return nil, err
}
meal_read.Popups = []*Popup{popup}
meal_reads = append(meal_reads, meal_read)
}
log.Println("GetUpcomingAttendingMealsForGuest end")
return meal_reads, nil
}
func GetAttendingPopupIdsForGuest(db *sql.DB, guest_id int64) ([]interface{}, error) {
rows, err := db.Query(`
SELECT Popup_id
FROM PopupBooking
WHERE Guest_id = ?`,
guest_id,
)
if err != nil {
return nil, err
}
attending_popup_ids_slice := make([]int64, 0)
defer rows.Close()
for rows.Next() {
popup_id := int64(0)
if err := rows.Scan(
&popup_id,
); err != nil {
return nil, err
}
attending_popup_ids_slice = append(attending_popup_ids_slice, popup_id)
}
attending_popup_ids := make([]interface{}, len(attending_popup_ids_slice))
for i, d := range attending_popup_ids_slice {
attending_popup_ids[i] = d
}
return attending_popup_ids, nil
}
func GetAllMealsFromDB(db *sql.DB) ([]*Meal_read, error) {
rows, err := db.Query(`
SELECT Id
FROM Popup
ORDER BY Rsvp_by DESC`,
)
if err != nil {
log.Println(err)
return nil, err
}
defer rows.Close()
meal_reads := make([]*Meal_read, 0)
// get the guest for each guest id and add them to the slice of guests
for rows.Next() {
popup_id := 0
if err := rows.Scan(
&popup_id,
); err != nil {
log.Println(err)
return nil, err
}
popup, err := GetPopupById(db, int64(popup_id))
log.Println(popup_id)
if err != nil {
log.Println(err)
return nil, err
}
if (meal_reads_contains(popup.Meal_id, meal_reads)) {
continue
}
meal_read, err := GetMealCardDataById(db, popup.Meal_id)
if err != nil {
log.Println(err)
continue
}
meal_reads = append(meal_reads, meal_read)
}
return meal_reads, nil
}
func GetUpcomingMealsFromDB(db *sql.DB) ([]*Meal_read, error) {
rows, err := db.Query(`
SELECT Id
FROM Popup
WHERE Rsvp_by > ? AND Id > 0
ORDER BY Rsvp_by ASC`,
time.Now(),
)
if err != nil {
return nil, err
}
defer rows.Close()
meal_reads := make([]*Meal_read, 0)
// get the guest for each guest id and add them to the slice of guests
for rows.Next() {
popup_id := 0
if err := rows.Scan(
&popup_id,
); err != nil {
return nil, err
}
popup, err := GetPopupById(db, int64(popup_id))
log.Println(popup_id)
if err != nil {
return nil, err
}
if (meal_reads_contains(popup.Meal_id, meal_reads)) {
continue
}
meal_read, err := GetMealCardDataById(db, popup.Meal_id)
if err != nil {
log.Println(err)
continue
}
meal_reads = append(meal_reads, meal_read)
}
return meal_reads, nil
}
func meal_reads_contains(meal_id int64, meal_reads []*Meal_read) bool{
for _, meal_read := range meal_reads {
if meal_read.Id == meal_id {
return true
}
}
return false
}
// Returns meal objects with all fields necessary for display in a meal card
func GetMealCardDataById(db *sql.DB, meal_id int64) (*Meal_read, error) {
meal, err := GetMealById(db, meal_id)
if err != nil {
log.Println(err)
return nil, err
}
meal_data := new(Meal_read)
meal_data.Id = meal.Id
meal_data.Title = meal.Title
meal_data.Description = meal.Description
meal_data.Price, err = GetMealPrice(db, meal)
if err != nil {
log.Println(err)
return nil, err
}
meal_data.Pics, err = GetAllPicsForMeal(db, meal.Id)
if err != nil{
log.Println(err)
return nil, err
}
host, err := GetHostById(db, meal.Host_id)
if err != nil {
log.Println(err)
return nil, err
}
host_as_guest, err := GetGuestById(db, host.Guest_id)
if err != nil {
log.Println(err)
return nil, err
}
meal_data.Host_name = host_as_guest.First_name
if host_as_guest.Prof_pic != "" {
meal_data.Host_pic = "https://yaychakula.com/img/" + host_as_guest.Prof_pic
} else {
meal_data.Host_pic = GetFacebookPic(host_as_guest.Facebook_id)
}
meal_data.Host_id = host.Id
meal_data.Host_bio = host_as_guest.Bio
meal_data.Popups, err = GetPopupsForMeal(db, meal.Id)
meal_data.New_host, err = GetNewHostStatus(db, meal.Host_id)
if err != nil {
log.Println(err)
return nil, err
}
return meal_data, nil
}
func GetUpcomingPopupsForMeal(db *sql.DB, meal_id int64) ([]*Popup, error) {
rows, err := db.Query(`SELECT Id, Meal_id, Starts, Rsvp_by, Address, City, State, Capacity, Processed
FROM Popup
WHERE Meal_id = ? AND Rsvp_by > ?`, meal_id, time.Now(),
)
if err != nil {
log.Println(err)
return nil, err
}
defer rows.Close()
return read_popup_rows(rows, db)
}
func GetPopupsForMeal(db *sql.DB, meal_id int64) ([]*Popup, error) {
rows, err := db.Query(`SELECT Id, Meal_id, Starts, Rsvp_by, Address, City, State, Capacity, Processed
FROM Popup
WHERE Meal_id = ?
ORDER BY Rsvp_by ASC`, meal_id,
)
if err != nil {
log.Println(err)
return nil, err
}
defer rows.Close()
return read_popup_rows(rows, db)
}
func GetPopupById(db *sql.DB, popup_id int64) (*Popup, error) {
row := db.QueryRow(`SELECT Id, Meal_id, Starts, Rsvp_by, Address, City, State, Capacity, Processed
FROM Popup
WHERE Id = ?`, popup_id,
)
return readPopupLine(row)
}
func GetPopupsFromTimeWindow(db *sql.DB, window_starts, window_ends time.Time) ([]*Popup, error) {
rows, err := db.Query(`SELECT Id, Meal_id, Starts, Rsvp_by, Address, City, State, Capacity, Processed
FROM Popup
WHERE Starts > ? AND Starts < ?`,
window_starts,
window_ends)
if err != nil {
return nil, err
}
defer rows.Close()
return read_popup_rows(rows, db)
}
func read_popup_rows(rows *sql.Rows, db *sql.DB) ([]*Popup, error) {
popups := make([]*Popup, 0)
for rows.Next() {
popup := new(Popup)
if err := rows.Scan(
&popup.Id,
&popup.Meal_id,
&popup.Starts,
&popup.Rsvp_by,
&popup.Address,
&popup.City,
&popup.State,
&popup.Capacity,
&popup.Processed,
); err != nil {
return nil, err
}
attendees, err := GetAttendeesForPopup(db, popup.Id)
if err != nil {
log.Println(err)
return nil, err
}
popup.Attendees, err = get_attendee_reads_for_attendees(attendees)
if err != nil {
log.Println(err)
return nil, err
}
full_address := popup.Address + ", " + popup.City + ", " + popup.State
popup.Maps_url, err = GetStaticMapsUrlForMeal(db, full_address)
if err != nil {
log.Println(err)
return nil, err
}
popups = append(popups, popup)
}
return popups, nil
}
func CreatePopup(db *sql.DB, popup *Popup) (sql.Result, error) {
return db.Exec(
`INSERT INTO Popup
(Meal_id, Capacity, Starts, Rsvp_by, Address, City, State, Processed)
VALUES
(?, ?, ?, ?, ?, ?, ?, 0)
`,
popup.Meal_id,
popup.Capacity,
popup.Starts,
popup.Rsvp_by,
popup.Address,
popup.City,
popup.State,
)
}
func GetBookingById(db *sql.DB, booking_id int64) (*PopupBooking, error) {
row := db.QueryRow(`
SELECT Id, Popup_id, Guest_id, Seats, Last4, Nudge_count, Last_nudge, Meal_price
FROM PopupBooking
WHERE Id = ?`, booking_id,
)
return readBookingLine(row)
}
func GetBookingsForPopup(db *sql.DB, popup_id int64) ([]*PopupBooking, error) {
rows, err := db.Query(`
SELECT Id, Popup_id, Guest_id, Seats, Last4, Nudge_count, Last_nudge, Meal_price
FROM PopupBooking
WHERE Popup_id = ?`, popup_id,
)
if err != nil {
return nil, err
}
defer rows.Close()
bookings := make([]*PopupBooking, 0)
for rows.Next() {
booking := new(PopupBooking)
if err := rows.Scan(
&booking.Id,
&booking.Popup_id,
&booking.Guest_id,
&booking.Seats,
&booking.Last4,
&booking.Nudge_count,
&booking.Last_nudge,
&booking.Meal_price,
); err != nil {
return nil, err
}
bookings = append(bookings, booking)
}
return bookings, nil
}
func GetBookingByGuestAndPopupId(db *sql.DB, guest_id, popup_id int64) (*PopupBooking, error) {
row := db.QueryRow(`SELECT Id, Popup_id, Guest_id, Seats, Last4, Nudge_count, Last_nudge, Meal_price
FROM PopupBooking
WHERE Guest_id = ? AND Popup_id = ?`, guest_id, popup_id,
)
return readBookingLine(row)
}
func SavePopupBooking(db *sql.DB, booking *PopupBooking) error {
_, err := db.Exec(
`INSERT INTO PopupBooking
(Guest_id, Popup_id, Seats, Last4, Nudge_count, Last_nudge, Meal_price)
VALUES
(?, ?, ?, ?, ?, ?, ?)`,
booking.Guest_id,
booking.Popup_id,
booking.Seats,
booking.Last4,
booking.Nudge_count,
time.Now(),
booking.Meal_price)
return err
}
func get_attendee_reads_for_attendees(attendees []*AttendeeData) ([]*Attendee_read, error) {
attendee_reads := make([]*Attendee_read, 0)
for _, attendee := range attendees {
as_guest := attendee.Guest
attendee_read := new(Attendee_read)
attendee_read.First_name = as_guest.First_name
if as_guest.Prof_pic != "" {
attendee_read.Prof_pic_url = "https://yaychakula.com/img/" + as_guest.Prof_pic
} else if as_guest.Facebook_id != "" {
attendee_read.Prof_pic_url = GetFacebookPic(attendee.Guest.Facebook_id)
}
attendee_read.Seats = attendee.Seats
attendee_reads = append(attendee_reads, attendee_read)
}
return attendee_reads, nil
}
func GetAttendeesForPopup(db *sql.DB, popup_id int64) ([]*AttendeeData, error) {
// get all the guest ids attending the meal
rows, err := db.Query(`
SELECT Guest_id, Seats
FROM PopupBooking
WHERE Popup_id = ?`, popup_id,
)
if err != nil {
return nil, err
}
defer rows.Close()
attendees := make([]*AttendeeData, 0)
// get the guest for each guest id and add them to the slice of guests
for rows.Next() {
var guest_id int64
var seats int64
if err := rows.Scan(
&guest_id,
&seats,
); err != nil {
return nil, err
}
guest_data, err := GetGuestById(db, guest_id)
if err == nil {
attendee := new(AttendeeData)
attendee.Guest = guest_data
attendee.Seats = seats
attendees = append(attendees, attendee)
} else {
log.Println(err)
}
}
return attendees, nil
}
func GetReviewById(db *sql.DB, review_id int64) (*Review, error) {
row := db.QueryRow(`SELECT Id, Guest_id, Rating, Comment, Popup_id, Date, Tip_percent, Suggestion
FROM HostReview
WHERE Id = ?`, review_id)
return readReviewLine(row)
}
/*
UPDATE HostReview
SET "Meal_id" = (SELECT "Id" FROM Popup WHERE "Meal_id" = (SELECT "Meal_id" FROM HostReview))
*/
func GetReviewsForHost(db *sql.DB, host_id int64) ([]*Review, error) {
// get all meal ids associated with host
rows, err := db.Query(`SELECT Id FROM Meal WHERE Host_id = ?`, host_id,)
if err != nil {
log.Println(err)
return nil, err
}
defer rows.Close()
host_reviews := make([]*Review, 0)
for rows.Next() { // for each meal, get all associated popups.
meal_id := int64(0)
if err := rows.Scan(
&meal_id,
); err != nil {
log.Println(err)
return nil, err
}
popups, err := GetPopupsForMeal(db, meal_id)
if err != nil {
log.Println(err)
return nil, err
}
for _, popup := range popups { // for each popup, get all associated reviews
popup_reviews, err := GetReviewsForPopup(db, popup.Id)
if err != nil {
log.Println(err)
continue
}
host_reviews = append(host_reviews, popup_reviews...)
}
}
return host_reviews, nil
}
func GetReviewsForPopup(db *sql.DB, popup_id int64) ([]*Review, error) {
rows, err := db.Query(`
SELECT Id, Guest_id, Rating, Comment, Popup_id, Date, Tip_percent, Suggestion
FROM HostReview
WHERE Popup_id = ?`,
popup_id,
)
if err != nil {
return nil, err
}
defer rows.Close()
reviews := make([]*Review, 0)
// get the guest for each guest id and add them to the slice of guests
for rows.Next() {
review := new(Review)
if err := rows.Scan(
&review.Id,
&review.Guest_id,
&review.Rating,
&review.Comment,
&review.Popup_id,
&review.Date,
&review.Tip_percent,
&review.Suggestion,
); err != nil {
log.Println(err)
return nil, err
}
reviews = append(reviews, review)