duct-framework / docs

Documentation for Duct
21 stars 9 forks source link

Invalid token: ::response/create #7

Closed brantchyoga closed 3 years ago

brantchyoga commented 3 years ago

In the tutorial at the users.clj where and after finishing the file with

(defmethod ig/init-key ::create [_ {:keys [db]}]
        (fn [{[_ email password] :ataraxy/result}]
            (let [id (create-user db email password)]
                [::response/created (str "/users/" id)])))

I reset the repl and get the error-- Syntax error reading source at (users.clj:21:35). Invalid token: ::response/created. I am new to clojure and duct. Is there something I should know at this point in the tutorial that would fix this?

weavejester commented 3 years ago

It sounds like you've mistyped the namespace declaration. What does your first ns form look like?

brantchyoga commented 3 years ago
(ns todo.handler.users
    (:require [ataraxy.response :as response]
              [buddy.hashers :as hashers]
              [clojure.java.jdbc :as jdbc]
              duct.database.spl
              [integrant.core :as ig])

    (defprotocol Users
        (create-user [db email password]))
    (extend-protocol Users
        duct.database.sql.Boundary
        (create-user [{db :spec} email password]
            (let [pw-hash (hashers/derive password)
                results (jdbc/insert! db :users {:email email, :password pw-hash})]
            (-> results ffirst val))))
    (defmethod ig/init-key ::create [_ {:keys [db]}]
        (fn [{[_ email password] :ataraxy/result}]
            (let [id (create-user db email password)]
                [::response/created (str "/users/" id)])))
)

This is my users.clj file so far. Thank you very responding 👍

brantchyoga commented 3 years ago

This is my config.edn as well in case the error comes from there...

{:duct.profile/base
 {:duct.core/project-ns todo

  :duct.router/ataraxy
  {:routes 
    {[:get "/"] [:todo.handler/index]
    [:get "/entries"] [:todo.handler.entries/list]

    [:post "/entries" {{:keys [description]} :body-params}]
    [:todo.handler.entries/create description]

    [:get "/entries/" id] [:todo.handler.entries/find ^int id]
    [:delete "/entries/" id] [:todo.handler.entries/destroy ^int id]

    [:post "/users" {{:keys [email password]} :body-params}]
    [:todo.handler.users/create email password]
    }}

  :todo.handler.users/create
  {:db #ig/ref :duct.database/sql}

  [:duct.handler.sql/insert :todo.handler.entries/create]
  {:request {[_ description] :ataraxy/result}
   :sql     ["INSERT INTO entries (description) VALUES (?)" description]
   :location "/entries/{last_insert_rowid}"}

  [:duct.handler.sql/query :todo.handler.entries/list]
  {:sql ["SELECT * FROM entries"]
   :hrefs {:href "/entries/{id}"}}

  [:duct.handler.static/ok :todo.handler/index]
  {:body {:entries "/entries"}}

  [:duct.handler.sql/query-one :todo.handler.entries/find]
  {:request {[_ id] :ataraxy/result}
   :sql ["SELECT * FROM entries WHERE id = ?" id]
   :hrefs {:href "/entries/{id}"}}

  [:duct.handler.sql/execute :todo.handler.entries/destroy]
  {:request {[_ id] :ataraxy/result}
   :sql ["DELETE FROM entries WHERE id = ?" id]}

  :duct.migrator/ragtime
  {:migrations [#ig/ref :todo.migration/create-entries
                #ig/ref :todo.migration/create-users]}

  [:duct.migrator.ragtime/sql :todo.migration/create-entries]
  {:up ["CREATE TABLE entries (id INTEGER PRIMARY KEY, description TEXT)"]
   :down ["DROP TABLE entries"]}

   [:duct.migrator.ragtime/sql :todo.migration/create-users]
   {:up ["CREATE TABLE users (id INTEGER PRIMARY KEY, email TEXT UNIQUE, password TEXT)"]
    :down ["DROP TABLE users"]}
 }

 :duct.profile/dev   #duct/include "dev"
 :duct.profile/local #duct/include "local"
 :duct.profile/prod  {}

 :duct.module/logging {}
 :duct.module.web/api
 {}
 :duct.module/sql
 {}}
weavejester commented 3 years ago

It looks like you've put your code in your ns declaration by mistake. It should look like this:

(ns todo.handler.users
  (:require [ataraxy.response :as response]
            [buddy.hashers :as hashers]
            [clojure.java.jdbc :as jdbc]
            duct.database.sql
            [integrant.core :as ig]))

(defprotocol Users
  (create-user [db email password]))

(extend-protocol Users
  duct.database.sql.Boundary
  (create-user [{db :spec} email password]
    (let [pw-hash (hashers/derive password)
          results (jdbc/insert! db :users {:email email, :password pw-hash})]
      (-> results ffirst val))))

(defmethod ig/init-key ::create [_ {:keys [db]}]
  (fn [{[_ email password] :ataraxy/result}]
    (let [id (create-user db email password)]
      [::response/created (str "/users/" id)])))
brantchyoga commented 3 years ago

Thank you! Any good resources for learning clojure? I am going through the docs currently but any other resources would be awesome.

weavejester commented 3 years ago

You could take a look at Clojure for the Brave and True or Clojure by Example.