diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-08-16 22:31:45 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-08-16 22:31:45 +0200 |
commit | 5c39adb7313e0696aabb4b71196ab7b0b378c359 (patch) | |
tree | ac44b67890509338b984f8cbf11660dc77cdd0fd /server/models | |
parent | 089ff2f2046fdbaf9531726eea1f8c6726ebf0c0 (diff) | |
download | PeerTube-5c39adb7313e0696aabb4b71196ab7b0b378c359.tar.gz PeerTube-5c39adb7313e0696aabb4b71196ab7b0b378c359.tar.zst PeerTube-5c39adb7313e0696aabb4b71196ab7b0b378c359.zip |
Server: add user list sort/pagination
Diffstat (limited to 'server/models')
-rw-r--r-- | server/models/user.js | 16 | ||||
-rw-r--r-- | server/models/utils.js | 30 | ||||
-rw-r--r-- | server/models/video.js | 4 |
3 files changed, 43 insertions, 7 deletions
diff --git a/server/models/user.js b/server/models/user.js index d289da19a..c9c35b3e2 100644 --- a/server/models/user.js +++ b/server/models/user.js | |||
@@ -1,10 +1,15 @@ | |||
1 | const mongoose = require('mongoose') | 1 | const mongoose = require('mongoose') |
2 | 2 | ||
3 | const customUsersValidators = require('../helpers/custom-validators').users | 3 | const customUsersValidators = require('../helpers/custom-validators').users |
4 | const modelUtils = require('./utils') | ||
4 | 5 | ||
5 | // --------------------------------------------------------------------------- | 6 | // --------------------------------------------------------------------------- |
6 | 7 | ||
7 | const UserSchema = mongoose.Schema({ | 8 | const UserSchema = mongoose.Schema({ |
9 | createdDate: { | ||
10 | type: Date, | ||
11 | default: Date.now | ||
12 | }, | ||
8 | password: String, | 13 | password: String, |
9 | username: String, | 14 | username: String, |
10 | role: String | 15 | role: String |
@@ -19,9 +24,9 @@ UserSchema.methods = { | |||
19 | } | 24 | } |
20 | 25 | ||
21 | UserSchema.statics = { | 26 | UserSchema.statics = { |
22 | count: count, | 27 | countTotal: countTotal, |
23 | getByUsernameAndPassword: getByUsernameAndPassword, | 28 | getByUsernameAndPassword: getByUsernameAndPassword, |
24 | list: list, | 29 | listForApi: listForApi, |
25 | loadById: loadById, | 30 | loadById: loadById, |
26 | loadByUsername: loadByUsername | 31 | loadByUsername: loadByUsername |
27 | } | 32 | } |
@@ -30,7 +35,7 @@ mongoose.model('User', UserSchema) | |||
30 | 35 | ||
31 | // --------------------------------------------------------------------------- | 36 | // --------------------------------------------------------------------------- |
32 | 37 | ||
33 | function count (callback) { | 38 | function countTotal (callback) { |
34 | return this.count(callback) | 39 | return this.count(callback) |
35 | } | 40 | } |
36 | 41 | ||
@@ -38,8 +43,9 @@ function getByUsernameAndPassword (username, password) { | |||
38 | return this.findOne({ username: username, password: password }) | 43 | return this.findOne({ username: username, password: password }) |
39 | } | 44 | } |
40 | 45 | ||
41 | function list (callback) { | 46 | function listForApi (start, count, sort, callback) { |
42 | return this.find(callback) | 47 | const query = {} |
48 | return modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback) | ||
43 | } | 49 | } |
44 | 50 | ||
45 | function loadById (id, callback) { | 51 | function loadById (id, callback) { |
diff --git a/server/models/utils.js b/server/models/utils.js new file mode 100644 index 000000000..a961e8c5b --- /dev/null +++ b/server/models/utils.js | |||
@@ -0,0 +1,30 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const parallel = require('async/parallel') | ||
4 | |||
5 | const utils = { | ||
6 | listForApiWithCount: listForApiWithCount | ||
7 | } | ||
8 | |||
9 | function listForApiWithCount (query, start, count, sort, callback) { | ||
10 | const self = this | ||
11 | |||
12 | parallel([ | ||
13 | function (asyncCallback) { | ||
14 | self.find(query).skip(start).limit(count).sort(sort).exec(asyncCallback) | ||
15 | }, | ||
16 | function (asyncCallback) { | ||
17 | self.count(query, asyncCallback) | ||
18 | } | ||
19 | ], function (err, results) { | ||
20 | if (err) return callback(err) | ||
21 | |||
22 | const data = results[0] | ||
23 | const total = results[1] | ||
24 | return callback(null, data, total) | ||
25 | }) | ||
26 | } | ||
27 | |||
28 | // --------------------------------------------------------------------------- | ||
29 | |||
30 | module.exports = utils | ||
diff --git a/server/models/video.js b/server/models/video.js index a5540d127..63afc2efe 100644 --- a/server/models/video.js +++ b/server/models/video.js | |||
@@ -197,7 +197,7 @@ function getDurationFromFile (videoPath, callback) { | |||
197 | 197 | ||
198 | function listForApi (start, count, sort, callback) { | 198 | function listForApi (start, count, sort, callback) { |
199 | const query = {} | 199 | const query = {} |
200 | return modelUtils.findWithCount.call(this, query, start, count, sort, callback) | 200 | return modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback) |
201 | } | 201 | } |
202 | 202 | ||
203 | function listByUrlAndMagnet (fromUrl, magnetUri, callback) { | 203 | function listByUrlAndMagnet (fromUrl, magnetUri, callback) { |
@@ -234,7 +234,7 @@ function search (value, field, start, count, sort, callback) { | |||
234 | query[field] = new RegExp(value) | 234 | query[field] = new RegExp(value) |
235 | } | 235 | } |
236 | 236 | ||
237 | modelUtils.findWithCount.call(this, query, start, count, sort, callback) | 237 | modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback) |
238 | } | 238 | } |
239 | 239 | ||
240 | function seedAllExisting (callback) { | 240 | function seedAllExisting (callback) { |