summaryrefslogtreecommitdiff
path: root/server/guest
diff options
context:
space:
mode:
Diffstat (limited to 'server/guest')
-rw-r--r--server/guest/handler.go31
-rw-r--r--server/guest/models.go2
-rw-r--r--server/guest/store.go38
3 files changed, 33 insertions, 38 deletions
diff --git a/server/guest/handler.go b/server/guest/handler.go
index 20ed2dc..62de5be 100644
--- a/server/guest/handler.go
+++ b/server/guest/handler.go
@@ -6,7 +6,6 @@ import (
"net/http"
"os"
"regexp"
- "strconv"
"time"
"github.com/golang-jwt/jwt/v5"
@@ -14,7 +13,7 @@ import (
var (
guestRegex = regexp.MustCompile(`^/api/guests/*$`)
- guestIDRegex = regexp.MustCompile(`^/api/guests/([0-9]+)$`)
+ guestIdRegex = regexp.MustCompile(`^/api/guests/([a-z0-9-]+)$`)
)
type GuestHandler struct {
@@ -26,7 +25,7 @@ type GuestStore interface {
Get() ([]Guest, error)
Add(guest Guest) error
Update(guest Guest) error
- Delete(id int) error
+ Delete(id string) error
}
type appError struct {
@@ -48,13 +47,13 @@ func (handler *GuestHandler) ServeHTTP(responseWriter http.ResponseWriter,
responseWriter.WriteHeader(http.StatusOK)
case request.Method == http.MethodPost && request.URL.Path == "/api/guests/login":
handler.handleLogIn(responseWriter, request)
- case request.Method == http.MethodPut && guestIDRegex.MatchString(request.URL.Path):
+ case request.Method == http.MethodPut && guestIdRegex.MatchString(request.URL.Path):
handler.handlePut(responseWriter, request)
case request.Method == http.MethodGet && guestRegex.MatchString(request.URL.Path):
handler.handleGet(responseWriter, request)
case request.Method == http.MethodPost && guestRegex.MatchString(request.URL.Path):
handler.handlePost(responseWriter, request)
- case request.Method == http.MethodDelete && guestIDRegex.MatchString(request.URL.Path):
+ case request.Method == http.MethodDelete && guestIdRegex.MatchString(request.URL.Path):
handler.handleDelete(responseWriter, request)
default:
responseWriter.WriteHeader(http.StatusNotFound)
@@ -194,7 +193,7 @@ func (handler *GuestHandler) putGuest(request *http.Request) *appError {
if err := handler.validateToken(request, guestKey); err != nil {
return err
}
- if handler.findID(request) {
+ if handler.findId(request) {
return &appError{errors.New("ID not found"), "{ \"message\": \"ID not found\" }",
http.StatusNotFound}
}
@@ -244,8 +243,8 @@ func (handler *GuestHandler) parseWithClaims(token string, claims *Claims,
})
}
-func (handler *GuestHandler) findID(request *http.Request) bool {
- matches := guestIDRegex.FindStringSubmatch(request.URL.Path)
+func (handler *GuestHandler) findId(request *http.Request) bool {
+ matches := guestIdRegex.FindStringSubmatch(request.URL.Path)
return len(matches) < 2
}
@@ -310,7 +309,7 @@ func (handler *GuestHandler) postGuest(request *http.Request) *appError {
func (handler *GuestHandler) checkExistingGuests(guests []Guest, newGuest Guest) error {
for _, guest := range guests {
- if guest.ID == newGuest.ID {
+ if guest.Id == newGuest.Id {
return errors.New("ID already exists")
}
}
@@ -326,15 +325,11 @@ func (handler *GuestHandler) deleteGuest(request *http.Request) *appError {
if err := handler.validateToken(request, adminKey); err != nil {
return err
}
- if handler.findID(request) {
+ if handler.findId(request) {
return &appError{errors.New("ID not found"), "{ \"message\": \"ID not found\" }", http.StatusNotFound}
}
- guestID, err := getID(request)
- if err != nil {
- return &appError{err, "{ \"message\": \"Failed to parse ID\" }",
- http.StatusInternalServerError}
- }
- err = handler.store.Delete(int(guestID))
+ guestId := getId(request)
+ err = handler.store.Delete(guestId)
if err != nil {
return &appError{err, "{ \"message\": \"Failed to get guests\" }",
http.StatusInternalServerError}
@@ -342,6 +337,6 @@ func (handler *GuestHandler) deleteGuest(request *http.Request) *appError {
return nil
}
-func getID(request *http.Request) (int64, error) {
- return strconv.ParseInt(guestIDRegex.FindStringSubmatch(request.URL.Path)[1], 10, 32)
+func getId(request *http.Request) string {
+ return guestIdRegex.FindStringSubmatch(request.URL.Path)[1]
}
diff --git a/server/guest/models.go b/server/guest/models.go
index d93bd53..280ee97 100644
--- a/server/guest/models.go
+++ b/server/guest/models.go
@@ -3,7 +3,7 @@ package guest
import "github.com/golang-jwt/jwt/v5"
type Guest struct {
- ID int `json:"id"`
+ Id string `json:"id"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Attendance string `json:"attendance"`
diff --git a/server/guest/store.go b/server/guest/store.go
index 16123e3..56a8c2c 100644
--- a/server/guest/store.go
+++ b/server/guest/store.go
@@ -48,7 +48,7 @@ func (store Store) Find(name Name) (Guest, error) {
func createGuest(name Name, guestRows pgx.Rows) (Guest, bool) {
var guest Guest
for guestRows.Next() {
- err := guestRows.Scan(&guest.ID, &guest.FirstName, &guest.LastName,
+ err := guestRows.Scan(&guest.Id, &guest.FirstName, &guest.LastName,
&guest.Attendance, &guest.Email, &guest.Message, &guest.PartySize)
if err != nil {
return Guest{}, false
@@ -64,13 +64,13 @@ func createGuest(name Name, guestRows pgx.Rows) (Guest, bool) {
func addParty(guestWithoutParty Guest, partyRows pgx.Rows) (Guest, error) {
guestWithParty := guestWithoutParty
for partyRows.Next() {
- var guestID int
+ var guestId string
var partyMember Name
- err := partyRows.Scan(&guestID, &partyMember.FirstName, &partyMember.LastName)
+ err := partyRows.Scan(&guestId, &partyMember.FirstName, &partyMember.LastName)
if err != nil {
return Guest{}, err
}
- if guestID == guestWithParty.ID {
+ if guestId == guestWithParty.Id {
guestWithParty.PartyList = append(guestWithParty.PartyList, partyMember)
}
}
@@ -108,7 +108,7 @@ func (store Store) createGuestSlice(guestRows pgx.Rows) ([]Guest, error) {
guests := []Guest{}
for guestRows.Next() {
var guest Guest
- err := guestRows.Scan(&guest.ID, &guest.FirstName, &guest.LastName,
+ err := guestRows.Scan(&guest.Id, &guest.FirstName, &guest.LastName,
&guest.Attendance, &guest.Email, &guest.Message, &guest.PartySize)
if err != nil {
return []Guest{}, err
@@ -122,14 +122,14 @@ func addPartySlice(guestsWithoutParty []Guest,
partyRows pgx.Rows) ([]Guest, error) {
guestsWithParty := guestsWithoutParty
for partyRows.Next() {
- var guestID int
+ var guestId string
var partyMember Name
- err := partyRows.Scan(&guestID, &partyMember.FirstName, &partyMember.LastName)
+ err := partyRows.Scan(&guestId, &partyMember.FirstName, &partyMember.LastName)
if err != nil {
return []Guest{}, err
}
for i, guest := range guestsWithParty {
- if guestID == guest.ID {
+ if guestId == guest.Id {
guestsWithParty[i].PartyList = append(guest.PartyList, partyMember)
}
}
@@ -147,7 +147,7 @@ func (store Store) Add(guest Guest) error {
func (store Store) insertGuest(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 := store.database.Exec(context.Background(), statement, guest.ID,
+ _, err := store.database.Exec(context.Background(), statement, guest.Id,
guest.FirstName, guest.LastName, guest.Attendance, guest.Email,
guest.Message, guest.PartySize)
return err
@@ -157,7 +157,7 @@ func (store Store) Update(guest Guest) error {
if err := store.updateGuest(guest); err != nil {
return err
}
- if err := store.deleteParty(guest.ID); err != nil {
+ if err := store.deleteParty(guest.Id); err != nil {
return err
}
return store.insertParty(guest)
@@ -167,13 +167,13 @@ func (store Store) updateGuest(guest Guest) error {
statement := `update guest set attendance = $1, email = $2, message = $3,
party_size = $4 where id = $5`
_, err := store.database.Exec(context.Background(), statement,
- guest.Attendance, guest.Email, guest.Message, guest.PartySize, guest.ID)
+ guest.Attendance, guest.Email, guest.Message, guest.PartySize, guest.Id)
return err
}
-func (store Store) deleteParty(guestID int) error {
+func (store Store) deleteParty(guestId string) error {
statement := "delete from party where guest_id = $1"
- _, err := store.database.Exec(context.Background(), statement, guestID)
+ _, err := store.database.Exec(context.Background(), statement, guestId)
return err
}
@@ -181,7 +181,7 @@ func (store Store) insertParty(guest Guest) error {
statement := `insert into party (guest_id, first_name, last_name)
values ($1, $2, $3)`
for _, pg := range guest.PartyList {
- _, err := store.database.Exec(context.Background(), statement, guest.ID,
+ _, err := store.database.Exec(context.Background(), statement, guest.Id,
pg.FirstName, pg.LastName)
if err != nil {
return err
@@ -190,18 +190,18 @@ func (store Store) insertParty(guest Guest) error {
return nil
}
-func (store Store) Delete(guestID int) error {
- if err := store.deleteGuest(guestID); err != nil {
+func (store Store) Delete(guestId string) error {
+ if err := store.deleteGuest(guestId); err != nil {
return err
}
- if err := store.deleteParty(guestID); err != nil {
+ if err := store.deleteParty(guestId); err != nil {
return err
}
return nil
}
-func (store Store) deleteGuest(guestID int) error {
+func (store Store) deleteGuest(guestId string) error {
statement := "delete from guest where id = $1"
- _, err := store.database.Exec(context.Background(), statement, guestID)
+ _, err := store.database.Exec(context.Background(), statement, guestId)
return err
}