diff options
author | Michael Hunteman <michael@huntm.net> | 2024-05-24 10:44:46 -0700 |
---|---|---|
committer | Michael Hunteman <michael@huntm.net> | 2024-05-24 10:44:46 -0700 |
commit | fa72f449465905ff2fc9b79886cb5e4b948a8fa8 (patch) | |
tree | c963038f7d7563226e96d258a3d8e22057a74365 /server/guests | |
parent | 0b2490ed46d1958625dc3c660455a5465adaf763 (diff) |
Connect server to database
Diffstat (limited to 'server/guests')
-rw-r--r-- | server/guests/store.go | 84 |
1 files changed, 74 insertions, 10 deletions
diff --git a/server/guests/store.go b/server/guests/store.go index f1d8558..597cc80 100644 --- a/server/guests/store.go +++ b/server/guests/store.go @@ -1,26 +1,90 @@ package guests +import ( + "context" + + "github.com/jackc/pgx/v5/pgxpool" +) + type MemStore struct { - guestMap map[int]Guest + db *pgxpool.Pool } -func NewMemStore() *MemStore { - guestMap := make(map[int]Guest) +func NewMemStore(db *pgxpool.Pool) *MemStore { return &MemStore{ - guestMap, + db, } } -func (m MemStore) Get() (map[int]Guest, error) { - return m.guestMap, nil +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(id int, guest Guest) error { - m.guestMap[id] = guest +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(id int, guest Guest) error { - m.guestMap[id] = guest +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 = "update party set first_name = $1, last_name = $2 where guest_id = $3" + for _, pg := range guest.PartyList { + _, err = m.db.Exec(context.Background(), statement, pg.FirstName, pg.LastName, guest.Id) + if err != nil { + return err + } + } return nil } |