Created
February 22, 2013 17:07
-
-
Save artcommacode/5014982 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var User = function (user) { | |
this.user = user; | |
}; | |
User.prototype.all = function (callback) { | |
var User = this; | |
var users = []; | |
/* snip */ | |
callback(error, users); | |
}; | |
User.prototype.save = function (callback) { | |
var User = this | |
, user = User.user; | |
async.waterfall([ | |
function (callback) { | |
if (!user) { | |
callback(new Error('No User found')); | |
} else { | |
callback(null); | |
} | |
}, | |
function (callback) { | |
if (!user.id) { | |
client.incr('next.' + db + '.users.id', function (error, id) { | |
user.id = id; | |
user.createdAt = +Date.now(); | |
client.get(db + ':users:emails:' + user.email, callback); | |
}); | |
} else { | |
client.get(db + ':users:emails:' + user.email, callback); | |
} | |
}, | |
function (id, callback) { | |
if (!id || id == user.id) { | |
client.get(db + ':users:names:' + user.name, callback); | |
} else { | |
callback(new Error('Details already taken.')); | |
} | |
}, | |
function (id, callback) { | |
if (!id || id == user.id) { | |
User.get(user, callback); | |
} else { | |
callback(new Error('Details already taken.')); | |
} | |
}, | |
function (returnedUser, callback) { | |
if (returnedUser) { | |
User.delete(returnedUser, callback); | |
} else { | |
callback(null, null); | |
} | |
}, | |
function (response, callback) { | |
client.sadd(db + ':users', user.id, callback); | |
}, | |
function (response, callback) { | |
client.set(db + ':users:emails:' + user.email, user.id, callback); | |
}, | |
function (response, callback) { | |
client.set(db + ':users:names:' + user.name, user.id, callback); | |
}, | |
function (response, callback) { | |
bcrypt.genSalt(10, callback); | |
}, | |
function (salt, callback) { | |
bcrypt.hash(user.password, salt, callback); | |
}, | |
function (hash, callback) { | |
user.password = hash; | |
user.updatedAt = +Date.now(); | |
client.hmset(db + ':users:' + user.id, user, callback); | |
} | |
], function (error, result) { | |
callback(error, user); | |
}); | |
}; | |
User.prototype.set = function(user, callback) { | |
var User = this; | |
if (!user.id) user = {id: user}; | |
client.hgetall(db + ':users:' + user.id, function (error, user) { | |
User.user = user; | |
callback(error); | |
}); | |
}; | |
User.prototype.get = function(user, callback) { | |
if (!user.id) user = {id: user}; | |
client.hgetall(db + ':users:' + user.id, callback); | |
}; | |
User.prototype.delete = function(user, callback) { | |
async.waterfall([ | |
function (callback) { | |
client.del(db + ':users:emails:' + user.email, callback); | |
}, | |
function (response, callback) { | |
client.del(db + ':users:names:' + user.name, callback); | |
}, | |
function (response, callback) { | |
client.srem( db + ':users', user.id, callback); | |
}, | |
function (response, callback) { | |
client.del(db + ':users:' + user.id, callback); | |
} | |
], function (error, result) { | |
callback(error, false); | |
}); | |
}; | |
module.exports = User; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment