Răsfoiți Sursa

add reverse option

Masanori Matsuura 8 ani în urmă
părinte
comite
17b511cf3c
1 a modificat fișierele cu 25 adăugiri și 0 ștergeri
  1. 25 0
      packages/growi-plugin-lsx/src/lib/routes/lsx.js

+ 25 - 0
packages/growi-plugin-lsx/src/lib/routes/lsx.js

@@ -59,6 +59,27 @@ class Lsx {
     return query.skip(skip).limit(limit);
     return query.skip(skip).limit(limit);
   }
   }
 
 
+  /**
+   * add reverse condition that sort pages
+   *
+   * @static
+   * @param {any} query
+   * @param {any} pagePath
+   * @param {any} optionsReverse
+   * @returns
+   *
+   * @memberOf Lsx
+   */
+  static addReverseCondition(query, pagePath, optionsReverse){
+    if (optionsReverse !== 'true' && optionsReverse !== 'false') {
+      throw new Error(`specified reverse is [' + ${optionsReverse} + '] : reverse are must be true or false`);
+    }
+    if(optionsReverse === 'true'){
+      return query.sort({path: -1});
+    }else{
+      return query.sort({path: 1});
+    }
+  }
 }
 }
 
 
 module.exports = (crowi, app) => {
 module.exports = (crowi, app) => {
@@ -90,6 +111,10 @@ module.exports = (crowi, app) => {
       if (options.num !== undefined) {
       if (options.num !== undefined) {
         query = Lsx.addNumCondition(query, pagePath, options.num);
         query = Lsx.addNumCondition(query, pagePath, options.num);
       }
       }
+      // reverse
+      if (options.reverse !== undefined) {
+        query = Lsx.addReverseCondition(query, pagePath, options.reverse);
+      }
     }
     }
     catch (error) {
     catch (error) {
       return res.json(ApiResponse.error(error.message));
       return res.json(ApiResponse.error(error.message));