Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/amalloy/4clojure into ama…
Browse files Browse the repository at this point in the history
…lloy-master
  • Loading branch information
dbyrne committed Apr 12, 2011
2 parents eb4d705 + 6099496 commit a4488dd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
47 changes: 23 additions & 24 deletions src/foreclojure/problems.clj
Expand Up @@ -7,10 +7,10 @@
(:require [sandbar.stateful-session :as session])) (:require [sandbar.stateful-session :as session]))


(defn get-solved [user] (defn get-solved [user]
(into #{} (set
(:solved (fetch-one :users (:solved (fetch-one :users
:where {:user user} :where {:user user}
:only [:solved])))) :only [:solved]))))


(defn get-problem [x] (defn get-problem [x]
(fetch-one :problems :where {:_id x})) (fetch-one :problems :where {:_id x}))
Expand All @@ -23,15 +23,14 @@
(defn mark-completed [id] (defn mark-completed [id]
(if-let [user (session/session-get :user)] (if-let [user (session/session-get :user)]
(do (do
(when (not-any? #(= % id) (get-solved user)) (when (not-any? #{id} (get-solved user))

(update! :users {:user user} {:$push {:solved id}}) (update! :users {:user user} {:$push {:solved id}})
(update! :problems {:_id id} {:$inc {:times-solved 1}})) (update! :problems {:_id id} {:$inc {:times-solved 1}}))
(flash-msg "Congratulations, you've solved the problem!" "/problems")) (flash-msg "Congratulations, you've solved the problem!" "/problems"))
(flash-msg "You've solved the problem! If you log in we can track your progress." "/problems"))) (flash-msg "You've solved the problem! If you log in we can track your progress." "/problems")))


(defn get-tester [restricted] (defn get-tester [restricted]
(reduce #(conj %1 (symbol %2)) secure-tester restricted)) (into secure-tester (map symbol restricted)))


(def sb (sandbox*)) (def sb (sandbox*))


Expand All @@ -41,16 +40,16 @@
func-name (:function-name p) func-name (:function-name p)
sb-tester (get-tester (:restricted p))] sb-tester (get-tester (:restricted p))]
(try (try
(loop [t tests] (loop [[test & more] tests]
(if (empty? t) (if-not test
(mark-completed id) (mark-completed id)
(let [testcase (replace-str "__" code (first t))] (let [testcase (replace-str "__" code test)]
(if (sb sb-tester (read-string testcase)) (if (sb sb-tester (read-string testcase))
(recur (rest t)) (recur more)
(do (do
(session/flash-put! :code code) (session/flash-put! :code code)
(flash-error "You failed the unit tests." (flash-error "You failed the unit tests."
(str "/problem/" id))))))) (str "/problem/" id)))))))
(catch Exception e (catch Exception e
(do (do
(session/flash-put! :code code) (session/flash-put! :code code)
Expand Down Expand Up @@ -90,15 +89,15 @@
(let [solved (get-solved (session/session-get :user))] (let [solved (get-solved (session/session-get :user))]
(map-indexed (map-indexed
(fn [x p] (fn [x p]
(vec [:tr (row-class x) [:tr (row-class x)
[:td {:class "title-link"} [:td {:class "title-link"}
[:a {:href (str "/problem/" (p :_id))} [:a {:href (str "/problem/" (p :_id))}
(p :title)]] (p :title)]]
[:td {:class "centered"} [:td {:class "centered"}
(map #(str % " ") (p :tags))] (map #(str % " ") (p :tags))]
[:td {:class "centered"} (p :times-solved)] [:td {:class "centered"} (p :times-solved)]
[:td {:class "centered"} [:td {:class "centered"}
(if (contains? solved (p :_id)) [:img {:src (if (contains? solved (p :_id))
[:img {:src "/checkmark.png"}] "/checkmark.png"
[:img {:src "/empty-sq.png"}])]])) "/empty-sq.png")}]]])
(get-problem-list)))]) (get-problem-list)))])
17 changes: 9 additions & 8 deletions src/foreclojure/utils.clj
Expand Up @@ -4,15 +4,16 @@
(:require [sandbar.stateful-session :as session] (:require [sandbar.stateful-session :as session]
(ring.util [response :as response]))) (ring.util [response :as response])))


(defmacro dbg[x] `(let [x# ~x] (println '~x "=" x#) x#)) (defmacro dbg [x]
`(let [x# ~x] (println '~x "=" x#) x#))


(defn flash-error [msg url] (defn flash-fn [type]
(do (session/flash-put! :error msg) (fn [msg url]
(response/redirect url))) (session/flash-put! type msg)
(response/redirect url)))


(defn flash-msg [msg url] (def flash-error (flash-fn :error))
(do (session/flash-put! :message msg) (def flash-msg (flash-fn :message))
(response/redirect url)))


(defmacro def-page [page-name [& args] & code] (defmacro def-page [page-name [& args] & code]
`(defn ~page-name [~@args] `(defn ~page-name [~@args]
Expand Down Expand Up @@ -53,4 +54,4 @@
[:img {:src "/PoweredMongoDBbeige50.png"}]]] [:img {:src "/PoweredMongoDBbeige50.png"}]]]
[:div {:id "content"} body] [:div {:id "content"} body]
[:footer [:footer
[:span {:id "footer"} "© 2011 David Byrne" ]]]])) [:span {:id "footer"} "© 2011 David Byrne" ]]]]))

0 comments on commit a4488dd

Please sign in to comment.