send responses as json

This commit is contained in:
Bart Akeley 2017-10-20 23:20:18 -05:00
parent a5ee819324
commit 67b61ba65a
2 changed files with 28 additions and 6 deletions

View file

@ -8,7 +8,8 @@
[org.clojure/java.jdbc "0.7.3"]
[org.postgresql/postgresql "42.1.4.jre6"]
[com.mchange/c3p0 "0.9.5.2"]
[ring-middleware-format "0.7.2"]]
[ring-middleware-format "0.7.2"]
[org.clojure/data.json "0.2.6"]]
:plugins [[lein-ring "0.9.7"]]
:ring {:handler aretherecookies-server.handler/app}
:profiles

View file

@ -2,7 +2,8 @@
(:require [compojure.core :refer :all]
[compojure.route :as route]
[ring.middleware.defaults :refer [wrap-defaults site-defaults]]
[clojure.java.jdbc :as jdbc])
[clojure.java.jdbc :as jdbc]
[clojure.data.json :as json])
(:import com.mchange.v2.c3p0.ComboPooledDataSource))
(def db-spec {:dbtype "postgresql"
@ -51,12 +52,32 @@
(jdbc/query @pooled-db
[location-query])
(defn food-items-handler [request]
(jdbc/query @pooled-db
[location-query]))
(defn uuid-to-string [key value]
(if (instance? java.util.UUID value) (.toString value) value))
(defn get-coords [item]
(get (json/read-str (get item :location)) "coordinates"))
(defn build-lat-lng [[lng lat]]
(hash-map :longitude lng :latitude lat))
(defn parse-location [item]
(dissoc (merge item (build-lat-lng (get-coords item))) :location))
(defn food-items-to-json [response]
(map parse-location response))
(defn food-items-handler [location orderby filter]
(json/write-str
(hash-map
:orderby orderby
:filter filter
:fooditems (food-items-to-json (jdbc/query @pooled-db [location-query])))
:value-fn uuid-to-string))
(defroutes app-routes
(GET "/fooditems" request (food-items-handler request)))
(GET "/fooditems" [location orderby filter] (food-items-handler location orderby filter))
(GET "/echo" [location orderby filter] (str location orderby filter)))
(def app
(wrap-defaults app-routes site-defaults))