Skip to content

Instantly share code, notes, and snippets.

@oubiwann
Last active August 29, 2015 14:01

Revisions

  1. oubiwann revised this gist May 18, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 02-clojure-routes.clj
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@
    (defroutes handler
    ;; top-level
    (GET "/" []
    (html-response (make-front-page)))
    "Welcome to the Volvo Store!")
    ;; single order operations
    (POST "/order" [attrs]
    (json-response (create-order attrs)))
  2. oubiwann revised this gist May 18, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion 02-clojure-routes.clj
    Original file line number Diff line number Diff line change
    @@ -26,4 +26,5 @@
    ; ('ALLOWONLY
    ; ('GET 'POST 'PUT 'DELETE)
    ; (lfest-json-resp:method-not-allowed))
    (route/not-found "Bad path: invalid operation."))
    (route/not-found
    "Bad path: invalid operation."))
  3. oubiwann revised this gist May 18, 2014. 2 changed files with 42 additions and 8 deletions.
    29 changes: 29 additions & 0 deletions 02-clojure-routes.clj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    (ns volvo-store
    (:use compojure.core)
    (:require [compojure.route :as route]))

    (defroutes handler
    ;; top-level
    (GET "/" []
    (html-response (make-front-page)))
    ;; single order operations
    (POST "/order" [attrs]
    (json-response (create-order attrs)))
    (GET "/order/:id" [id]
    (json-response (get-order id)))
    (DELETE "/order/:id" [id]
    (json-response (delete-order id)))
    ;; order collection operations
    (GET "/orders" []
    (json-response (get-orders)))
    ;; payment operations
    (GET "/payment/order/:id" [id]
    (get-payment-status id))
    (PUT "/payment/order/:id" [id attrs]
    (make-payment id attrs))
    ;; error conditions
    ; XXX not sure how you'd do this in Compojure ...
    ; ('ALLOWONLY
    ; ('GET 'POST 'PUT 'DELETE)
    ; (lfest-json-resp:method-not-allowed))
    (route/not-found "Bad path: invalid operation."))
    21 changes: 13 additions & 8 deletions 02-macro-routes.lfe → 03-macro-routes.lfe
    Original file line number Diff line number Diff line change
    @@ -1,24 +1,29 @@
    (defmodule volvo-store
    (export all))

    (include-lib "deps/lfest/include/macros.lfe")

    (defroutes
    ;; top-level
    ('GET "/"
    (lfest-html-resp:ok "Welcome to the Volvo Store!"))
    (lfest-html-resp:ok "Welcome to the Volvo Store!"))
    ;; single order operations
    ('POST "/order"
    (create-order (lfest:get-data arg-data)))
    (create-order (lfest:get-data arg-data)))
    ('GET "/order/:id"
    (get-order id))
    (get-order id))
    ('PUT "/order/:id"
    (update-order id (lfest:get-data arg-data)))
    (update-order id (lfest:get-data arg-data)))
    ('DELETE "/order/:id"
    (delete-order id))
    (delete-order id))
    ;; order collection operations
    ('GET "/orders"
    (get-orders))
    (get-orders))
    ;; payment operations
    ('GET "/payment/order/:id"
    (get-payment-status id))
    (get-payment-status id))
    ('PUT "/payment/order/:id"
    (make-payment id (lfest:get-data arg-data)))
    (make-payment id (lfest:get-data arg-data)))
    ;; error conditions
    ('ALLOWONLY
    ('GET 'POST 'PUT 'DELETE)
  4. oubiwann created this gist May 18, 2014.
    20 changes: 20 additions & 0 deletions 01-manual-routes.lfe
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    (defun routes
    "Routes for the Volvoshop REST API."
    ;; /order
    (((list "order") method arg-data)
    (order-api method arg-data))
    ;; /order/:id
    (((list "order" order-id) method arg-data)
    (order-api method order-id arg-data))
    ;; /orders
    (((list "orders") method arg-data)
    (orders-api method arg-data))
    ;; /payment/order/:id
    (((list "payment" "order" order-id) method arg-data)
    (payment-api method order-id arg-data))
    ;; When nothing matches, do this
    ((path method arg)
    (io:format
    "Unmatched route!~nPath-info: ~p~nmethod: ~p~narg-data: ~p~n~n"
    (list path method arg))
    (lfest-json-resp:not-found "Unmatched route.")))
    27 changes: 27 additions & 0 deletions 02-macro-routes.lfe
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    (defroutes
    ;; top-level
    ('GET "/"
    (lfest-html-resp:ok "Welcome to the Volvo Store!"))
    ;; single order operations
    ('POST "/order"
    (create-order (lfest:get-data arg-data)))
    ('GET "/order/:id"
    (get-order id))
    ('PUT "/order/:id"
    (update-order id (lfest:get-data arg-data)))
    ('DELETE "/order/:id"
    (delete-order id))
    ;; order collection operations
    ('GET "/orders"
    (get-orders))
    ;; payment operations
    ('GET "/payment/order/:id"
    (get-payment-status id))
    ('PUT "/payment/order/:id"
    (make-payment id (lfest:get-data arg-data)))
    ;; error conditions
    ('ALLOWONLY
    ('GET 'POST 'PUT 'DELETE)
    (lfest-json-resp:method-not-allowed))
    ('NOTFOUND
    (lfest-json-resp:not-found "Bad path: invalid operation.")))