diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2012-06-29 23:21:43 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2012-06-29 23:21:43 +0200 |
commit | 2a78df9bd4059344b2133830eebe28fccaff8c81 (patch) | |
tree | ec843d1b3201ce654111f3695643e6e614ae3a22 | |
download | example-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.
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | app.js | 29 | ||||
-rw-r--r-- | package.json | 15 | ||||
-rw-r--r-- | public/stylesheets/style.css | 8 | ||||
-rw-r--r-- | routes/index.js | 96 | ||||
-rw-r--r-- | views/department.jade | 11 | ||||
-rw-r--r-- | views/departments.jade | 8 | ||||
-rw-r--r-- | views/employee.jade | 21 | ||||
-rw-r--r-- | views/index.jade | 5 | ||||
-rw-r--r-- | views/layout.jade | 7 |
10 files changed, 201 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules @@ -0,0 +1,29 @@ +var express = require('express') + , routes = require('./routes') + , http = require('http'); + +var app = express(); + +app.configure(function(){ + app.set('port', process.env.PORT || 3000); + app.set('views', __dirname + '/views'); + app.set('view engine', 'jade'); + app.use(express.favicon()); + app.use(express.logger('dev')); + app.use(express.bodyParser()); + app.use(express.methodOverride()); + app.use(app.router); + app.use(express.static(__dirname + '/public')); +}); + +app.configure('development', function(){ + app.use(express.errorHandler()); +}); + +app.get('/', routes.departments); +app.get('/department/:dept_no', routes.department); +app.get('/employee/:emp_no', routes.employee); + +http.createServer(app).listen(app.get('port'), function(){ + console.log("Express server listening on port " + app.get('port')); +}); diff --git a/package.json b/package.json new file mode 100644 index 0000000..9c9b6a1 --- /dev/null +++ b/package.json @@ -0,0 +1,15 @@ +{ + "name": "application-name", + "version": "0.0.1", + "private": true, + "scripts": { + "start": "node app" + }, + "dependencies": { + "collection_json": "git+http://trygvis.dyndns.org/~trygvis/git/2012/06/collection_json.js.git", + "express": "3.0.0beta4", + "jade": "~0.26.3", + "pg": "0.7.2", + "underscore": "~1.3.3" + } +} diff --git a/public/stylesheets/style.css b/public/stylesheets/style.css new file mode 100644 index 0000000..30e047d --- /dev/null +++ b/public/stylesheets/style.css @@ -0,0 +1,8 @@ +body { + padding: 50px; + font: 14px "Lucida Grande", Helvetica, Arial, sans-serif; +} + +a { + color: #00B7FF; +}
\ No newline at end of file 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 + }); + }); + }); +}; diff --git a/views/department.jade b/views/department.jade new file mode 100644 index 0000000..7005ea6 --- /dev/null +++ b/views/department.jade @@ -0,0 +1,11 @@ +extends layout + +block content + h1= title + + h2 Showing #{employees.length} employees + + each employee in employees + table + tr + td: a(href='/employee/#{employee.emp_no}') #{employee.first_name} #{employee.last_name} diff --git a/views/departments.jade b/views/departments.jade new file mode 100644 index 0000000..711e0ec --- /dev/null +++ b/views/departments.jade @@ -0,0 +1,8 @@ +extends layout + +block content + h1= title + each department in departments + table + tr + td: a(href='/department/#{department.dept_no}') #{department.dept_name} diff --git a/views/employee.jade b/views/employee.jade new file mode 100644 index 0000000..6cd2c10 --- /dev/null +++ b/views/employee.jade @@ -0,0 +1,21 @@ +extends layout + +block content + h1= title + + table + tr + td First Name + td #{employee.first_name} + tr + td Last Name + td #{employee.last_name} + tr + td Birth Date + td #{employee.birth_date} + tr + td Gender + td #{employee.gender} + tr + td Hire Date + td #{employee.hire_date} diff --git a/views/index.jade b/views/index.jade new file mode 100644 index 0000000..ef7b09f --- /dev/null +++ b/views/index.jade @@ -0,0 +1,5 @@ +extends layout + +block content + h1= title + p Welcome to #{title}
\ No newline at end of file diff --git a/views/layout.jade b/views/layout.jade new file mode 100644 index 0000000..1b7b305 --- /dev/null +++ b/views/layout.jade @@ -0,0 +1,7 @@ +doctype 5 +html + head + title= title + link(rel='stylesheet', href='/stylesheets/style.css') + body + block content
\ No newline at end of file |