From 0ff21c1c086f907612e34880f76f08bc74eeb78c Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 16 Aug 2016 21:51:04 +0200 Subject: [PATCH] Server: video.list -> video.listForApi (with pagination, sort...) --- server/controllers/api/v1/videos.js | 2 +- server/models/video.js | 28 +++++----------------------- 2 files changed, 6 insertions(+), 24 deletions(-) diff --git a/server/controllers/api/v1/videos.js b/server/controllers/api/v1/videos.js index 1f939b077..0a441f146 100644 --- a/server/controllers/api/v1/videos.js +++ b/server/controllers/api/v1/videos.js @@ -142,7 +142,7 @@ function getVideo (req, res, next) { } function listVideos (req, res, next) { - Video.list(req.query.start, req.query.count, req.query.sort, function (err, videosList, videosTotal) { + Video.listForApi(req.query.start, req.query.count, req.query.sort, function (err, videosList, videosTotal) { if (err) return next(err) res.json(getFormatedVideos(videosList, videosTotal)) diff --git a/server/models/video.js b/server/models/video.js index 14bc91b16..a5540d127 100644 --- a/server/models/video.js +++ b/server/models/video.js @@ -11,6 +11,7 @@ const mongoose = require('mongoose') const constants = require('../initializers/constants') const customVideosValidators = require('../helpers/custom-validators').videos const logger = require('../helpers/logger') +const modelUtils = require('./utils') const utils = require('../helpers/utils') const webtorrent = require('../lib/webtorrent') @@ -60,7 +61,7 @@ VideoSchema.methods = { VideoSchema.statics = { getDurationFromFile: getDurationFromFile, - list: list, + listForApi: listForApi, listByUrlAndMagnet: listByUrlAndMagnet, listByUrls: listByUrls, listOwned: listOwned, @@ -194,9 +195,9 @@ function getDurationFromFile (videoPath, callback) { }) } -function list (start, count, sort, callback) { +function listForApi (start, count, sort, callback) { const query = {} - return findWithCount.call(this, query, start, count, sort, callback) + return modelUtils.findWithCount.call(this, query, start, count, sort, callback) } function listByUrlAndMagnet (fromUrl, magnetUri, callback) { @@ -233,7 +234,7 @@ function search (value, field, start, count, sort, callback) { query[field] = new RegExp(value) } - findWithCount.call(this, query, start, count, sort, callback) + modelUtils.findWithCount.call(this, query, start, count, sort, callback) } function seedAllExisting (callback) { @@ -249,25 +250,6 @@ function seedAllExisting (callback) { // --------------------------------------------------------------------------- -function findWithCount (query, start, count, sort, callback) { - const self = this - - 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) - - const videos = results[0] - const totalVideos = results[1] - return callback(null, videos, totalVideos) - }) -} - function removeThumbnail (video, callback) { fs.unlink(thumbnailsDir + video.thumbnail, callback) }