1
0
Переглянути джерело

[back] 'Latest' album sorting

root 5 роки тому
батько
коміт
d6c078ae50
3 змінених файлів з 30 додано та 13 видалено
  1. 1 0
      back/chad-music.asd
  2. 22 7
      back/db.lisp
  3. 7 6
      back/server.lisp

+ 1 - 0
back/chad-music.asd

@@ -24,4 +24,5 @@
   :serial t
   :components ((:file "utils")
                (:file "db")
+	       (:file "rym")
                (:file "server")))

+ 22 - 7
back/db.lisp

@@ -21,7 +21,8 @@
 (defstruct db
   (albums (make-hash-table :test 'equal) :type hash-table)
   (tracks (make-hash-table :test 'equal) :type hash-table)
-  (album-tracks (make-hash-table :test 'equal) :type hash-table))
+  (album-tracks (make-hash-table :test 'equal) :type hash-table)
+  (album-added (make-hash-table :test 'equal) :type hash-table))
 
 (defvar *db* (make-db) "Metadata database")
 (defvar *db-path* "music.sexp" "Default file to save/load database")
@@ -159,11 +160,14 @@
   (declare #.*standard-optimize-settings*)
 
   (let ((tracks-db (db-tracks *db*))
-        (album-tracks-db (db-album-tracks *db*)))
+        (album-tracks-db (db-album-tracks *db*))
+	(album-added-db (db-album-added *db*)))
     (clrhash album-tracks-db)
+    (clrhash album-added-db)
     (loop for entry being the hash-values in tracks-db
        for track = (entry-track entry)
        when track
+       do (setf (gethash (track-album-id track) album-added-db) (entry-added entry))
        do (push track (gethash (track-album-id track) album-tracks-db)))
     (loop for album-id being the hash-keys in album-tracks-db using (hash-value tracks)
        do (setf (gethash album-id album-tracks-db)
@@ -205,13 +209,24 @@
                                 (t (if (string< slot-a slot-b) -1 1)))))))))
 (defparameter +album<>+ (gen-comparator '(artist type original-date year album)))
 (defun album< (a b)
-  (< (funcall +album<>+ a b) 0))
+  (declare #.*standard-optimize-settings*
+	   (type function +album<>+))
+  (< (the fixnum (funcall +album<>+ a b)) 0))
+
+(defun album-added> (a b)
+  (declare #.*standard-optimize-settings*
+	   (type album a b))
+  (let ((album-added-db (db-album-added *db*)))
+    (> (the fixnum (gethash (album-id a) album-added-db))
+       (the fixnum (gethash (album-id b) album-added-db)))))
 
 (defparameter +track<>+ (gen-comparator '(no title)))
 (defun track< (a b)
-  (let ((albs (funcall +album<>+ (track-album a) (track-album b))))
+  (declare #.*standard-optimize-settings*
+	   (type function +album<>+ +track<>+))
+  (let ((albs (the fixnum (funcall +album<>+ (track-album a) (track-album b)))))
     (if (zerop albs)
-        (< (funcall +track<>+ a b) 0)
+	(< (the fixnum (funcall +track<>+ a b)) 0)
         albs)))
 
 (defun match-filter (data category filter)
@@ -243,7 +258,7 @@
              (equal (slot-value data (car r)) (cdr r)))
          restrictions))
 
-(defun query-category (category &key filter restrictions limit (offset 0) count-only)
+(defun query-category (category &key filter restrictions limit (offset 0) count-only latest)
   (declare #.*standard-optimize-settings*
            (type (or null fixnum) limit offset))
   (let ((albums-db (db-albums *db*))
@@ -267,7 +282,7 @@
                                     (album (hash-table-keys results))
                                     (t (hash-table-alist results)))
                                   (case category
-                                    (album #'album<)
+                                    (album (if latest #'album-added> #'album<))
                                     (year #'<)
                                     (t #'string<))
                                   :key (case category

+ 7 - 6
back/server.lisp

@@ -97,7 +97,7 @@
                                     (otherwise value)))))
 
 (defparameter +max-limit+ 100)
-(defmacro with-category ((params category filter restrictions offset limit) &body body)
+(defmacro with-category ((params category filter restrictions offset limit latest) &body body)
   (with-gensyms (query-string query-params)
     `(let ((,category (getsym ,params :category)))
        (case ,category
@@ -108,14 +108,15 @@
                  (,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+)))))
+                              (the fixnum (or (may-integer (aget ,query-params "limit")) +max-limit+))))
+		 (,latest (aget ,query-params "latest")))
             ,@body))
          (otherwise +404+)))))
 
 (defun get-category-size (params)
   (declare #.*standard-optimize-settings*)
-  (with-category (params category filter restrictions offset limit)
-    (declare (ignore offset limit))
+  (with-category (params category filter restrictions offset limit latest)
+    (declare (ignore offset limit latest))
     (200-json (query-category category
                               :filter filter :restrictions restrictions
                               :count-only t))))
@@ -131,10 +132,10 @@
 
 (defun get-category (params)
   (declare #.*standard-optimize-settings*)
-  (with-category (params category filter restrictions offset limit)
+  (with-category (params category filter restrictions offset limit latest)
     (200-json (query-category category
                               :filter filter :restrictions restrictions
-                              :limit limit :offset offset)
+                              :limit limit :offset offset :latest latest)
               (case category
                 (album #'to-json)
                 (t #'dumps-category-result)))))