Various collection refactoring.

This commit is contained in:
Julien Fontanet 2013-07-16 11:48:54 +02:00
parent 4ea335b974
commit c611cccc06
3 changed files with 74 additions and 45 deletions

View File

@ -31,6 +31,7 @@ Api.prototype.exec = function (session, request) {
if (!method) if (!method)
{ {
console.warn('Invalid method: '+ request.method);
return Q.reject(Api.err.INVALID_METHOD); return Q.reject(Api.err.INVALID_METHOD);
} }
@ -141,7 +142,7 @@ Api.prototype.checkPermission = function (session, permission)
return Q(); 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)) if (!user.hasPermission(permission))
{ {
throw Api.err.UNAUTHORIZED; throw Api.err.UNAUTHORIZED;
@ -175,7 +176,7 @@ Api.fn.session = {
throw Api.err.ALREADY_AUTHENTICATED; 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) if (!user)
{ {
throw Api.err.INVALID_CREDENTIAL; throw Api.err.INVALID_CREDENTIAL;
@ -206,7 +207,7 @@ Api.fn.session = {
throw Api.err.ALREADY_AUTHENTICATED; 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) if (!token)
{ {
throw Api.err.INVALID_CREDENTIAL; throw Api.err.INVALID_CREDENTIAL;
@ -225,7 +226,7 @@ Api.fn.session = {
return null; 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'); return _.pick(user.properties, 'id', 'email', 'permission');
}); });
}), }),
@ -295,7 +296,7 @@ Api.fn.user = {
var user; var user;
var users = this.xo.users; var users = this.xo.users;
return users.get(user_id).then(function (u) { return users.first(user_id).then(function (u) {
user = u; user = u;
return user.checkPassword(p_old); return user.checkPassword(p_old);
@ -315,7 +316,7 @@ Api.fn.user = {
'getAll': function (session) { 'getAll': function (session) {
var users = this.xo.users; var users = this.xo.users;
return this.checkPermission(session, 'admin').then(function () { return this.checkPermission(session, 'admin').then(function () {
return users.where(); return users.get();
}).then(function (all_users) { }).then(function (all_users) {
for (var i = 0, n = all_users.length; i < n; ++i) 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. // @todo Check there are no invalid parameter.
return users.get(p_id); return users.first(p_id);
}).then(function (user) { }).then(function (user) {
// @todo Check user exists. // @todo Check user exists.
@ -413,7 +414,7 @@ Api.fn.token = {
var p_token = req.params.token; var p_token = req.params.token;
var tokens = this.xo.tokens; var tokens = this.xo.tokens;
return tokens.get(p_token).then(function (token) { return tokens.first(p_token).then(function (token) {
if (!token) if (!token)
{ {
throw Api.err.INVALID_PARAMS; throw Api.err.INVALID_PARAMS;
@ -478,7 +479,7 @@ Api.fn.server = {
'getAll': function (session) { 'getAll': function (session) {
var servers = this.xo.servers; var servers = this.xo.servers;
return this.checkPermission(session, 'admin').then(function () { return this.checkPermission(session, 'admin').then(function () {
return servers.where(); return servers.get();
}).then(function (all_servers) { }).then(function (all_servers) {
_.each(all_servers, function (server, i) { _.each(all_servers, function (server, i) {
all_servers[i] = _.pick(server, 'id', 'host', 'username'); all_servers[i] = _.pick(server, 'id', 'host', 'username');

View File

@ -84,10 +84,42 @@ Collection.prototype.add = function (models) {
return Q(array ? models : models[0]); 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 */ /* 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) if (!model)
{ {
@ -97,32 +129,22 @@ Collection.prototype.get = function (id) {
return Q(new this.model(model)); 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. * Find all models which have a given set of properties.
* *
* /!\: Does not return instance of this.model. * /!\: Does not return instance of this.model.
*/ */
Collection.prototype.where = function (properties) { Collection.prototype.get = function (properties) {
/* jshint newcap: false */ /* jshint newcap: false */
// For coherence with other methods.
if ((undefined !== properties) && !_.isObject(properties))
{
properties = {
'id': properties,
};
}
if (_.isEmpty(properties)) if (_.isEmpty(properties))
{ {
return Q(_.extend({}, this.models)); return Q(_.extend({}, this.models));
@ -152,6 +174,17 @@ Collection.prototype.remove = function (ids) {
return Q(true); // @todo Returns false if it fails. 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. * Updates existing models.
*/ */
@ -191,17 +224,6 @@ Collection.prototype.update = function (models) {
return Q(array ? models : models[0]); 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'); Collection.extend = require('extendable');
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////

View File

@ -156,9 +156,15 @@ var Servers = Collection.extend({
function Xo() function Xo()
{ {
if ( !(this instanceof Xo) )
{
return new Xo();
}
this.servers = new Servers(); this.servers = new Servers();
this.tokens = new Tokens(); this.tokens = new Tokens();
this.users = new Users(); this.users = new Users();
this.users.add({ this.users.add({
'email': 'bob@gmail.com', 'email': 'bob@gmail.com',
'pw_hash': '$2a$10$PsSOXflmnNMEOd0I5ohJQ.cLty0R29koYydD0FBKO9Rb7.jvCelZq', 'pw_hash': '$2a$10$PsSOXflmnNMEOd0I5ohJQ.cLty0R29koYydD0FBKO9Rb7.jvCelZq',
@ -181,6 +187,6 @@ function Xo()
} }
require('util').inherits(Xo, require('events').EventEmitter); require('util').inherits(Xo, require('events').EventEmitter);
module.exports = function () { //////////////////////////////////////////////////////////////////////
return new Xo();
}; module.exports = Xo;