From 68ce3ae021c9bc11b155044df6d23ba60e91eee4 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sat, 21 May 2016 19:30:22 +0200 Subject: Add total results field and wrap videos in data field when listing videos --- server/models/videos.js | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) (limited to 'server/models') diff --git a/server/models/videos.js b/server/models/videos.js index 9cac8edda..9521e63e3 100644 --- a/server/models/videos.js +++ b/server/models/videos.js @@ -1,5 +1,6 @@ 'use strict' +const async = require('async') const config = require('config') const mongoose = require('mongoose') @@ -81,15 +82,8 @@ function get (id, callback) { } function list (start, count, sort, callback) { - VideosDB.find({}).skip(start).limit(start + count).sort(sort) - .exec(function (err, videosList) { - if (err) { - logger.error('Cannot get the list of the videos.') - return callback(err) - } - - return callback(null, videosList) - }) + const query = {} + return findWithCount(query, start, count, sort, callback) } function listFromUrl (fromUrl, callback) { @@ -131,17 +125,29 @@ function removeByIds (ids, callback) { } function search (name, start, count, sort, callback) { - VideosDB.find({ name: new RegExp(name) }).skip(start).limit(start + count).sort(sort) - .exec(function (err, videos) { - if (err) { - logger.error('Cannot search the videos.') - return callback(err) - } - - return callback(null, videos) - }) + const query = { name: new RegExp(name) } + findWithCount(query, start, count, sort, callback) } // --------------------------------------------------------------------------- module.exports = Videos + +// --------------------------------------------------------------------------- + +function findWithCount (query, start, count, sort, callback) { + async.parallel([ + function (asyncCallback) { + VideosDB.find(query).skip(start).limit(start + count).sort(sort).exec(asyncCallback) + }, + function (asyncCallback) { + VideosDB.count(query, asyncCallback) + } + ], function (err, results) { + if (err) return callback(err) + + const videos = results[0] + const totalVideos = results[1] + return callback(null, videos, totalVideos) + }) +} -- cgit v1.2.3