X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Futils.js;h=49636b3d85e62cc6dcaf2aa90532d44dd06868b1;hb=d5f345ed4cfac4e1fa84dcb4fce1cda4d32f9c73;hp=a961e8c5ba4b07fd47f1a19fa409f00e8c038975;hpb=5c39adb7313e0696aabb4b71196ab7b0b378c359;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/utils.js b/server/models/utils.js index a961e8c5b..49636b3d8 100644 --- a/server/models/utils.js +++ b/server/models/utils.js @@ -1,28 +1,23 @@ 'use strict' -const parallel = require('async/parallel') - const utils = { - listForApiWithCount: listForApiWithCount + getSort } -function listForApiWithCount (query, start, count, sort, callback) { - const self = this +// Translate for example "-name" to [ 'name', 'DESC' ] +function getSort (value) { + let field + let direction - parallel([ - function (asyncCallback) { - self.find(query).skip(start).limit(count).sort(sort).exec(asyncCallback) - }, - function (asyncCallback) { - self.count(query, asyncCallback) - } - ], function (err, results) { - if (err) return callback(err) + if (value.substring(0, 1) === '-') { + direction = 'DESC' + field = value.substring(1) + } else { + direction = 'ASC' + field = value + } - const data = results[0] - const total = results[1] - return callback(null, data, total) - }) + return [ field, direction ] } // ---------------------------------------------------------------------------