diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2012-07-14 01:02:35 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2012-07-14 01:02:35 +0200 |
commit | 47b4b51573f240763ee8b761bfe9f37ffce8e0fb (patch) | |
tree | 08cb4a77db6c84e7d9a560b8a1b4240b1c9dce26 | |
parent | 27fdccc050bbfe09beca29d8d50ca25e18c5102a (diff) | |
download | example-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.
-rw-r--r-- | app.js | 2 | ||||
-rw-r--r-- | lib/dao.js | 27 | ||||
-rw-r--r-- | routes/index.js | 43 |
3 files changed, 66 insertions, 6 deletions
@@ -12,8 +12,6 @@ app.configure(function(){ app.set('view engine', 'jade'); app.use(express.favicon()); app.use(express.logger('dev')); - app.use(express.bodyParser()); - app.use(express.methodOverride()); // Unfuck the stuff that express injects app.use(function (req, res, next) { res.removeHeader("X-Powered-By"); @@ -7,6 +7,7 @@ function EmployeeDao(con) { this.getEmployeesByName = getEmployeesByName; this.getEmployee = getEmployee; this.deleteEmployee = deleteEmployee; + this.insertEmployee = insertEmployee; } function getDepartments(pager, cb) { @@ -124,4 +125,30 @@ function deleteEmployee(emp_no, cb) { }); } +function insertEmployee(birth_date, first_name, last_name, gender, hire_date, cb) { + var con = this.con; + var insert_employee = { + text: 'INSERT INTO employees(birth_date, first_name, last_name, gender, hire_date) VALUES($1, $2, $3, $4, $5)', + values: [] + }; + var employee_currval = { + text: 'SELECT currval(\'employees_seq\') as emp_no', + values: [] + }; + insert_employee.values.push(birth_date); + insert_employee.values.push(first_name); + insert_employee.values.push(last_name); + insert_employee.values.push(gender); + insert_employee.values.push(hire_date); + con.query('begin', function(err, rs) { if(err) return cb(err); + con.query(insert_employee, function(err, rs) { if(err) return cb(err); + con.query(employee_currval, function(err, rs2) { if(err) return cb(err); + con.query('commit', function(err) { if(err) return cb(err); + cb(undefined, rs2.rows[0].emp_no); + }); + }); + }); + }); +} + module.exports = EmployeeDao; 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) { |