diff options
Diffstat (limited to 'src/post/routes.go')
| -rw-r--r-- | src/post/routes.go | 23 |
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}) } } |
