kit-clj / kit-clj.github.io

Kit documentation
https://kit-clj.github.io/
20 stars 27 forks source link

"Creating a controller for guestbook" in the guestbook documentation accurate? #65

Closed nackjicholson closed 1 month ago

nackjicholson commented 1 month ago

The code mentioned in the getting started documentation here, does not work for me.

(ns kit.guestbook.web.controllers.guestbook
  (:require
   [clojure.tools.logging :as log]
   [kit.guestbook.web.routes.utils :as utils]
   [ring.util.http-response :as http-response]))

(defn save-message!
  [{:keys [query-fn]} {{:strs [name message]} :form-params :as request}]
  (log/debug "saving message" name message)
  (try
    (if (or (empty? name) (empty? message))
      (cond-> (http-response/found "/")
        (empty? name)
        (assoc-in [:flash :errors :name] "name is required")
        (empty? message)
        (assoc-in [:flash :errors :message] "message is required"))
      (do
        (query-fn :save-message! {:name name :message message})
        (http-response/found "/")))
    (catch Exception e
      (log/error e "failed to save message!")
      (-> (http-response/found "/")
          (assoc :flash {:errors {:unknown (.getMessage e)}})))))

The request object is not possible to destructure in this way, for me. I also notice that the utils are imported but not used. I think the solution is that in kit, to access things injected by integrant you actually have to do something like (let [{:keys [query-fn]} (utils/route-data request)] ...). Is that correct? It would be super convenient to destructure it from the request as seen in the docs, for the record. I'm unfortunately struggling to get started with kit because I can't follow the documentation.

yogthos commented 1 month ago

You shouldn't need to use utils/route-data since opts from Integrant get passed to api-routes and then can be passed directly to the controller, which is the first parameter in the example above. You can see the full example project here https://github.com/kit-clj/kit-examples/tree/master/guestbook

nackjicholson commented 1 month ago

I appreciate the response @yogthos, this is helpful to see the guestbook code. The below line does use utils/route-data though.

https://github.com/kit-clj/kit-examples/blob/master/guestbook/src/clj/kit/guestbook/web/controllers/guestbook.clj#L10

yogthos commented 1 month ago

The example is a bit out of date, but if you look in the way the latest template is set up here https://github.com/kit-clj/kit/blob/master/libs/deps-template/resources/io/github/kit_clj/kit/src/clj/web/routes/api.clj#L35

The _opts from Integrant are being passed in to the function that generates the API routes, so they can be directly passed to the controller function from there.

nackjicholson commented 1 month ago

@yogthos Do I need to partial the opts into the controler fn's from there? If so, then I think I have what I need to keep going. But, to the question of the PR, I think this does mean the documentation is quite out of date and inaccurate. It's even more out of date than the example app code.

yogthos commented 1 month ago

Yup, using (partial your-handler opts) will work here. I'll take a look at updating the relevant docs to match the latest before closing the issue.