Browse files

add sorting

  • Loading branch information...
1 parent 4007273 commit c573972740b7384b86b347ac4061639b2465a323 @gigasquid gigasquid committed Oct 5, 2011
Showing with 65 additions and 25 deletions.
  1. +31 −8 src/foreclojure/users.clj
  2. +34 −17 test/foreclojure/test/users.clj
View
39 src/foreclojure/users.clj
@@ -262,18 +262,41 @@
{:$set {:hide-solutions (boolean hide-flag)}})
(response/redirect "/problems")))
-(defn datatable-paging [start length coll]
- (take length (drop start coll)))
+(defn datatable-paging [users start length]
+ (take length (drop start users)))
-(defn user-datatable-query [params]
+(defn datatable-sort-cols [users sort-col]
+ (case sort-col
+ 0 (sort-by :rank users)
+ 1 (sort-by :user users)
+ 2 (sort-by (comp :solved count) users)
+ users))
+
+(defn datatable-sort-dir [users sort-dir]
+ (if (= sort-dir "asc")
+ users
+ (reverse users)))
+
+(defn datatable-sort [users sort-col sort-dir]
+ (-> users (datatable-sort-cols sort-col) (datatable-sort-dir sort-dir)))
+
+(defn datatable-process [users params]
(let [display-start (Integer. (params :iDisplayStart))
display-length (Integer. (params :iDisplayLength))
+ sort-col (Integer. (params :iSortCol_0))
+ sort-dir (params :sSortDir_0)]
+ (-> users
+ (datatable-sort sort-col sort-dir)
+ (datatable-paging display-start display-length))))
+
+(defn user-datatable-query [params]
+ (let [
ranked-users (get-ranked-users)
- page-users (datatable-paging display-start
- display-length
- (generate-datatable-users-list ranked-users))]
- (println params)
- (println page-users)
+ page-users (datatable-process
+ (generate-datatable-users-list ranked-users)
+ params)]
+ (println params)
+ (println page-users)
{:sEcho (params :sEcho)
:iTotalRecords (str (count ranked-users))
:iTotalDisplayRecords (str (count ranked-users))
View
51 test/foreclojure/test/users.clj
@@ -3,24 +3,41 @@
(:use [clojure.test])
(:use [midje.sweet]))
+(def users [{:user "user1", :solved [1 2 3 4] :rank 1}
+ {:user "user2", :solved [1 2 3] :rank 2}
+ {:user "user3", :sovled [1 2] :rank 3}
+ {:user "user4", :solved [1] :rank 4}
+ {:user "user5", :solved [1] :rank 5}
+ {:user "user6", :solved [1] :rank 6}
+ {:user "user7", :solved [1] :rank 7}
+ {:user "user8", :solved [1] :rank 8}
+ {:user "user9", :solved [1] :rank 9}
+ {:user "user10", :solved [1] :rank 10}
+ ])
+
(deftest user-datatables-paging
- (def users [{:user "user1", :solved [1 2 3 4]}
- {:user "user2", :solved [1 2 3]}
- {:user "user3", :sovled [1 2]}
- {:user "user4", :solved [1]}
- {:user "user5", :solved [1]}
- {:user "user6", :solved [1]}
- {:user "user7", :solved [1]}
- {:user "user8", :solved [1]}
- {:user "user9", :solved [1]}
- {:user "user10", :solved [1]}
- ])
(facts "about datatable-paging"
- (count (datatable-paging 0 10 users)) => 10
- (count (datatable-paging 8 10 users)) => 2
- (count (datatable-paging 2 5 users))) => 5
- (:user (first (datatable-paging 2 5 users))) => "user3"
- (:user (last (datatable-paging 2 5 users))) => "user7"
- )
+ (count (datatable-paging users 0 10)) => 10
+ (count (datatable-paging users 8 10)) => 2
+ (count (datatable-paging users 2 5))) => 5
+ (:user (first (datatable-paging users 2 5))) => "user3"
+ (:user (last (datatable-paging users 2 5))) => "user7")
+
+(deftest user-datatables-sort-cols
+ (facts "about datatable sorting by columns"
+ (:user (first (datatable-sort-cols users 0))) => "user1"
+ (:user (second (datatable-sort-cols users 1))) => "user10"
+ (:user (first (datatable-sort-cols users 2))) => "user1"
+ (:user (first (datatable-sort-cols users 3))) => "user1"))
+
+(deftest user-datatables-sort-dir
+ (facts "about datatable sort direction"
+ (:user (first (datatable-sort-dir users "asc"))) => "user1"
+ (:user (first (datatable-sort-dir users "desc"))) => "user10"))
+
+(deftest user-datatables-sort
+ (facts "about sorting by column and direction combined"
+ (:user (first (datatable-sort users 1 "asc"))) => "user1"
+ (:user (first (datatable-sort users 1 "desc"))) => "user9"))

0 comments on commit c573972

Please sign in to comment.