aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/dao.js23
-rw-r--r--routes/index.js154
2 files changed, 122 insertions, 55 deletions
diff --git a/lib/dao.js b/lib/dao.js
index 8fd6efd..969bb8e 100644
--- a/lib/dao.js
+++ b/lib/dao.js
@@ -8,6 +8,7 @@ function EmployeeDao(con) {
this.getEmployee = getEmployee;
this.deleteEmployee = deleteEmployee;
this.insertEmployee = insertEmployee;
+ this.updateEmployee = updateEmployee;
}
function getDepartments(pager, cb) {
@@ -125,6 +126,28 @@ function deleteEmployee(emp_no, cb) {
});
}
+function updateEmployee(emp_no, object, cb) {
+ var query = [];
+ var values = [];
+ var i = 1;
+ _.each(object, function(value, key) {
+ query.push(key + '=$' + i++);
+ values.push(value);
+ });
+ query = 'UPDATE employees SET ' + query.join(', ') + ' WHERE emp_no=$' + i;
+ values.push(emp_no);
+
+ /*
+ console.log('object', object);
+ console.log('query', query);
+ console.log('values', values);
+ */
+ this.con.query(query, values, function(err, rs) {
+ if(err) return cb(err);
+ cb(undefined, rs.rowCount);
+ });
+}
+
function insertEmployee(birth_date, first_name, last_name, gender, hire_date, cb) {
var con = this.con;
var insert_employee = {
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
});