diff options
Diffstat (limited to 'server')
-rw-r--r-- | server/.bashrc | 1 | ||||
-rw-r--r-- | server/cmd/main.go | 109 | ||||
-rw-r--r-- | server/go.mod | 3 | ||||
-rw-r--r-- | server/guests/models.go | 17 | ||||
-rw-r--r-- | server/guests/store.go | 26 | ||||
-rw-r--r-- | server/post.json | 15 |
6 files changed, 171 insertions, 0 deletions
diff --git a/server/.bashrc b/server/.bashrc new file mode 100644 index 0000000..802d935 --- /dev/null +++ b/server/.bashrc @@ -0,0 +1 @@ +PATH=$PATH:/c/Go/bin diff --git a/server/cmd/main.go b/server/cmd/main.go new file mode 100644 index 0000000..ed4f748 --- /dev/null +++ b/server/cmd/main.go @@ -0,0 +1,109 @@ +package main + +import ( + "encoding/json" + "log" + "net/http" + "regexp" + "strconv" + + "git.huntm.net/wedding/server/guests" +) + +type guestHandler struct { + store guestStore +} + +type guestStore interface { + Get() (map[int]guests.Guest, error) + Add(id int, guest guests.Guest) error + Update(id int, guest guests.Guest) error +} + +var ( + guestRe = regexp.MustCompile(`^/guests/*$`) + guestIdRe = regexp.MustCompile(`^/guests/([0-9]+)$`) +) + +func newGuestHandler(s guestStore) *guestHandler { + return &guestHandler{ + store: s, + } +} + +func (h *guestHandler) getGuests(w http.ResponseWriter, _ *http.Request) { + guests, err := h.store.Get() + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + } + + jsonBytes, err := json.Marshal(guests) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + } + + w.WriteHeader(http.StatusOK) + w.Write(jsonBytes) +} + +func (h *guestHandler) createGuest(w http.ResponseWriter, r *http.Request) { + var guest guests.Guest + err := json.NewDecoder(r.Body).Decode(&guest) + defer r.Body.Close() + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + + err = h.store.Add(guest.Id, guest) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + + w.WriteHeader(http.StatusOK) +} + +func (h *guestHandler) updateGuest(w http.ResponseWriter, r *http.Request) { + matches := guestIdRe.FindStringSubmatch(r.URL.Path) + if len(matches) < 2 { + http.Error(w, "No id found", http.StatusBadRequest) + } + + var guest guests.Guest + err := json.NewDecoder(r.Body).Decode(&guest) + defer r.Body.Close() + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + } + + id, err := strconv.Atoi(matches[1]) + if err != nil { + http.Error(w, "Cannot convert string to integer", http.StatusBadRequest) + } + + err = h.store.Update(id, guest) + if err != nil { + http.Error(w, "Guest not found", http.StatusBadRequest) + } +} + +func (h *guestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + switch { + case r.Method == http.MethodGet && guestRe.MatchString(r.URL.Path): + h.getGuests(w, r) + case r.Method == http.MethodPost && guestRe.MatchString(r.URL.Path): + h.createGuest(w, r) + case r.Method == http.MethodPut && guestIdRe.MatchString(r.URL.Path): + h.updateGuest(w, r) + } +} + +func main() { + store := guests.NewMemStore() + guestHandler := newGuestHandler(store) + + mux := http.NewServeMux() + mux.Handle("/guests/", guestHandler) + log.Fatal(http.ListenAndServe(":8080", mux)) +} diff --git a/server/go.mod b/server/go.mod new file mode 100644 index 0000000..737eac4 --- /dev/null +++ b/server/go.mod @@ -0,0 +1,3 @@ +module git.huntm.net/wedding/server + +go 1.22.2 diff --git a/server/guests/models.go b/server/guests/models.go new file mode 100644 index 0000000..c68a4c3 --- /dev/null +++ b/server/guests/models.go @@ -0,0 +1,17 @@ +package guests + +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"` +} diff --git a/server/guests/store.go b/server/guests/store.go new file mode 100644 index 0000000..f1d8558 --- /dev/null +++ b/server/guests/store.go @@ -0,0 +1,26 @@ +package guests + +type MemStore struct { + guestMap map[int]Guest +} + +func NewMemStore() *MemStore { + guestMap := make(map[int]Guest) + return &MemStore{ + guestMap, + } +} + +func (m MemStore) Get() (map[int]Guest, error) { + return m.guestMap, nil +} + +func (m MemStore) Add(id int, guest Guest) error { + m.guestMap[id] = guest + return nil +} + +func (m MemStore) Update(id int, guest Guest) error { + m.guestMap[id] = guest + return nil +} diff --git a/server/post.json b/server/post.json new file mode 100644 index 0000000..f550b1b --- /dev/null +++ b/server/post.json @@ -0,0 +1,15 @@ +{ + "id": 1, + "firstName": "Michael", + "lastName": "Hunteman", + "attendance": "true", + "email": "mhunteman@cox.net", + "message": "Hi", + "partySize": 1, + "partyList": [ + { + "firstName": "Madison", + "lastName": "Rossitto" + } + ] +} |