aboutsummaryrefslogtreecommitdiff
path: root/routes
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2012-07-14 01:02:35 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2012-07-14 01:02:35 +0200
commit47b4b51573f240763ee8b761bfe9f37ffce8e0fb (patch)
tree08cb4a77db6c84e7d9a560b8a1b4240b1c9dce26 /routes
parent27fdccc050bbfe09beca29d8d50ca25e18c5102a (diff)
downloadexample-collection-json-db-47b4b51573f240763ee8b761bfe9f37ffce8e0fb.tar.gz
example-collection-json-db-47b4b51573f240763ee8b761bfe9f37ffce8e0fb.tar.bz2
example-collection-json-db-47b4b51573f240763ee8b761bfe9f37ffce8e0fb.tar.xz
example-collection-json-db-47b4b51573f240763ee8b761bfe9f37ffce8e0fb.zip
o Adding support for inserting employees.
Diffstat (limited to 'routes')
-rw-r--r--routes/index.js43
1 files changed, 39 insertions, 4 deletions
diff --git a/routes/index.js b/routes/index.js
index 49db10c..82bce70 100644
--- a/routes/index.js
+++ b/routes/index.js
@@ -313,11 +313,11 @@ function getEmployees(req, res) {
}],
template: {
data: [
- { name: 'birth_date', prompt: 'Birth Date' },
+ { name: 'birth_date', prompt: 'Birth Date (YYYY-MM-DD)' },
{ name: 'first_name', prompt: 'First Name' },
{ name: 'last_name', prompt: 'Last Name' },
- { name: 'gender', prompt: 'Gender' },
- { name: 'hire_date', prompt: 'Hire Date. Defaults to today.' }
+ { name: 'gender', prompt: 'Gender (M/F)' },
+ { name: 'hire_date', prompt: 'Hire Date (YYYY-MM-DD). Defaults to today.' }
]
},
items: mapEmployees(res, employees)
@@ -331,8 +331,43 @@ 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();
+ }
+
+ 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;
+ }
+}
+
exports.employees = method({
- GET: getEmployees
+ GET: getEmployees,
+ POST: postEmployees
});
function getEmployee(req, res) {