-
Notifications
You must be signed in to change notification settings - Fork 6
/
userController.js
44 lines (40 loc) · 1.06 KB
/
userController.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
var User = require('../models/userModel.js');
var _ = require('underscore');
exports.findById = function(req, res){
var id = req.params.id;
console.log('Retrieving user by ID: ' + id);
User.findById(id, function(err, obj){
if(err) console.log(err);
res.send(obj);
});
};
exports.findAll = function(req, res){
User.find({}, function(err, obj){
res.send(obj);
});
};
exports.findByUsername = function(req, res){
var username = req.params.username;
console.log('Retrieving user: '+ username);
var query = {'username': username};
User.findOne(query, function (err, obj){
if(err){
res.writeHead(404);
res.end();
} else {
res.send(obj);
}
});
};
exports.findByRegex = function(req, res) {
//For autocompletion while searching for players to invite to a game
var username = req.params.partial;
var regex = '^'+username+'.*';
User.find({username: {$regex: regex, $options: 'i'}})
.sort({username: 1})
.limit(10)
.exec(function (err, obj){
if(err) console.log(err);
res.send(obj);
});
};