-
Notifications
You must be signed in to change notification settings - Fork 0
/
userModel.js
63 lines (48 loc) · 1.8 KB
/
userModel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
var bcrypt = require( 'bcrypt' );
var collection = 'user';
var broker;
function UserModel ( mongoBroker ) {
broker = mongoBroker;
}
UserModel.prototype.getCredsFromReq = function( req ) {
return {
user : req.body.username,
pass : req.body.password
};
};
UserModel.prototype.get = function( id, callback ) {
broker.find( collection, {"_id" : broker.makeId( id )}, {}, callback);
};
UserModel.prototype.getByName = function( username, callback ) {
broker.find( collection, {"username" : username}, {}, callback );
};
UserModel.prototype.addStop = function( userId, stop, callback ) {
var Id = broker.makeId( userId );
broker.update( collection, {"_id" : Id}, {$addToSet : {"HotStops" : stop}}, {}, callback );
};
UserModel.prototype.getStops = function( userId, callback ) {
var Id = broker.makeId( userId );
broker.find(collection, {"_id" : Id}, {"HotStops":1, "_id":0}, callback );
};
UserModel.prototype.removeHotStop = function( userId, stop, callback ) {
var Id = broker.makeId( userId );
broker.update( collection, {"_id" : Id}, {$pull: {"HotStops" : stop}}, callback );
};
UserModel.prototype.addUser = function( username, password, callback ) {
broker.find( 'user', {"username" : username}, {}, function(err, result) {
if (err) throw err;
if ( result.length > 0 ) callback( null, { "success" : false, "duplicate" : true });
bcrypt.hash( password, 5, function(err, hash) {
if (err) throw err;
var user = {
'username' : username,
'password' : hash,
'joined' : new Date(),
'modified' : new Date(),
'HotStops' : []
};
broker.insert('user', user, callback );
});
});
};
module.exports = UserModel;