From 9bd2662976a75d3b03364cdbe6419e57c80f99a6 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 4 Aug 2016 22:32:36 +0200 Subject: Implement user API (create, update, remove, list) --- server/models/user.js | 33 +++++++++++++++++++++++++++------ server/models/video.js | 5 +++++ 2 files changed, 32 insertions(+), 6 deletions(-) (limited to 'server/models') diff --git a/server/models/user.js b/server/models/user.js index 14ffecbff..0bbd638d4 100644 --- a/server/models/user.js +++ b/server/models/user.js @@ -1,28 +1,49 @@ const mongoose = require('mongoose') +const customUsersValidators = require('../helpers/custom-validators').users + // --------------------------------------------------------------------------- const UserSchema = mongoose.Schema({ password: String, - username: String + username: String, + role: String }) -UserSchema.path('password').required(true) -UserSchema.path('username').required(true) +UserSchema.path('password').required(customUsersValidators.isUserPasswordValid) +UserSchema.path('username').required(customUsersValidators.isUserUsernameValid) +UserSchema.path('role').validate(customUsersValidators.isUserRoleValid) + +UserSchema.methods = { + toFormatedJSON: toFormatedJSON +} UserSchema.statics = { getByUsernameAndPassword: getByUsernameAndPassword, - list: list + list: list, + loadByUsername: loadByUsername } mongoose.model('User', UserSchema) // --------------------------------------------------------------------------- +function getByUsernameAndPassword (username, password) { + return this.findOne({ username: username, password: password }) +} + function list (callback) { return this.find(callback) } -function getByUsernameAndPassword (username, password) { - return this.findOne({ username: username, password: password }) +function loadByUsername (username, callback) { + return this.findOne({ username: username }, callback) +} + +function toFormatedJSON () { + return { + id: this._id, + username: this.username, + role: this.role + } } diff --git a/server/models/video.js b/server/models/video.js index acb8353c2..14bc91b16 100644 --- a/server/models/video.js +++ b/server/models/video.js @@ -64,6 +64,7 @@ VideoSchema.statics = { listByUrlAndMagnet: listByUrlAndMagnet, listByUrls: listByUrls, listOwned: listOwned, + listOwnedByAuthor: listOwnedByAuthor, listRemotes: listRemotes, load: load, search: search, @@ -211,6 +212,10 @@ function listOwned (callback) { this.find({ filename: { $ne: null } }, callback) } +function listOwnedByAuthor (author, callback) { + this.find({ filename: { $ne: null }, author: author }, callback) +} + function listRemotes (callback) { this.find({ filename: null }, callback) } -- cgit v1.2.3