From c611cccc06a347c9740f8dcb23ed5c35ae8093f4 Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Tue, 16 Jul 2013 11:48:54 +0200 Subject: [PATCH] Various collection refactoring. --- src/api.js | 19 +++++----- src/collection.js | 88 +++++++++++++++++++++++++++++------------------ src/xo.js | 12 +++++-- 3 files changed, 74 insertions(+), 45 deletions(-) diff --git a/src/api.js b/src/api.js index 9784e9035..47cf0deba 100644 --- a/src/api.js +++ b/src/api.js @@ -31,6 +31,7 @@ Api.prototype.exec = function (session, request) { if (!method) { + console.warn('Invalid method: '+ request.method); return Q.reject(Api.err.INVALID_METHOD); } @@ -141,7 +142,7 @@ Api.prototype.checkPermission = function (session, permission) return Q(); } - return this.xo.users.get(user_id).then(function (user) { + return this.xo.users.first(user_id).then(function (user) { if (!user.hasPermission(permission)) { throw Api.err.UNAUTHORIZED; @@ -175,7 +176,7 @@ Api.fn.session = { throw Api.err.ALREADY_AUTHENTICATED; } - return this.xo.users.findWhere({'email': p_email}).then(function (user) { + return this.xo.users.first({'email': p_email}).then(function (user) { if (!user) { throw Api.err.INVALID_CREDENTIAL; @@ -206,7 +207,7 @@ Api.fn.session = { throw Api.err.ALREADY_AUTHENTICATED; } - return this.xo.tokens.get(p_token).then(function (token) { + return this.xo.tokens.first(p_token).then(function (token) { if (!token) { throw Api.err.INVALID_CREDENTIAL; @@ -225,7 +226,7 @@ Api.fn.session = { return null; } - return this.xo.users.get(user_id).then(function (user) { + return this.xo.users.first(user_id).then(function (user) { return _.pick(user.properties, 'id', 'email', 'permission'); }); }), @@ -295,7 +296,7 @@ Api.fn.user = { var user; var users = this.xo.users; - return users.get(user_id).then(function (u) { + return users.first(user_id).then(function (u) { user = u; return user.checkPassword(p_old); @@ -315,7 +316,7 @@ Api.fn.user = { 'getAll': function (session) { var users = this.xo.users; return this.checkPermission(session, 'admin').then(function () { - return users.where(); + return users.get(); }).then(function (all_users) { for (var i = 0, n = all_users.length; i < n; ++i) { @@ -363,7 +364,7 @@ Api.fn.user = { // @todo Check there are no invalid parameter. - return users.get(p_id); + return users.first(p_id); }).then(function (user) { // @todo Check user exists. @@ -413,7 +414,7 @@ Api.fn.token = { var p_token = req.params.token; var tokens = this.xo.tokens; - return tokens.get(p_token).then(function (token) { + return tokens.first(p_token).then(function (token) { if (!token) { throw Api.err.INVALID_PARAMS; @@ -478,7 +479,7 @@ Api.fn.server = { 'getAll': function (session) { var servers = this.xo.servers; return this.checkPermission(session, 'admin').then(function () { - return servers.where(); + return servers.get(); }).then(function (all_servers) { _.each(all_servers, function (server, i) { all_servers[i] = _.pick(server, 'id', 'host', 'username'); diff --git a/src/collection.js b/src/collection.js index 6ac07d601..b02380cc5 100644 --- a/src/collection.js +++ b/src/collection.js @@ -84,10 +84,42 @@ Collection.prototype.add = function (models) { return Q(array ? models : models[0]); }; -Collection.prototype.get = function (id) { +/** + * + */ +Collection.prototype.count = function (properties) { + return this.get(properties).then(function (models) { + return models.length; + }); +}; + + +/** + * + */ +Collection.prototype.exists = function (properties) { + return this.first(properties).then(function (model) { + return (null !== model); + }); +}; + +/** + * + */ +Collection.prototype.first = function (properties) { /* jshint newcap:false */ - var model = this.models[id]; + var model; + + if (_.isObject(properties)) + { + model = _.findWhere(this.models, properties); + } + else + { + // Research by id. + model = this.models[properties]; + } if (!model) { @@ -97,32 +129,22 @@ Collection.prototype.get = function (id) { return Q(new this.model(model)); }; -/** - * - */ -Collection.prototype.exists = function (id) { - return (undefined !== this.models[id]); -}; - -/** - * Find the first model which has a given set of properties. - */ -Collection.prototype.findWhere = function (properties) { - /* jshint newcap: false */ - - var model = _.findWhere(this.models, properties); - - return Q(model ? new this.model(model) : null); -}; - - /** * Find all models which have a given set of properties. * * /!\: Does not return instance of this.model. */ -Collection.prototype.where = function (properties) { +Collection.prototype.get = function (properties) { /* jshint newcap: false */ + + // For coherence with other methods. + if ((undefined !== properties) && !_.isObject(properties)) + { + properties = { + 'id': properties, + }; + } + if (_.isEmpty(properties)) { return Q(_.extend({}, this.models)); @@ -152,6 +174,17 @@ Collection.prototype.remove = function (ids) { return Q(true); // @todo Returns false if it fails. }; +/** + * Smartly updates the collection. + * + * - Adds new models. + * - Updates existing models. + * - Removes missing models. + */ +// Collection.prototype.set = function (/*models*/) { +// // @todo +// }; + /** * Updates existing models. */ @@ -191,17 +224,6 @@ Collection.prototype.update = function (models) { return Q(array ? models : models[0]); }; -/** - * Smartly updates the collection. - * - * - Adds new models. - * - Updates existing models. - * - Removes missing models. - */ -Collection.prototype.set = function (/*models*/) { - throw 'not implemented'; -}; - Collection.extend = require('extendable'); ////////////////////////////////////////////////////////////////////// diff --git a/src/xo.js b/src/xo.js index 246b17702..dfc0cfcc2 100644 --- a/src/xo.js +++ b/src/xo.js @@ -156,9 +156,15 @@ var Servers = Collection.extend({ function Xo() { + if ( !(this instanceof Xo) ) + { + return new Xo(); + } + this.servers = new Servers(); this.tokens = new Tokens(); this.users = new Users(); + this.users.add({ 'email': 'bob@gmail.com', 'pw_hash': '$2a$10$PsSOXflmnNMEOd0I5ohJQ.cLty0R29koYydD0FBKO9Rb7.jvCelZq', @@ -181,6 +187,6 @@ function Xo() } require('util').inherits(Xo, require('events').EventEmitter); -module.exports = function () { - return new Xo(); -}; +////////////////////////////////////////////////////////////////////// + +module.exports = Xo;