aboutsummaryrefslogtreecommitdiff
path: root/routes
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2012-07-01 03:26:26 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2012-07-01 03:26:26 +0200
commitc66f8be25f230f8fcea2774e54a56ad43fba9b58 (patch)
treed20130f43292ff0322afcc71b9fcbf41e076fd93 /routes
parent02588ca7aa174c5b4c95c8f124eb8086042f9538 (diff)
downloadexample-collection-json-db-c66f8be25f230f8fcea2774e54a56ad43fba9b58.tar.gz
example-collection-json-db-c66f8be25f230f8fcea2774e54a56ad43fba9b58.tar.bz2
example-collection-json-db-c66f8be25f230f8fcea2774e54a56ad43fba9b58.tar.xz
example-collection-json-db-c66f8be25f230f8fcea2774e54a56ad43fba9b58.zip
o Adding paging.
o Adding new root resource.
Diffstat (limited to 'routes')
-rw-r--r--routes/index.js88
1 files changed, 79 insertions, 9 deletions
diff --git a/routes/index.js b/routes/index.js
index 9d0b8ac..dcb6e23 100644
--- a/routes/index.js
+++ b/routes/index.js
@@ -25,22 +25,60 @@ function mapEmployees(res, employees) {
});
}
-function limit(req) {
- return "LIMIT " + (_.isString(limit) ? limit : 10);
+function pager(req) {
+ var pageSize = 10;
+ var offset = parseInt(req.query.offset) || 0;
+ var limit = parseInt(req.query.limit) || pageSize;
+ var prevOffset = offset - pageSize;
+ var nextOffset = offset + pageSize;
+ return {
+ offset: offset,
+ limit: limit,
+ prevOffset: prevOffset >= 0 ? prevOffset : 0,
+ nextOffset: function(length) {
+ return length > 0 ? nextOffset : undefined;
+ }
+ };
}
+exports.index = function(req, res) {
+ if(req.accepts('html')) {
+ res.render('index', {
+ title: 'Employee DB',
+ urlgenerator: res.urlgenerator
+ });
+ }
+ else {
+ var c = {collection: {
+ links: [ {
+ rel: 'departments',
+ prompt: 'Department List',
+ href: res.urlgenerator.departments()
+ }, {
+ rel: 'employees',
+ prompt: 'Employee List',
+ href: res.urlgenerator.employees()
+ } ]
+ }};
+ res.contentType('application/vnd.collection+json');
+ res.send(JSON.stringify(collection_json.fromObject(c)), 200);
+ }
+};
+
exports.departments = function(req, res) {
pg.connect(process.env.DATABASE_URL, function(err, client) {
if(err) throw err;
+ var p = pager(req);
var departments = [];
- client.query('SELECT dept_no, dept_name FROM departments').
+ client.query('SELECT dept_no, dept_name FROM departments OFFSET $1 LIMIT $2', [ p.offset, p.limit ] ).
on('row', function(row) {
departments.push(row);
}).
on('end', function() {
if(req.accepts('html')) {
res.render('departments', {
- title: 'Departments',
+ title: 'Department List',
+ urlgenerator: res.urlgenerator, pager: p,
departments: departments
});
}
@@ -87,19 +125,20 @@ exports.employees_in_department = function(req, res) {
pg.connect(process.env.DATABASE_URL, function(err, client) {
if(err) throw err;
var dept_no = req.params.dept_no;
+ var p = pager(req);
var employees = [];
// TODO: Add dept_name to view
// TODO: Add manager as a link
- client.query('SELECT employees.* FROM employees, dept_emp WHERE employees.emp_no=dept_emp.emp_no AND dept_emp.dept_no=$1 AND dept_emp.to_date > now() AND now() > dept_emp.from_date ' + limit(req), [ dept_no ]).
+ client.query('SELECT employees.* FROM employees, dept_emp WHERE employees.emp_no=dept_emp.emp_no AND dept_emp.dept_no=$1 AND dept_emp.to_date > now() AND now() > dept_emp.from_date OFFSET $2 LIMIT $3', [ dept_no, p.offset, p.limit ]).
on('row', function(row) {
employees.push(row);
}).
on('end', function() {
if(req.accepts('html')) {
res.render('employees_in_department', {
- title: 'Department ' + dept_no,
+ title: 'Department: #' + dept_no,
+ urlgenerator: res.urlgenerator, pager: p,
dept_no: dept_no,
- urlgenerator: res.urlgenerator,
employees: employees
});
}
@@ -108,7 +147,7 @@ exports.employees_in_department = function(req, res) {
href: res.urlgenerator.employees_in_department(dept_no),
links: [ {
rel: 'department',
- prompt: 'Departments ' + dept_no,
+ prompt: 'Department: #' + dept_no,
href: res.urlgenerator.department(dept_no)
} ],
items: mapEmployees(res, employees)
@@ -120,6 +159,36 @@ exports.employees_in_department = function(req, res) {
});
};
+exports.employees = function(req, res) {
+ pg.connect(process.env.DATABASE_URL, function(err, client) {
+ if(err) throw err;
+ var p = pager(req);
+ var emp_no = req.params.emp_no;
+ var employees = [];
+ client.query('SELECT * FROM employees ORDER BY emp_no OFFSET $1 LIMIT $2', [ p.offset, p.limit ]).
+ on('row', function(row) {
+ employees.push(row);
+ }).
+ on('end', function() {
+ if(req.accepts('html')) {
+ res.render('employees', {
+ title: 'Employee List',
+ urlgenerator: res.urlgenerator, pager: p,
+ employees: employees
+ });
+ }
+ else {
+ var c = {collection: {
+ href: res.urlgenerator.employees(),
+ items: mapEmployees(res, employees)
+ }};
+ res.contentType('application/vnd.collection+json');
+ res.send(JSON.stringify(collection_json.fromObject(c)), 200);
+ }
+ });
+ });
+};
+
exports.employee = function(req, res) {
pg.connect(process.env.DATABASE_URL, function(err, client) {
if(err) throw err;
@@ -132,7 +201,8 @@ exports.employee = function(req, res) {
on('end', function() {
if(req.accepts('html')) {
res.render('employee', {
- title: 'Employee ' + emp_no,
+ title: 'Employee: #' + emp_no,
+ urlgenerator: res.urlgenerator,
employee: employee
});
}