Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add results namespace.

  • Loading branch information...
commit 1a5a8e5535461b9ef3225c8117b8db46a47b1be7 1 parent 001dfff
@technomancy authored
Showing with 42 additions and 14 deletions.
  1. +28 −0 src/lein_survey/results.clj
  2. +14 −14 src/lein_survey/web.clj
View
28 src/lein_survey/results.clj
@@ -0,0 +1,28 @@
+(ns lein-survey.results
+ (:require [clojure.java.jdbc :as sql]
+ [clojure.pprint :as pprint]
+ [lein-survey.questions :as q]))
+
+(defn setize [x]
+ (if (coll? x)
+ (set x)
+ (hash-set x)))
+
+(defn merge-results [{:keys [id body timestamp]}]
+ (assoc (read-string body) :id id :timestamp (.getTime timestamp)))
+
+(defn results-str []
+ (sql/with-connection (or (System/getenv "DATABASE_URL")
+ "postgres://localhost:5432/lein-survey")
+ (sql/with-query-results results ["select * from answers"]
+ (doall (map (comp pr-str merge-results) results)))))
+
+(def results-url (java.net.URL. "http://lein-survey.herokuapp.com/results.clj"))
+
+(defonce get-results
+ (memoize (fn [] (read-string (slurp results-url)))))
+
+(defn total []
+ (clojure.java.jdbc/with-connection (System/getenv "DATABASE_URL")
+ (clojure.java.jdbc/with-query-results res ["select count(*) from answers"]
+ (println res))))
View
28 src/lein_survey/web.clj
@@ -1,6 +1,7 @@
(ns lein-survey.web
(:require [lein-survey.render :as render]
[lein-survey.questions :as q]
+ [lein-survey.results :as results]
[ring.adapter.jetty :as jetty]
[clojure.java.jdbc :as sql]
[ring.middleware.params :as params]
@@ -22,24 +23,23 @@
(render/layout [:h1 "Thank you!"]))
(defn handler [req]
- (if (= :post (:request-method req))
- {:status 201
- :headers {"Content-type" "text/html"}
- :body (record (:params req))}
- {:status 200
- :headers {"Content-type" "text/html"}
- :body (render/layout (render/questions-form q/questions))}))
+ (cond (= :post (:request-method req))
+ {:status 201
+ :headers {"Content-type" "text/html"}
+ :body (record (:params req))}
+ (= "/results.clj" (:uri req))
+ {:status 200
+ :headers {"Content-type" "application/x-clojure"}
+ :body (results/results-str)}
+ :else
+ {:status 200
+ :headers {"Content-type" "text/html"}
+ :body (render/layout (render/questions-form q/questions))}))
(def app (-> handler
params/wrap-params
(resource/wrap-resource "/public")))
(defn -main []
- (let [port (Integer. (or (System/getenv "PORT") 5000))]
+ (let [port (Integer. (or (System/getenv "PORT") 5005))]
(jetty/run-jetty #'app {:port port})))
-
-(defn total []
- (clojure.java.jdbc/with-connection (or (System/getenv "DATABASE_URL")
- "postgres://localhost:5432/lein-survey")
- (clojure.java.jdbc/with-query-results res ["select count(*) from answers"]
- (println res))))
Please sign in to comment.
Something went wrong with that request. Please try again.