Parcourir la source

return album count on category-query

Innocenty Enikeew il y a 7 ans
Parent
commit
7cd142ff6f
2 fichiers modifiés avec 31 ajouts et 11 suppressions
  1. 8 3
      back/db.lisp
  2. 23 8
      back/server.lisp

+ 8 - 3
back/db.lisp

@@ -214,16 +214,21 @@
              result
              (match-restrictions data restrictions)
              (match-filter data category filter))
-       do (setf (gethash result results) t))
+       do (incf (the fixnum (gethash result results 0))))
     (let* ((total (hash-table-count results))
            (start (min total offset))
            (end (min total (+ offset limit))))
       (if count-only total
-          (subseq (the list (sort (hash-table-keys results)
+          (subseq (the list (sort (case category
+                                    (album (hash-table-keys results))
+                                    (t (hash-table-alist results)))
                                   (case category
                                     (album #'album<)
                                     (year #'<)
-                                    (t #'string<))))
+                                    (t #'string<))
+                                  :key (case category
+                                         (album nil)
+                                         (t #'car))))
                   start end)))))
 
 (defun query-tracks (tracks-db &key filter restrictions limit (offset 0))

+ 23 - 8
back/server.lisp

@@ -57,10 +57,11 @@
 (defparameter +200-empty+ '(200 (:content-type "application/json")
                             #.(trivial-utf-8:string-to-utf-8-bytes "{}")))
 
-(defun 200-json (data)
-  (declare #.*standard-optimize-settings*)
+(defun 200-json (data &optional (dumper #'to-json))
+  (declare #.*standard-optimize-settings*
+           (type function dumper))
   `(200 (:content-type "application/json")
-        ,(trivial-utf-8:string-to-utf-8-bytes (to-json data))))
+        ,(trivial-utf-8:string-to-utf-8-bytes (funcall dumper data))))
 
 (defun get-category-list (params)
   (declare #.*standard-optimize-settings*
@@ -85,7 +86,7 @@
   (declare #.*standard-optimize-settings*
            (type (or null string) string))
   (when string
-    (parse-integer string)))
+    (parse-integer string :junk-allowed t)))
 
 (defun get-restrictions (query-params)
   (declare #.*standard-optimize-settings*
@@ -96,7 +97,7 @@
                                     (year (parse-integer value :junk-allowed t))
                                     (otherwise value)))))
 
-(defparameter +max-limit+ 1000)
+(defparameter +max-limit+ 100)
 (defmacro with-category ((params category filter restrictions offset limit) &body body)
   (with-gensyms (query-string query-params)
     `(let ((,category (getsym ,params :category)))
@@ -107,7 +108,8 @@
                  (,filter (aget ,query-params "filter"))
                  (,restrictions (get-restrictions ,query-params))
                  (,offset (or (may-integer (aget ,query-params "offset")) 0))
-                 (,limit (min (the fixnum +max-limit+) (the fixnum (or (may-integer (aget ,query-params "limit")) +max-limit+)))))
+                 (,limit (min (the fixnum +max-limit+)
+                              (the fixnum (or (may-integer (aget ,query-params "limit")) +max-limit+)))))
             ,@body))
          (otherwise +404+)))))
 
@@ -115,14 +117,27 @@
   (declare #.*standard-optimize-settings*)
   (with-category (params category filter restrictions offset limit)
     (declare (ignore offset limit))
-    (200-json (query-category (cdr *db*) category :filter filter :restrictions restrictions :count-only t))))
+    (200-json (query-category (cdr *db*) category
+                              :filter filter :restrictions restrictions
+                              :count-only t))))
+
+(defun dumps-category-result (results)
+  (with-output-to-string*
+    (with-array
+      (loop for (cat . count) in results
+         do (with-object
+              (write-key-value "item" cat)
+              (write-key-value "count" count))))))
 
 (defun get-category (params)
   (declare #.*standard-optimize-settings*)
   (with-category (params category filter restrictions offset limit)
     (200-json (query-category (cdr *db*) category
                               :filter filter :restrictions restrictions
-                              :limit limit :offset offset))))
+                              :limit limit :offset offset)
+              (case category
+                (album #'to-json)
+                (t #'dumps-category-result)))))
 
 (defun album-tracks (params)
   (declare #.*standard-optimize-settings*)