-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
106 lines (100 loc) · 3.21 KB
/
app.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
var express = require('express');
var app = express();
var request = require('request');
var serveIndex = require('serve-static');
var _ = require('lodash');
var allUsers = [
{ id : 1, first_name: 'james', last_name : 'hrisho'},
{ id : 2, first_name: 'abc', last_name : 'ahrisho'},
{ id : 3, first_name: 'james', last_name : 'hrisho'},
{ id : 4, first_name: 'james', last_name : 'hrisho'},
{ id : 5, first_name: 'james', last_name : 'hrisho'},
{ id : 6, first_name: 'james', last_name : 'hrisho'},
{ id : 7, first_name: 'james', last_name : 'hrisho'},
{ id : 8, first_name: 'james', last_name : 'hrisho'},
{ id : 9, first_name: 'james', last_name : 'hrisho'},
{ id : 10, first_name: 'james', last_name : 'hrisho'}
];
var filteredUsersCount = 1;
/**
* Fetch the users
* @param {object} req The request itself
*/
function getUsers(req) {
var allUsersClone = _.clone(allUsers);
var currentPage = req.query.page ? parseInt(req.query.page ): 1;
var pageSize = req.query.per_page ? parseInt(req.query.per_page) : 10;
if(req.query.q && req.query != '' && req.query.fields && req.query.fields != '') {
var fields = req.query.fields.split(',');
allUsersClone = _.filter(allUsersClone,function(model) {
var regexTest = new RegExp(req.query.q,"gi");
var result = false;
_.each(fields, function(field) {
if(regexTest.test(model[field])) {
result = true;
}
});
return result;
});
filteredUsersCount = allUsersClone.lenght;
}
if(req.query.sort_by) {
var sortBy = req.query.sort_by;
allUsersClone = _.sortBy(allUsersClone,sortBy)
if (req.query.order && req.query.order == 'desc') {
allUsersClone.reverse();
}
}
var startingRecord = 0;
if (currentPage !== 1) {
startingRecord = ((currentPage - 1)* pageSize) ;
}
var users = []
for(var i = startingRecord; i < (startingRecord + pageSize) ; i++) {
if(allUsersClone[i]) users.push(allUsersClone[i]);
}
var usersObj = {};
usersObj.users = users;
usersObj.totalPages = (allUsersClone.length / pageSize);
return usersObj
//return users;
}
app.get('/api/users', function(req, res){
var users = getUsers(req);
var currentPage = req.query.page ? parseInt(req.query.page ): 1;
var pageSize = req.query.per_page ? parseInt(req.query.per_page) : 10;
var totalPages = users.totalPages
if (totalPages < 1) {
totalPages = 1;
}
var order = -1
if(req.query.order && req.query.order == 'desc'){
order = 1;
};
var totalRecords = allUsers.length;
if(req.query.q) {
totalRecords = filteredUsersCount;
}
var sort_by = req.query.sort_by ? req.query.sort_by : "id";
res.json({
"data" : users.users,
"pagination" : {
totalPages : totalPages,
totalRecords : totalRecords,
currentPage: currentPage,
pageSize : pageSize,
order: order,
sort_by: sort_by,
query : req.query.q ? req.query.q : '',
fields: req.query.fields ? req.query.fields : ''
}
});
});
app.get('/', function(req,res) {
res.send('hi james');
});
app.use('/app', serveIndex('app', {'icons': true}))
var port = Number(3001);
var server = app.listen(port, function() {
console.log('Listening on port %d', server.address().port);
});