Created
July 28, 2011 10:30
-
-
Save jdoig/1111343 to your computer and use it in GitHub Desktop.
Very simple scooter hire example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns webapp.routes | |
(:use compojure.core | |
hiccup.core) | |
(:require [clj-http.client :as client] | |
[compojure.route :as route] | |
[clj-json.core :as json])) | |
;Make everything £40 for simplicity | |
(defn calculate-price [vehicle] | |
(assoc vehicle :price-per-day 40)) | |
;Remove CouchDB revision, if there is one | |
(defn remove-revision-number [document] | |
(dissoc document :_rev)) | |
;Hard-coded connection string, for brevity | |
(defn get-by-id [id] | |
(let [url (str "http://127.0.0.1:5984/vehicles/" id)] | |
(json/parse-string ((client/get url) :body) true))) | |
;Format hashmap as json reponse | |
(defn json-response [data & [status]] | |
{ :status (or status 200) | |
:headers {"Content-Type" "application/json"} | |
:body (json/generate-string data)}) | |
(defn get-vehicle-data [id] | |
(let [vehicle (get-by-id id)] | |
(-> vehicle | |
remove-revision-number | |
calculate-price | |
json-response))) | |
(defroutes main-routes | |
(GET "/scooter/:id" [id] (get-vehicle-data id)) | |
(route/not-found (html [:h3 "Resource not found"]))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment