aboutsummaryrefslogtreecommitdiff
path: root/routes
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2012-06-29 23:21:43 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2012-06-29 23:21:43 +0200
commit2a78df9bd4059344b2133830eebe28fccaff8c81 (patch)
treeec843d1b3201ce654111f3695643e6e614ae3a22 /routes
downloadexample-collection-json-db-2a78df9bd4059344b2133830eebe28fccaff8c81.tar.gz
example-collection-json-db-2a78df9bd4059344b2133830eebe28fccaff8c81.tar.bz2
example-collection-json-db-2a78df9bd4059344b2133830eebe28fccaff8c81.tar.xz
example-collection-json-db-2a78df9bd4059344b2133830eebe28fccaff8c81.zip
o Initial import of a application/vnd.collection+json demo application.
Diffstat (limited to 'routes')
-rw-r--r--routes/index.js96
1 files changed, 96 insertions, 0 deletions
diff --git a/routes/index.js b/routes/index.js
new file mode 100644
index 0000000..0ecec65
--- /dev/null
+++ b/routes/index.js
@@ -0,0 +1,96 @@
+var collection_json = require('collection_json')
+ , pg = require('pg').native
+ , _ = require('underscore');
+
+var now='2000-01-01';
+
+function mapRow(row) {
+ console.log('mapRow', row);
+ var data = _.map(row, function(value, key) {
+ return {name: key, value: value};
+ });
+ console.log('mapRow', row, data);
+ return { data: data };
+}
+
+function mapEmployee(row) {
+ return {
+ data: [
+ {name: 'first_name', value: row.first_name}
+ ]
+ };
+}
+
+exports.departments = function(req, res){
+ pg.connect(process.env.DATABASE_URL, function(err, client) {
+ if(err) throw err;
+ var departments = [];
+ client.query('SELECT dept_no, dept_name FROM departments').
+ on('row', function(row) {
+ departments.push(row);
+ }).
+ on('end', function() {
+ client.end();
+ res.render('departments', {
+ title: 'Departments',
+ departments: departments
+ });
+ });
+ });
+};
+
+exports.department = function(req, res){
+ pg.connect(process.env.DATABASE_URL, function(err, client) {
+ if(err) throw err;
+ var dept_no = req.params.dept_no;
+ var employees = [];
+ // TODO:
+ // TODO: Add dept_name to view
+ // TODO: Add manager as a link
+ client.query('SELECT employees.* FROM employees, dept_emp WHERE employees.emp_no=dept_emp.emp_no AND dept_emp.dept_no=$1 AND dept_emp.to_date > now() AND now() > dept_emp.from_date', [ dept_no ]).
+ on('row', function(row) {
+ employees.push(row);
+ }).
+ on('end', function() {
+ client.end();
+ if(req.accepts('application/vnd.collection+json')) {
+ var object = collection_json.fromObject({});
+ res.contentType('application/vnd.collection+json');
+ res.send(object);
+ }
+ else
+ res.render('department', {
+ title: 'Department ' + dept_no,
+ employees: employees
+ });
+ });
+ });
+};
+
+exports.employee = function(req, res){
+ pg.connect(process.env.DATABASE_URL, function(err, client) {
+ if(err) throw err;
+ var emp_no = req.params.emp_no;
+ var employee;
+ client.query('SELECT * FROM employees WHERE employees.emp_no=$1', [ emp_no ]).
+ on('row', function(row) {
+ employee = row;
+ }).
+ on('end', function() {
+ client.end();
+ if(req.accepts('application/vnd.collection+json')) {
+ var items = [ mapRow(employee) ];
+ console.log('items', items);
+ var object = collection_json.fromObject({collection: {items: items}});
+ console.log('object', object);
+ res.contentType('application/vnd.collection+json');
+ res.send(JSON.stringify(object), 200);
+ }
+ else
+ res.render('employee', {
+ title: 'Employee ' + emp_no,
+ employee: employee
+ });
+ });
+ });
+};