diff options
Diffstat (limited to 'server/guest')
-rw-r--r-- | server/guest/models.go | 34 | ||||
-rw-r--r-- | server/guest/store.go | 117 |
2 files changed, 151 insertions, 0 deletions
diff --git a/server/guest/models.go b/server/guest/models.go new file mode 100644 index 0000000..aa2c56f --- /dev/null +++ b/server/guest/models.go @@ -0,0 +1,34 @@ +package guest + +import "github.com/golang-jwt/jwt/v5" + +type Guest struct { + Id int `json:"id"` + FirstName string `json:"firstName"` + LastName string `json:"lastName"` + Attendance string `json:"attendance"` + Email string `json:"email"` + Message string `json:"message"` + PartySize int `json:"partySize"` + PartyList []PartyGuest `json:"partyList"` +} + +type PartyGuest struct { + FirstName string `json:"firstName"` + LastName string `json:"lastName"` +} + +type Credentials struct { + FirstName string `json:"firstName"` + LastName string `json:"lastName"` +} + +type Claims struct { + Credentials Credentials `json:"credentials"` + jwt.RegisteredClaims +} + +type LoginResponse struct { + Guest Guest `json:"guest"` + Token string `json:"token"` +} diff --git a/server/guest/store.go b/server/guest/store.go new file mode 100644 index 0000000..525bb71 --- /dev/null +++ b/server/guest/store.go @@ -0,0 +1,117 @@ +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() + + 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 { + return guest, nil + } + } + return guest, errors.New("Guest does not exist") +} + +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 +} |