Permalink
Browse files

Refactored get-user and user-attribute

Put the cond inside the where clause of the database fetch.  The one
thing I don't like about this is that if these functions are passed an
invalid object (such as a map without :user or :openid), then we will
have a situation where we do (fetch-one :users :where nil), which will
incorrectly return a result.  Perhaps we should add an :else to these
conds which looks for something we know not to be in the database.
  • Loading branch information...
1 parent 17c085b commit 52b217dc9dbf38a7c48f4371653760edb6e649e3 @chrismgray committed Dec 14, 2011
Showing with 21 additions and 25 deletions.
  1. +21 −25 src/foreclojure/utils.clj
View
@@ -132,15 +132,15 @@
(:openid user)))
(defn get-user [username]
- (cond
- (string? username)
- (from-mongo (fetch-one :users :where {:user username}))
- (map? username)
- (let [user (:user username)
- openid (:openid username)]
- (if user
- (from-mongo (fetch-one :users :where {:user user}))
- (from-mongo (fetch-one :users :where {:openid openid}))))))
+ (from-mongo
+ (fetch-one :users :where
+ (cond
+ (string? username)
+ {:user username}
+ (and (map? username) (:user username))
+ {:user (:user username)}
+ (and (map? username) (:openid username))
+ {:openid (:openid username)}))))
(defmacro if-user
"Look for a user with the given username in the database, let-ing it
@@ -175,22 +175,18 @@
(defn user-attribute [attr]
(fn [username]
- (cond
- (string? username)
- (attr (from-mongo
- (fetch-one :users
- :where {:user username}
- :only [attr])))
- (and (map? username) (:user username))
- (attr (from-mongo
- (fetch-one :users
- :where {:user (:user username)}
- :only [attr])))
- (and (map? username) (:openid username))
- (attr (from-mongo
- (fetch-one :users
- :where {:openid (:openid username)}
- :only [attr]))))))
+ (attr
+ (from-mongo
+ (fetch-one :users
+ :where
+ (cond
+ (string? username)
+ {:user username}
+ (and (map? username) (:user username))
+ {:user (:user username)}
+ (and (map? username) (:openid username))
+ {:openid (:openid username)})
+ :only [attr])))))
(def get-solved (comp set (user-attribute :solved)))
(def approver? (user-attribute :approver))

0 comments on commit 52b217d

Please sign in to comment.