From d8d4a98be0a022cea96f16e73988917df6896c20 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 30 Jun 2012 01:15:10 +0200 Subject: o Adding views for all departments, department, employees in department and employee in both HTML and Collection+JSON variants. --- routes/index.js | 99 +++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 83 insertions(+), 16 deletions(-) (limited to 'routes') diff --git a/routes/index.js b/routes/index.js index 533b7b1..9d0b8ac 100644 --- a/routes/index.js +++ b/routes/index.js @@ -2,8 +2,6 @@ var collection_json = require('collection_json') , pg = require('pg').native , _ = require('underscore'); -var now='2000-01-01'; - function mapRow(row) { var data = _.map(row, function(value, key) { return {name: key, value: value}; @@ -11,7 +9,27 @@ function mapRow(row) { return { data: data }; } -exports.departments = function(req, res){ +function mapDepartments(res, departments) { + return _.map(departments, function(row) { + var item = mapRow(row); + item.href = res.urlgenerator.department(row.dept_no); + return item; + }); +} + +function mapEmployees(res, employees) { + return _.map(employees, function(row) { + var item = mapRow(row); + item.href = res.urlgenerator.employee(row.emp_no); + return item; + }); +} + +function limit(req) { + return "LIMIT " + (_.isString(limit) ? limit : 10); +} + +exports.departments = function(req, res) { pg.connect(process.env.DATABASE_URL, function(err, client) { if(err) throw err; var departments = []; @@ -20,42 +38,89 @@ exports.departments = function(req, res){ departments.push(row); }). on('end', function() { - res.render('departments', { - title: 'Departments', - departments: departments - }); + if(req.accepts('html')) { + res.render('departments', { + title: 'Departments', + departments: departments + }); + } + else { + var c = {collection: { + href: res.urlgenerator.departments(), + items: mapDepartments(res, departments) + }}; + res.contentType('application/vnd.collection+json'); + res.send(JSON.stringify(collection_json.fromObject(c)), 200); + } }); }); }; -exports.department = function(req, res){ +exports.department = function(req, res) { + var dept_no = req.params.dept_no; + if(req.accepts('html')) { + res.render('department', { + title: 'Department ' + dept_no, + urlgenerator: res.urlgenerator, + dept_no: dept_no + }); + } + else { + var c = {collection: { + href: res.urlgenerator.department(dept_no), + links: [ { + rel: 'employees', + prompt: 'Employees in department ' + dept_no, + href: res.urlgenerator.employees_in_department(dept_no) + },{ + rel: 'departments', + prompt: 'All departments', + href: res.urlgenerator.departments() + } ] + }}; + res.contentType('application/vnd.collection+json'); + res.send(collection_json.fromObject(c)); + } +} + +exports.employees_in_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: 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 ]). + 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 ' + limit(req), [ dept_no ]). on('row', function(row) { employees.push(row); }). on('end', function() { if(req.accepts('html')) { - res.render('department', { + res.render('employees_in_department', { title: 'Department ' + dept_no, + dept_no: dept_no, + urlgenerator: res.urlgenerator, employees: employees }); } else { - var object = collection_json.fromObject({}); + var c = {collection: { + href: res.urlgenerator.employees_in_department(dept_no), + links: [ { + rel: 'department', + prompt: 'Departments ' + dept_no, + href: res.urlgenerator.department(dept_no) + } ], + items: mapEmployees(res, employees) + }}; res.contentType('application/vnd.collection+json'); - res.send(object); + res.send(collection_json.fromObject(c)); } }); }); }; -exports.employee = function(req, res){ +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; @@ -72,10 +137,12 @@ exports.employee = function(req, res){ }); } else { - var items = [ mapRow(employee) ]; - var object = collection_json.fromObject({collection: {items: items}}); + var c = {collection: { + href: res.urlgenerator.employee(emp_no), + items: [ mapRow(employee) ], + }}; res.contentType('application/vnd.collection+json'); - res.send(JSON.stringify(object), 200); + res.send(JSON.stringify(collection_json.fromObject(c)), 200); } }); }); -- cgit v1.2.3