diff options
Diffstat (limited to 'routes')
-rw-r--r-- | routes/index.js | 43 |
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) { |