aboutsummaryrefslogtreecommitdiff
path: root/routes
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2012-07-29 20:24:13 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2012-07-29 20:24:13 +0200
commit06c7d6c9e3837131d92e4d44741196ac2feff930 (patch)
treedaa3ee0f8a2b5cfeaa742a33343348ed7fb30fc1 /routes
parentefb54c43e5432875c15e504e7a0668a33d744a22 (diff)
downloadexample-collection-json-db-master.tar.gz
example-collection-json-db-master.tar.bz2
example-collection-json-db-master.tar.xz
example-collection-json-db-master.zip
o Adding updating of employees.HEADmaster
Diffstat (limited to 'routes')
-rw-r--r--routes/index.js154
1 files changed, 99 insertions, 55 deletions
diff --git a/routes/index.js b/routes/index.js
index 24ddf9f..442c157 100644
--- a/routes/index.js
+++ b/routes/index.js
@@ -137,7 +137,7 @@ function send_cj(status, req, res, c) {
'Content-Length': text.length
};
res.writeHead(status, headers);
- if(req.method == 'GET') {
+ if(req.method != 'HEAD') {
res.write(text);
}
res.end();
@@ -162,6 +162,15 @@ function linksFromPager(req, p, urlgenerator) {
return links;
}
+function parseEmpNo(req, res, f) {
+ var emp_no = parseInt(req.params.emp_no) || 0;
+ if(emp_no < 10000 || emp_no > 999999) {
+ return send_text(res, 404, 'Illegal emp_no.');
+ }
+
+ return f(emp_no);
+}
+
function getIndex(req, res) {
switch(req.accept.types.getBestMatch(['text/html', 'application/vnd.collection+json'])) {
case 'text/html':
@@ -353,45 +362,73 @@ function getEmployees(req, res) {
});
};
-function postEmployees(req, res) {
- switch(req.headers['content-type']) {
- case 'application/vnd.collection+json':
- var s = '';
- req.setEncoding('utf8');
- req.on('data', function(string) {
- s += string;
- }).on('end', function() {
- var body = collection_json.fromObject(JSON.parse(s));
- var data = body.template.toObject();
- connect(res, function(dao) {
- var now = new Date();
- var hire_date = data.hire_date;
- if(typeof hire_date !== 'string' || hire_date.trim().length == 0) {
- hire_date = now.getFullYear() + '-' + (now.getMonth() + 1) + '-' + now.getDate();
+function parseBody(cb) {
+ return function(req, res) {
+ switch(req.headers['content-type']) {
+ case 'application/vnd.collection+json':
+ var s = '';
+ req.setEncoding('utf8');
+ req.on('data', function(string) {
+ s += string;
+ }).on('end', function() {
+ try {
+ cb(req, res, collection_json.fromObject(JSON.parse(s)));
+ }
+ catch(e) {
+ send_text(res, 400, 'Unable to parse JSON: ' + e);
}
-
- dao.insertEmployee(data.birth_date,
- data.first_name,
- data.last_name,
- data.gender,
- hire_date, after(res, function(emp_no) {
- res.writeHead(201, {'Location' : res.urlgenerator.employee(emp_no)});
- res.end();
- }));
});
- });
- break;
- default:
- res.send(415, 'Sending "application/vnd.collection+json" is required.');
- break;
+ break;
+ default:
+ res.send(415, 'Sending "application/vnd.collection+json" is required.');
+ break;
+ }
}
}
+var postEmployees = parseBody(function(req, res, body) {
+ var data = body.template.toObject();
+ connect(res, function(dao) {
+ var now = new Date();
+ var hire_date = data.hire_date;
+ if(typeof hire_date !== 'string' || hire_date.trim().length == 0) {
+ hire_date = now.getFullYear() + '-' + (now.getMonth() + 1) + '-' + now.getDate();
+ }
+
+ dao.insertEmployee(data.birth_date,
+ data.first_name,
+ data.last_name,
+ data.gender,
+ hire_date, after(res, function(emp_no) {
+ res.writeHead(201, {'Location' : res.urlgenerator.employee(emp_no)});
+ res.end();
+ }));
+ });
+});
+
exports.employees = method({
GET: getEmployees,
POST: postEmployees
});
+function send_employee_cj(req, res, employee) {
+ var c;
+ if(typeof employee == 'object') {
+ c = {collection: {
+ href: res.urlgenerator.employee(employee.emp_no),
+ items: mapEmployees(res, [ employee ])
+ }};
+ }
+ else {
+ c = {collection: {
+ error: {
+ title: 'No employee with emp_no=' + emp_no + '.'
+ }
+ }};
+ }
+ send_cj(typeof employee == 'object' ? 200 : 404, req, res, c);
+}
+
function getEmployee(req, res) {
connect(res, function(dao) {
var emp_no = req.params.emp_no;
@@ -411,21 +448,7 @@ function getEmployee(req, res) {
break;
case 'application/vnd.collection+json':
case '*/*':
- var c;
- if(typeof employee == 'object') {
- c = {collection: {
- href: res.urlgenerator.employee(emp_no),
- items: mapEmployees(res, [ employee ])
- }};
- }
- else {
- c = {collection: {
- error: {
- title: 'No employee with emp_no=' + emp_no + '.'
- }
- }};
- }
- send_cj(typeof employee == 'object' ? 200 : 404, req, res, c);
+ send_employee_cj(req, res, employee);
break;
default:
res.send(406);
@@ -436,20 +459,41 @@ function getEmployee(req, res) {
function deleteEmployee(req, res) {
connect(res, function(dao) {
- var emp_no = parseInt(req.params.emp_no) || 0;
- if(emp_no < 10000 || emp_no > 999999) {
- return send_text(res, 404, 'Illegal emp_no.');
- }
- dao.deleteEmployee(emp_no, after(res, function(rowCount) {
- if(rowCount == 0) {
- return send_text(res, 404, 'No employee with emp_no=' + emp_no + '.');
- }
- res.send(204);
- }));
+ parseEmpNo(req, res, function(emp_no) {
+ dao.deleteEmployee(emp_no, after(res, function(rowCount) {
+ if(rowCount == 0) {
+ return send_text(res, 404, 'No employee with emp_no=' + emp_no + '.');
+ }
+ res.send(204);
+ }));
+ });
});
}
+var postEmployee = parseBody(function(req, res, body) {
+ connect(res, function(dao) {
+ parseEmpNo(req, res, function(emp_no) {
+ if(!body.isTemplate()) {
+ return send_text(res, 400, 'You have to send a proper template. See http://amundsen.com/media-types/collection/format/, section 1.1.4');
+ }
+ var o = body.template.toObject();
+ delete o.emp_no;
+ dao.updateEmployee(emp_no, o, after(res, function(rowCount) {
+ console.log('after updateEmployee');
+ console.log(arguments);
+ if(rowCount == 0) {
+ return send_text(res, 404, 'No employee with emp_no=' + emp_no + '.');
+ }
+ dao.getEmployee(emp_no, after(res, function(employee) {
+ send_employee_cj(req, res, employee);
+ }));
+ }));
+ });
+ });
+});
+
exports.employee = method({
GET: getEmployee,
+ POST: postEmployee,
DELETE: deleteEmployee
});