aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app.js21
-rw-r--r--routes/index.js99
-rw-r--r--views/department.jade7
-rw-r--r--views/employees_in_department.jade13
4 files changed, 119 insertions, 21 deletions
diff --git a/app.js b/app.js
index a2e82ce..54dbd44 100644
--- a/app.js
+++ b/app.js
@@ -12,6 +12,7 @@ app.configure(function(){
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
+ app.use(urlgenerator);
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
@@ -20,8 +21,28 @@ app.configure('development', function(){
app.use(express.errorHandler());
});
+function urlgenerator(req, res, next) {
+ var host = req.headers.host;
+ res.urlgenerator = {
+ departments: function() {
+ return 'http://' + host + '/';
+ },
+ department: function(dept_no) {
+ return 'http://' + host + '/department/' + dept_no;
+ },
+ employees_in_department: function(dept_no) {
+ return 'http://' + host + '/department/' + dept_no + '/employees';
+ },
+ employee: function(emp_no) {
+ return 'http://' + host + '/employee/' + emp_no;
+ }
+ };
+ next();
+}
+
app.get('/', routes.departments);
app.get('/department/:dept_no', routes.department);
+app.get('/department/:dept_no/employees', routes.employees_in_department);
app.get('/employee/:emp_no', routes.employee);
http.createServer(app).listen(app.get('port'), function(){
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);
}
});
});
diff --git a/views/department.jade b/views/department.jade
index 7005ea6..e2a5bde 100644
--- a/views/department.jade
+++ b/views/department.jade
@@ -3,9 +3,6 @@ extends layout
block content
h1= title
- h2 Showing #{employees.length} employees
+ p: a(href=urlgenerator.departments()) All departments
- each employee in employees
- table
- tr
- td: a(href='/employee/#{employee.emp_no}') #{employee.first_name} #{employee.last_name}
+ p: a(href=urlgenerator.employees_in_department(dept_no)) Employees
diff --git a/views/employees_in_department.jade b/views/employees_in_department.jade
new file mode 100644
index 0000000..f835a29
--- /dev/null
+++ b/views/employees_in_department.jade
@@ -0,0 +1,13 @@
+extends layout
+
+block content
+ h1= title
+
+ p: a(href=urlgenerator.department(dept_no)) Department #{dept_no}
+
+ p This department has #{employees.length} employees.
+
+ each employee in employees
+ table
+ tr
+ td: a(href=urlgenerator.employee(employee.emp_no)) #{employee.first_name} #{employee.last_name}