diff options
Diffstat (limited to 'server/models')
-rw-r--r-- | server/models/videos.js | 42 |
1 files changed, 24 insertions, 18 deletions
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 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const async = require('async') | ||
3 | const config = require('config') | 4 | const config = require('config') |
4 | const mongoose = require('mongoose') | 5 | const mongoose = require('mongoose') |
5 | 6 | ||
@@ -81,15 +82,8 @@ function get (id, callback) { | |||
81 | } | 82 | } |
82 | 83 | ||
83 | function list (start, count, sort, callback) { | 84 | function list (start, count, sort, callback) { |
84 | VideosDB.find({}).skip(start).limit(start + count).sort(sort) | 85 | const query = {} |
85 | .exec(function (err, videosList) { | 86 | return findWithCount(query, start, count, sort, callback) |
86 | if (err) { | ||
87 | logger.error('Cannot get the list of the videos.') | ||
88 | return callback(err) | ||
89 | } | ||
90 | |||
91 | return callback(null, videosList) | ||
92 | }) | ||
93 | } | 87 | } |
94 | 88 | ||
95 | function listFromUrl (fromUrl, callback) { | 89 | function listFromUrl (fromUrl, callback) { |
@@ -131,17 +125,29 @@ function removeByIds (ids, callback) { | |||
131 | } | 125 | } |
132 | 126 | ||
133 | function search (name, start, count, sort, callback) { | 127 | function search (name, start, count, sort, callback) { |
134 | VideosDB.find({ name: new RegExp(name) }).skip(start).limit(start + count).sort(sort) | 128 | const query = { name: new RegExp(name) } |
135 | .exec(function (err, videos) { | 129 | findWithCount(query, start, count, sort, callback) |
136 | if (err) { | ||
137 | logger.error('Cannot search the videos.') | ||
138 | return callback(err) | ||
139 | } | ||
140 | |||
141 | return callback(null, videos) | ||
142 | }) | ||
143 | } | 130 | } |
144 | 131 | ||
145 | // --------------------------------------------------------------------------- | 132 | // --------------------------------------------------------------------------- |
146 | 133 | ||
147 | module.exports = Videos | 134 | module.exports = Videos |
135 | |||
136 | // --------------------------------------------------------------------------- | ||
137 | |||
138 | function findWithCount (query, start, count, sort, callback) { | ||
139 | async.parallel([ | ||
140 | function (asyncCallback) { | ||
141 | VideosDB.find(query).skip(start).limit(start + count).sort(sort).exec(asyncCallback) | ||
142 | }, | ||
143 | function (asyncCallback) { | ||
144 | VideosDB.count(query, asyncCallback) | ||
145 | } | ||
146 | ], function (err, results) { | ||
147 | if (err) return callback(err) | ||
148 | |||
149 | const videos = results[0] | ||
150 | const totalVideos = results[1] | ||
151 | return callback(null, videos, totalVideos) | ||
152 | }) | ||
153 | } | ||