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
|
package guest
import (
"context"
"errors"
"github.com/jackc/pgx/v5/pgxpool"
)
type MemStore struct {
db *pgxpool.Pool
}
func NewMemStore(db *pgxpool.Pool) *MemStore {
return &MemStore{
db,
}
}
func (m MemStore) FindGuest(creds Credentials) (Guest, error) {
rows, err := m.db.Query(context.Background(), "select * from guest")
var guest Guest
if err != nil {
return guest, err
}
defer rows.Close()
found := false
for rows.Next() {
err := rows.Scan(&guest.Id, &guest.FirstName, &guest.LastName, &guest.Attendance, &guest.Email, &guest.Message, &guest.PartySize)
if err != nil {
return guest, err
}
if guest.FirstName == creds.FirstName && guest.LastName == creds.LastName {
found = true
break
}
}
rows, err = m.db.Query(context.Background(), "select * from party")
if err != nil {
return guest, err
}
defer rows.Close()
for rows.Next() {
var guestId int
var partyGuest PartyGuest
err := rows.Scan(&guestId, &partyGuest.FirstName, &partyGuest.LastName)
if err != nil {
return guest, err
}
if guestId == guest.Id {
guest.PartyList = append(guest.PartyList, partyGuest)
}
}
if found {
return guest, nil
}
return guest, errors.New("Guest not found")
}
func (m MemStore) Get() ([]Guest, error) {
rows, err := m.db.Query(context.Background(), "select * from guest")
if err != nil {
return nil, err
}
defer rows.Close()
guestSlice := []Guest{}
for rows.Next() {
var guest Guest
err := rows.Scan(&guest.Id, &guest.FirstName, &guest.LastName, &guest.Attendance, &guest.Email, &guest.Message, &guest.PartySize)
if err != nil {
return guestSlice, err
}
guestSlice = append(guestSlice, guest)
}
rows, err = m.db.Query(context.Background(), "select * from party")
if err != nil {
return guestSlice, err
}
defer rows.Close()
for rows.Next() {
var guestId int
var partyGuest PartyGuest
err := rows.Scan(&guestId, &partyGuest.FirstName, &partyGuest.LastName)
if err != nil {
return guestSlice, err
}
for i, g := range guestSlice {
if guestId == g.Id {
guestSlice[i].PartyList = append(g.PartyList, partyGuest)
}
}
}
return guestSlice, nil
}
func (m MemStore) Add(guest Guest) error {
statement := "insert into guest (id, first_name, last_name, attendance, email, message, party_size) values ($1, $2, $3, $4, $5, $6, $7)"
_, err := m.db.Exec(context.Background(), statement, guest.Id, guest.FirstName, guest.LastName, guest.Attendance, guest.Email, guest.Message, guest.PartySize)
if err != nil {
return err
}
statement = "insert into party (guest_id, first_name, last_name) values ($1, $2, $3)"
for _, pg := range guest.PartyList {
_, err = m.db.Exec(context.Background(), statement, guest.Id, pg.FirstName, pg.LastName)
if err != nil {
return err
}
}
return nil
}
func (m MemStore) Update(guest Guest) error {
statement := "update guest set attendance = $1, email = $2, message = $3, party_size = $4 where id = $5"
_, err := m.db.Exec(context.Background(), statement, guest.Attendance, guest.Email, guest.Message, guest.PartySize, guest.Id)
if err != nil {
return err
}
statement = "delete from party where guest_id = $1"
_, err = m.db.Exec(context.Background(), statement, guest.Id)
if err != nil {
return err
}
statement = "insert into party (guest_id, first_name, last_name) values ($1, $2, $3)"
for _, pg := range guest.PartyList {
_, err = m.db.Exec(context.Background(), statement, guest.Id, pg.FirstName, pg.LastName)
if err != nil {
return err
}
}
return nil
}
|