aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app.js2
-rw-r--r--lib/dao.js27
-rw-r--r--routes/index.js43
3 files changed, 66 insertions, 6 deletions
diff --git a/app.js b/app.js
index 036fe61..f7dea78 100644
--- a/app.js
+++ b/app.js
@@ -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");
diff --git a/lib/dao.js b/lib/dao.js
index 5ebb38e..8fd6efd 100644
--- a/lib/dao.js
+++ b/lib/dao.js
@@ -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) {