summaryrefslogtreecommitdiff
path: root/server/guest/handler.go
diff options
context:
space:
mode:
authorMichael Hunteman <michael@huntm.net>2024-10-20 18:52:16 -0700
committerMichael Hunteman <michael@huntm.net>2024-10-20 18:52:16 -0700
commit5a65b3a653eb4a9ca62332fa65c8d352e6dc782f (patch)
tree6f2fabb06936a7290a2f5e94a1b3aaf4534bf87c /server/guest/handler.go
parent5c8f0ca091ff7f962260c75a0ffe4c99e7cdca3c (diff)
Use UUID
Diffstat (limited to 'server/guest/handler.go')
-rw-r--r--server/guest/handler.go31
1 files changed, 13 insertions, 18 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]
}