From add714d21f110dcf3fb45bf2631cb946526db1ca Mon Sep 17 00:00:00 2001 From: Adriano Caloiaro Date: Sat, 29 Jul 2023 13:26:45 -0600 Subject: [PATCH] Support editing garbage --- content/{posts => garbage}/new.md | 0 hugo.toml | 2 +- partials/garbage/edit.html | 46 +++++++ partials/posts.html | 15 ++- server.go | 130 ++++++++++++++++++- themes/terminal/assets/css/post.scss | 4 +- themes/terminal/layouts/_default/baseof.html | 2 +- 7 files changed, 185 insertions(+), 14 deletions(-) rename content/{posts => garbage}/new.md (100%) create mode 100644 partials/garbage/edit.html diff --git a/content/posts/new.md b/content/garbage/new.md similarity index 100% rename from content/posts/new.md rename to content/garbage/new.md diff --git a/hugo.toml b/hugo.toml index 41a8050..a4efb91 100644 --- a/hugo.toml +++ b/hugo.toml @@ -105,7 +105,7 @@ enableInlineShortcodes = true [[languages.en.menu.main]] identifier = "submit" name = "Submit" - url = "/posts/new" + url = "/garbage/new" [module] # In case you would like to make changes to the theme and keep it locally in you repository, diff --git a/partials/garbage/edit.html b/partials/garbage/edit.html new file mode 100644 index 0000000..a291e5d --- /dev/null +++ b/partials/garbage/edit.html @@ -0,0 +1,46 @@ +

Edit Post

+
+ + + + + + + + + + + +
+
+ +
+ diff --git a/partials/posts.html b/partials/posts.html index 4e5194f..6ce5586 100644 --- a/partials/posts.html +++ b/partials/posts.html @@ -1,14 +1,14 @@
+ {{ $apiBaseURL := .ApiBaseUrl }} {{range .Posts}}
-

{{ .Title }}

+

{{ .Title }}

diff --git a/server.go b/server.go index a74c3fc..1bb9f56 100644 --- a/server.go +++ b/server.go @@ -173,6 +173,8 @@ func main() { }) r.Route("/garbage", func(r chi.Router) { r.Post("/new", createGarbageHandler) + r.Get("/{garbage_id}/edit", editGarbageHandler) + r.Put("/{garbage_id}", editGarbageUpdateHandler) }) fmt.Printf("Starting API server on port 1314\n") @@ -181,6 +183,113 @@ func main() { } } +func editGarbageUpdateHandler(w http.ResponseWriter, r *http.Request) { + garbageID := chi.URLParam(r, "garbage_id") + userID := sessions.GetString(r.Context(), "userID") + + if userID == "" { + ise(errors.New("not logged in"), w) + return + } + + if err := r.ParseForm(); err != nil { + ise(err, w) + return + } + + url := r.PostForm.Get("url") + title := r.PostForm.Get("title") + garbage := r.PostForm.Get("garbage") + // TODO this is an arbitrary length that will likely need to change + if len(garbage) == 10 { + w.WriteHeader(400) + return + } + + metadata := map[string]any{} + tags := r.Form["tags"] + if len(tags) > 0 { + metadata["tags"] = tags + } + + ctx := context.Background() + _, err := db.Exec(ctx, + "UPDATE garbages SET (title, content, url, metadata) = ($1, $2, $3, $4) WHERE id = $5", + title, + garbage, + url, + metadata, + garbageID) + + if err != nil { + log.Println(err) + return + } + + log.Println("HERE") + //var buff = bytes.NewBufferString("") + //tmpl := template.Must(template.ParseFiles("partials/users/created.html")) + //err = tmpl.Execute(buff, map[string]any{"Email": email}) + //if err != nil { + //ise(err, w) + //return + //} + + w.Header().Add("hx-location", baseURL()) +} + +func editGarbageHandler(w http.ResponseWriter, r *http.Request) { + garbageID := chi.URLParam(r, "garbage_id") + userID := sessions.GetString(r.Context(), "userID") + if userID == "" { + return // TODO render an error + } + + garbage := Garbage{} + ctx := context.Background() + err := pgxscan.Get( + ctx, + db, + &garbage, + `SELECT id, title, content, metadata, url FROM garbages WHERE id = $1`, garbageID) + if err != nil { + ise(err, w) + return + } + + availableTags := []string{ + "Nouned verb", + "Verbed noun", + "Nouned adjective", + "Novel garbage", + } + selectedTags := map[string]bool{} + if tags, ok := garbage.Metadata["tags"].([]interface{}); ok { + for _, tag := range tags { + selectedTags[tag.(string)] = true + } + } + + tmplVars := map[string]any{ + "ApiBaseUrl": apiURL(), + "Garbage": garbage, + "SelectedTags": selectedTags, + "AvailableTags": availableTags, + } + tmpl := template.Must(template.ParseFiles("partials/garbage/edit.html")) + + var buff = bytes.NewBufferString("") + err = tmpl.Execute(buff, tmplVars) + if err != nil { + ise(err, w) + return + } + + w.Header().Add("Content-Type", "text/html") + w.WriteHeader(http.StatusOK) + w.Write(buff.Bytes()) +} + // newUserValidationHandler checks whether a username is available during account creation func newUserValidationHandler(w http.ResponseWriter, r *http.Request) { tmplVars := map[string]any{"ApiBaseUrl": apiURL()} @@ -292,7 +401,7 @@ func loginHandler(w http.ResponseWriter, r *http.Request) { // logoutHandler handles users logout requests func logoutHandler(w http.ResponseWriter, r *http.Request) { - sessions.Clear(r.Context()) + sessions.Destroy(r.Context()) http.Redirect(w, r, baseURL(), http.StatusFound) } @@ -301,7 +410,6 @@ func navUserItems(w http.ResponseWriter, r *http.Request) { userID := sessions.GetString(r.Context(), "userID") var tmpl *template.Template - log.Println("user id:", userID) if userID == "" { tmpl = template.Must(template.ParseFiles("partials/nav/non_user_nav_items.html")) } else { @@ -371,7 +479,6 @@ func emailVerification(w http.ResponseWriter, r *http.Request) { } func createGarbageHandler(w http.ResponseWriter, r *http.Request) { - userID := sessions.GetString(r.Context(), "userID") if userID == "" { ise(errors.New("not logged in"), w) @@ -392,9 +499,11 @@ func createGarbageHandler(w http.ResponseWriter, r *http.Request) { } url := r.PostForm.Get("url") + + metadata := map[string]any{} tags := r.Form["tags"] - metadata := map[string]any{ - "tags": tags, + if len(tags) > 0 { + metadata["tags"] = tags } ctx := context.Background() @@ -526,7 +635,11 @@ func garbageBin(w http.ResponseWriter, r *http.Request) { var buff = bytes.NewBufferString("") tmpl := template.Must(template.ParseFiles("partials/posts.html")) - err = tmpl.Execute(buff, map[string]any{"Posts": posts}) + err = tmpl.Execute(buff, map[string]any{ + "Posts": posts, + "ApiBaseUrl": apiURL(), + "LoggedIn": isLoggedIn(r), + }) if err != nil { ise(err, w) return @@ -683,3 +796,8 @@ func ise(err error, w http.ResponseWriter) { fmt.Fprintf(w, "error: %v", err) w.WriteHeader(http.StatusInternalServerError) } + +func isLoggedIn(r *http.Request) bool { + var _, err = r.Cookie("session_id") + return err == nil +} diff --git a/themes/terminal/assets/css/post.scss b/themes/terminal/assets/css/post.scss index 1c7127d..dc0f36b 100644 --- a/themes/terminal/assets/css/post.scss +++ b/themes/terminal/assets/css/post.scss @@ -29,11 +29,11 @@ &-meta { font-size: 1rem; margin-bottom: 10px; - color: transparentize($accent, .3); + color: transparentize($accent, .6); & > *:not(:first-child) { &::before { - content: "::"; + content: "|"; display: inline-block; margin: 0 8px; } diff --git a/themes/terminal/layouts/_default/baseof.html b/themes/terminal/layouts/_default/baseof.html index 6c97af1..0caaf76 100644 --- a/themes/terminal/layouts/_default/baseof.html +++ b/themes/terminal/layouts/_default/baseof.html @@ -13,7 +13,7 @@ {{ partial "header.html" . }} -
+
{{ block "main" . }} {{ end }}