'use strict'
+const async = require('async')
const config = require('config')
const mongoose = require('mongoose')
podUrl: String,
author: String,
duration: Number,
- thumbnail: String
+ thumbnail: String,
+ createdDate: {
+ type: Date,
+ default: Date.now
+ }
})
const VideosDB = mongoose.model('videos', videosSchema)
const params = video
params.podUrl = http + '://' + host + ':' + port
- VideosDB.create(params, function (err, video) {
+ VideosDB.create(params, function (err, insertedVideo) {
if (err) {
logger.error('Cannot insert this video into database.')
return callback(err)
}
- callback(null)
+ callback(null, insertedVideo)
})
}
})
}
-function list (callback) {
- VideosDB.find(function (err, videos_list) {
- if (err) {
- logger.error('Cannot get the list of the videos.')
- return callback(err)
- }
-
- return callback(null, videos_list)
- })
+function list (start, count, sort, callback) {
+ const query = {}
+ return findWithCount(query, start, count, sort, callback)
}
function listFromUrl (fromUrl, callback) {
function listOwned (callback) {
// If namePath is not null this is *our* video
- VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) {
+ VideosDB.find({ namePath: { $ne: null } }, function (err, videosList) {
if (err) {
logger.error('Cannot get the list of owned videos.')
return callback(err)
}
- return callback(null, videos_list)
+ return callback(null, videosList)
})
}
VideosDB.remove({ _id: { $in: ids } }, callback)
}
-function search (name, callback) {
- VideosDB.find({ name: new RegExp(name) }, function (err, videos) {
- if (err) {
- logger.error('Cannot search the videos.')
- return callback(err)
- }
+function search (value, field, start, count, sort, callback) {
+ const query = {}
+ // Make an exact search with the magnet
+ if (field === 'magnetUri') {
+ query[field] = value
+ } else {
+ query[field] = new RegExp(value)
+ }
- return callback(null, videos)
- })
+ 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)
+ })
+}