Permalink
Browse files

Refactor code duplication in search method.

  • Loading branch information...
1 parent 096d164 commit 263e303272b8d9e5a56440b0644f02d0259b3ac7 @dakrone committed Aug 23, 2010
Showing with 19 additions and 25 deletions.
  1. +19 −25 src/cd_wsapi/core.clj
View
@@ -60,34 +60,28 @@
function)
-; TODO: reduce code duplication smell.
+(defn perform-search
+ "Given a searching query vector, return a function to perform the search."
+ [queryvec]
+ (fn [n]
+ {:status 200
+ :headers {"Content-Type" "application/json"}
+ :body (encode-to-str
+ (with-connection db
+ (transaction
+ (when-let [functions (with-query-results rs queryvec (doall rs))]
+ (map format-search functions)))))}))
+
+
(defn search
+ "Search for a method by name or namespace & name."
([name]
- (fn [n]
- {:status 200
- :headers {"Content-Type" "application/json"}
- :body (encode-to-str
- (with-connection db
- (transaction
- (when-let [functions (with-query-results
- rs
- ; There is definitely a better way to do this.
- [(str "select id,name,ns from functions where name like '%" name "%'")]
- (doall rs))]
- (map format-search functions)))))}))
+ (search nil name))
([ns name]
- (fn [n]
- {:status 200
- :headers {"Content-Type" "application/json"}
- :body (encode-to-str
- (with-connection db
- (transaction
- (when-let [functions (with-query-results
- rs
- ; There is definitely a better way to do this.
- [(str "select id,name,ns from functions where ns = ? and name like '%" name "%'") ns]
- (doall rs))]
- (map format-search functions)))))})))
+ (let [qv (if (nil? ns)
+ [(str "select id,name,ns from functions where name like '%" name "%'")]
+ [(str "select id,name,ns from functions where ns = ? and name like '%" name "%'") ns])]
+ (perform-search qv))))
(defn app-handler [channel request]

0 comments on commit 263e303

Please sign in to comment.