From d183c42825f2f423385368a374bd9adb8d817e7b Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Sun, 5 Feb 2023 21:50:28 +0400 Subject: [PATCH] Added guestbook POST handler for forms. --- internal/http/handlers.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/internal/http/handlers.go b/internal/http/handlers.go index 063cc3d..abf8c97 100644 --- a/internal/http/handlers.go +++ b/internal/http/handlers.go @@ -12,6 +12,7 @@ import ( "git.arav.top/Arav/dwelling-home/pkg/util" "git.arav.top/Arav/dwelling-home/web" gbsqlite "git.arav.top/Arav/justguestbook/database/sqlite" + "git.arav.top/Arav/justguestbook/guestbook" ) type Handlers struct { @@ -116,6 +117,34 @@ func (h *Handlers) Guestbook(w http.ResponseWriter, r *http.Request) { web.Guestbook("/ Guestbook", h.guestbookOwner, "", pageCount, entries, r, w) } +func (h *Handlers) GuestbookPost(w http.ResponseWriter, r *http.Request) { + var entry *guestbook.Entry + var err error + + if r.Header.Get("Content-Type") == "application/x-www-form-urlencoded" { + r.ParseForm() + + if r.FormValue("name") == "" { + r.Form.Set("name", "Anonymous") + } + + entry, err = guestbook.NewEntry(r.FormValue("name"), r.FormValue("message"), + r.FormValue("website"), r.FormValue("hide_website") != "") + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + } + + if err = h.guestbookDB.NewEntry(entry); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + http.Redirect(w, r, "/guestbook", http.StatusMovedPermanently) +} + func (h *Handlers) RSS(w http.ResponseWriter, r *http.Request) { posts := []mindflow.Post{ {Category: "Kek", Date: time.Now(), Title: "LMAO", Body: "Testing!"},