aboutsummaryrefslogtreecommitdiff
path: root/routes
diff options
context:
space:
mode:
Diffstat (limited to 'routes')
-rw-r--r--routes/index.js40
1 files changed, 23 insertions, 17 deletions
diff --git a/routes/index.js b/routes/index.js
index 82dd1ca..23af53d 100644
--- a/routes/index.js
+++ b/routes/index.js
@@ -87,14 +87,16 @@ exports.departments = function(req, res) {
client.query(sql, after(res, function(rs) {
var p = pager(req, parseInt(rs.rows[0].n_live_tup));
client.query(sql2, [ p.offset, p.limit ], after(res, function(rs2) {
- if(req.accepts('html')) {
+ switch(req.accept.types.getBestMatch(["text/html", "application/vnd.collection+json"])) {
+ case "text/html":
res.render('departments', {
title: 'Department List',
urlgenerator: res.urlgenerator, pager: p, query: req.query,
departments: rs2.rows
});
- }
- else {
+ break;
+ case "application/vnd.collection+json":
+ default:
var c = {collection: {
href: res.urlgenerator.departments(),
items: mapDepartments(res, rs2.rows)
@@ -109,14 +111,15 @@ exports.departments = function(req, res) {
exports.department = function(req, res) {
var dept_no = req.params.dept_no;
- if(req.accepts('html')) {
+ switch(req.accept.types.getBestMatch(["text/html", "application/vnd.collection+json"])) {
+ case "text/html":
res.render('department', {
title: 'Department ' + dept_no,
urlgenerator: res.urlgenerator,
dept_no: dept_no
});
- }
- else {
+ case "application/vnd.collection+json":
+ default:
var c = {collection: {
href: res.urlgenerator.department(dept_no),
links: [ {
@@ -130,7 +133,7 @@ exports.department = function(req, res) {
} ]
}};
res.contentType('application/vnd.collection+json');
- res.send(collection_json.fromObject(c));
+ res.send(JSON.stringify(collection_json.fromObject(c)), 200);
}
}
@@ -146,15 +149,16 @@ exports.employees_in_department = function(req, res) {
client.query(sql1, [ dept_no ], after(res, function(rs1) {
var p = pager(req, parseInt(rs1.rows[0].count));
client.query(sql2, [ dept_no, p.offset, p.limit ], after(res, function(rs2) {
- if(req.accepts('html')) {
+ switch(req.accept.types.getBestMatch(["text/html", "application/vnd.collection+json"])) {
+ case "text/html":
res.render('employees_in_department', {
title: 'Department: #' + dept_no,
urlgenerator: res.urlgenerator, pager: p, query: req.query,
dept_no: dept_no,
employees: rs2.rows
});
- }
- else {
+ case "application/vnd.collection+json":
+ default:
var c = {collection: {
href: res.urlgenerator.employees_in_department(dept_no),
links: [ {
@@ -165,7 +169,7 @@ exports.employees_in_department = function(req, res) {
items: mapEmployees(res, rs2.rows)
}};
res.contentType('application/vnd.collection+json');
- res.send(collection_json.fromObject(c));
+ res.send(JSON.stringify(collection_json.fromObject(c)), 200);
}
}));
}));
@@ -202,15 +206,16 @@ exports.employees = function(req, res) {
sql2Params.push(p.offset);
sql2Params.push(p.limit);
client.query(sql2, sql2Params, after(res, function(rs2) {
- if(req.accepts('html')) {
+ switch(req.accept.types.getBestMatch(["text/html", "application/vnd.collection+json"])) {
+ case "text/html":
res.render('employees', {
title: 'Employee List',
urlgenerator: res.urlgenerator, pager: p, query: req.query,
employees: rs2.rows,
query: req.query
});
- }
- else {
+ case "application/vnd.collection+json":
+ default:
var links = [];
if(_.isNumber(p.prevOffset)) {
links.push({
@@ -254,14 +259,15 @@ exports.employee = function(req, res) {
var emp_no = req.params.emp_no;
var sql = 'SELECT * FROM employees WHERE employees.emp_no=$1';
client.query(sql, [ emp_no ], after(res, function(rs) {
- if(req.accepts('html')) {
+ switch(req.accept.types.getBestMatch(["text/html", "application/vnd.collection+json"])) {
+ case "text/html":
res.render('employee', {
title: 'Employee: #' + emp_no,
urlgenerator: res.urlgenerator,
employee: rs.rows[0]
});
- }
- else {
+ case "application/vnd.collection+json":
+ default:
var c = {collection: {
href: res.urlgenerator.employee(emp_no),
items: [ mapRow(rs.rows[0]) ],