Skip to content

Instantly share code, notes, and snippets.

@dustingetz
Created December 31, 2014 01:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dustingetz/2dbaf643e1d07e201a5e to your computer and use it in GitHub Desktop.
Save dustingetz/2dbaf643e1d07e201a5e to your computer and use it in GitHub Desktop.
(ns streaker-service.routes
(:require [io.pedestal.http :as bootstrap]
[io.pedestal.http.route :as route]
[io.pedestal.http.body-params :as body-params]
[io.pedestal.http.route.definition :refer [defroutes]]
[io.pedestal.interceptor :as interceptor]
[streaker-service.api :as api]
[streaker-service.page :as page]
[streaker-service.pedestal-util :as pedestal-util]
[streaker-service.models.question :as question]
[streaker-service.models.friend :as friend]))
(defn collection-route [route-name query typeinfo]
{:get [route-name (fn [request]
(api/collection-read route-name query typeinfo))]
:post (fn [request]
(api/collection-create typeinfo))
})
(defroutes routes
[[["/api" {:get api/index}
^:interceptors [(body-params/body-params)
pedestal-util/auto-content-type
pedestal-util/combine-body-params]
["/questions" (collection-route :api/questions
'[:find ?e :where [?e :Question/text]]
question/typeinfo)]
["/friends" (collection-route :api/friends
'[:find ?e :where [?e :Friend/email]]
friend/typeinfo)]
["/entity/:id/:tx" {:get api/entity :post api/entity-post}]
]]])
@dustingetz
Copy link
Author

Take two

(def routes
  (expand-routes
   `[[["/api" {:get api/index}
       ^:interceptors [(body-params/body-params)
                       pedestal-util/auto-content-type
                       pedestal-util/combine-body-params]

       ["/questions" ~(collection-route :api/questions
                                        '[:find ?e :where [?e :Question/text]]
                                        question/typeinfo)]

       ["/friends" ~(collection-route :api/friends
                                      '[:find ?e :where [?e :Friend/email]]
                                      friend/typeinfo)]


       ["/entity/:id/:tx" {:get api/entity :post api/entity-post}]

       ]]]))

@dustingetz
Copy link
Author

Take three - "no route found"

(defn entity-get [{{:keys [id tx]} :path-params}]
  (api/entity-read (Long/parseLong id)
                   (Long/parseLong tx)))

(defn entity-put [req]
  (api/entity-update {}))

(defn entity-delete [req]
  (api/entity-delete {}))


(defn questions-get [req]
  (api/collection-read ::questions-get
                       '[:find ?e :where [?e :Question/text]]
                       question/typeinfo))

(defn questions-post [{:keys [:streaker-service.pedestal-util/body-params]}]
  (api/collection-create body-params question/typeinfo))

(defn friends-get [req]
  (api/collection-read ::friends-get
                       '[:find ?e :where [?e :Friend/email]]
                       friend/typeinfo))

(defroutes routes
  [[["/app" {:get page/index}
     ^:interceptors [bootstrap/html-body]]

    ["/echo" {:get page/echo}
     ^:interceptors [bootstrap/html-body]]

    ["/api" {:get api/index}
     ^:interceptors [(body-params/body-params)
                     pedestal-util/auto-content-type
                     pedestal-util/combine-body-params]

     ["/questions" {:get questions-get
                    :post questions-post
                    }]

     ["/friends" {:get friends-get}]

     ["/entity/:id/:tx" {:get entity-get
                         :put entity-put
                         :delete entity-delete}]

     ]]])

@dustingetz
Copy link
Author

take four - echo and app work, api and questions don't work

(defn entity-get [{{:keys [id tx]} :path-params}]
  (api/entity-read (Long/parseLong id)
                   (Long/parseLong tx)))

(defn entity-put [req]
  (api/entity-update {}))

(defn entity-delete [req]
  (api/entity-delete {}))


(defn questions-get [req]
  (api/collection-read ::questions-get
                       '[:find ?e :where [?e :Question/text]]
                       question/typeinfo))

(defn questions-post [{:keys [:streaker-service.pedestal-util/body-params]}]
  (api/collection-create body-params question/typeinfo))

(defn friends-get [req]
  (api/collection-read ::friends-get
                       '[:find ?e :where [?e :Friend/email]]
                       friend/typeinfo))

(def routes
  (expand-routes
   [[["/app" {:get (interceptor/handler :app page/index)}
      ^:interceptors [bootstrap/html-body]]

     ["/echo" {:get [(interceptor/handler :echo page/echo)]}
      ^:interceptors [bootstrap/html-body]]

     ["/api" {:get (interceptor/handler :api api/index)}
      ^:interceptors [(body-params/body-params)
                      pedestal-util/auto-content-type
                      pedestal-util/combine-body-params]

      ["/questions" {:get (interceptor/handler :api/questions questions-get)
                     ;;:post questions-post
                     }]

      ;; ["/friends" {:get friends-get}]

      ;; ["/entity/:id/:tx" {:get entity-get
      ;;                     :put entity-put
      ;;                     :delete entity-delete}]

      ]]]))

@dustingetz
Copy link
Author

attempt 6 - questions Exception: Not found

(defhandler entity-get [{{:keys [id tx]} :path-params}]
  (api/entity-read (Long/parseLong id)
                   (Long/parseLong tx)))

(defhandler entity-put [req]
  (api/entity-update {}))

(defhandler entity-delete [req]
  (api/entity-delete {}))


(defhandler questions-get [req]
  (api/collection-read ::questions-get
                       '[:find ?e :where [?e :Question/text]]
                       question/typeinfo))

(defhandler questions-post [{:keys [:streaker-service.pedestal-util/body-params]}]
  (api/collection-create body-params question/typeinfo))

(defhandler friends-get [req]
  (api/collection-read ::friends-get
                       '[:find ?e :where [?e :Friend/email]]
                       friend/typeinfo))

(def routes
  (expand-routes
   [[["/app" {:get 'page/index}
      ^:interceptors [bootstrap/html-body]]

     ["/echo" {:get 'page/echo}
      ^:interceptors [bootstrap/html-body]]

     ["/api" {:get 'api/index}
      ^:interceptors [(body-params/body-params)
                      pedestal-util/auto-content-type
                      pedestal-util/combine-body-params]

      ["/questions" {:get questions-get
                     :post questions-post}]

      ["/friends" {:get friends-get}]

      ["/entity/:id/:tx" {:get entity-get
                          :put entity-put
                          :delete entity-delete}]

      ]]]))

@dustingetz
Copy link
Author

attempt 7

(defn questions-get [req]
  (api/collection-read ::questions-get
                       '[:find ?e :where [?e :Question/text]]
                       question/typeinfo))

(defn mk-handlers []
  `{:get questions-get})


(def routes
  (expand-routes
   `[[["/api" {:get api/index}
       ^:interceptors [(body-params/body-params)
                       pedestal-util/auto-content-type
                       pedestal-util/combine-body-params]

       ["/questions" ~(mk-handlers)]


       ]]]))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment