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]))

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

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

(when (not-any? #{id} (get-solved user))
(update! :users {:user user} {:$push {:solved id}})
(update! :problems {:_id id} {:$inc {:times-solved 1}}))
(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")))

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

(def sb (sandbox*))

Expand All @@ -41,16 +40,16 @@
func-name (:function-name p)
sb-tester (get-tester (:restricted p))]
(try
(loop [t tests]
(if (empty? t)
(loop [[test & more] tests]
(if-not test
(mark-completed id)
(let [testcase (replace-str "__" code (first t))]
(let [testcase (replace-str "__" code test)]
(if (sb sb-tester (read-string testcase))
(recur (rest t))
(recur more)
(do
(session/flash-put! :code code)
(flash-error "You failed the unit tests."
(str "/problem/" id)))))))
(str "/problem/" id)))))))
(catch Exception e
(do
(session/flash-put! :code code)
Expand Down Expand Up @@ -90,15 +89,15 @@
(let [solved (get-solved (session/session-get :user))]
(map-indexed
(fn [x p]
(vec [:tr (row-class x)
[:td {:class "title-link"}
[:a {:href (str "/problem/" (p :_id))}
(p :title)]]
[:td {:class "centered"}
(map #(str % " ") (p :tags))]
[:td {:class "centered"} (p :times-solved)]
[:td {:class "centered"}
(if (contains? solved (p :_id))
[:img {:src "/checkmark.png"}]
[:img {:src "/empty-sq.png"}])]]))
(get-problem-list)))])
[:tr (row-class x)
[:td {:class "title-link"}
[:a {:href (str "/problem/" (p :_id))}
(p :title)]]
[:td {:class "centered"}
(map #(str % " ") (p :tags))]
[:td {:class "centered"} (p :times-solved)]
[:td {:class "centered"}
[:img {:src (if (contains? solved (p :_id))
"/checkmark.png"
"/empty-sq.png")}]]])
(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]
(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]
(do (session/flash-put! :error msg)
(response/redirect url)))
(defn flash-fn [type]
(fn [msg url]
(session/flash-put! type msg)
(response/redirect url)))

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

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

0 comments on commit a4488dd

Please sign in to comment.