aboutsummaryrefslogtreecommitdiff
path: root/src/post/routes.go
diff options
context:
space:
mode:
authorAlexander Kavon <me+git@alexkavon.com>2024-01-23 01:40:39 -0500
committerAlexander Kavon <me+git@alexkavon.com>2024-01-23 01:40:39 -0500
commit8510c36ded85740885e67b59ee2ec2360986c0a9 (patch)
treec8c4c70458bb65b0ee134f80cb364db851e9c7a2 /src/post/routes.go
parentf329e7f2095a57967bfe86563401236d53a15924 (diff)
new tables: tags, post_tags, generated sqlboiler tags model, updated posts/get template, updated posts table to take nullable description and user_id foreign key
Diffstat (limited to 'src/post/routes.go')
-rw-r--r--src/post/routes.go23
1 files changed, 19 insertions, 4 deletions
diff --git a/src/post/routes.go b/src/post/routes.go
index 09457d3..7714920 100644
--- a/src/post/routes.go
+++ b/src/post/routes.go
@@ -4,9 +4,11 @@ import (
"fmt"
"log"
"net/http"
+ "strconv"
"github.com/go-chi/chi/v5"
"github.com/volatiletech/sqlboiler/v4/boil"
+ . "github.com/volatiletech/sqlboiler/v4/queries/qm"
"gitlab.com/alexkavon/newsstand/src/models"
"gitlab.com/alexkavon/newsstand/src/server"
"gitlab.com/alexkavon/newsstand/src/sessions"
@@ -30,7 +32,7 @@ var Routes = server.Routes{
server.Route{
Name: "Get",
Method: "GET",
- Path: "/p/{:id}",
+ Path: "/p/{id}",
HandlerFunc: Get,
},
}
@@ -45,8 +47,8 @@ func Store(s *server.Server) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var post models.Post
post.Title = r.PostFormValue("title")
- post.Url = r.PostFormValue("url")
- post.Description = r.PostFormValue("description")
+ post.Description.SetValid(r.PostFormValue("description"))
+ post.URL.SetValid(r.PostFormValue("url"))
// validate post
// process post, look for spamminess, bad url
@@ -65,6 +67,19 @@ func Store(s *server.Server) http.HandlerFunc {
func Get(s *server.Server) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
- post := models.FindPost(r.Context(), s.Db.ToSqlDb(), chi.URLParam(r, "id"))
+ postId, err := strconv.Atoi(chi.URLParam(r, "id"))
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ post, err := models.Posts(
+ Where("id = ?", postId),
+ Load("User"),
+ Load("Tags"),
+ ).One(r.Context(), s.Db.ToSqlDb())
+ if err != nil {
+ log.Fatal(err)
+ }
+ s.Ui.Render(w, r, "post/get", map[string]any{"post": post})
}
}