From 8510c36ded85740885e67b59ee2ec2360986c0a9 Mon Sep 17 00:00:00 2001 From: Alexander Kavon Date: Tue, 23 Jan 2024 01:40:39 -0500 Subject: 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 --- src/post/routes.go | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'src/post/routes.go') 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}) } } -- cgit v1.2.3