Skip to content

Commit

Permalink
Moved to using Layers module
Browse files Browse the repository at this point in the history
  • Loading branch information
dave-elkan committed Apr 19, 2011
1 parent 11b6215 commit 57ec551
Show file tree
Hide file tree
Showing 16 changed files with 95 additions and 211 deletions.
72 changes: 7 additions & 65 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/**
* Module dependencies.
*/

var express = require('express'),
app = module.exports = express.createServer(),
Db = require('mongodb').Db,
ObjectId = require('mongodb').ObjectId,
Server = require('mongodb').Server,
GenericPool = require('generic-pool'),
notFoundHandler = require("./lib/middleware/NotFoundHandler"),
loader = require("./lib/loader");
Layers = require("layers").Layers,
NotFound = require("./lib/error/NotFound");

app.pool = GenericPool.Pool({
max: 10,
Expand Down Expand Up @@ -46,72 +46,14 @@ app.configure('production', function() {
app.use(express.errorHandler());
});

function getInstanceName(className) {
return className[0].toLowerCase() + className.substr(1);
}

/**
* Services and Controllers.
* Setup Layers.
*
* These services are instantiated here as singletons and attached to the app object.
* This prevents the need for services to instantiate new instances of other services
* (as they can inter-dependent) which would potentially cause circular issues.
* The Layers module automatically loads the controllers, services and views from
* their respective directories
*/
loader(app, "services");
loader(app, "controllers");
loader(app, "views");

var controllers = {},
routes = require('./lib/routes/routes')(app),
baseFielder = {
method: "get",
headers: {
"accept": "text/html"
},
view: app.views.blankView
};

for (var path in routes) {
var fielders = routes[path];
fielders.forEach(setupRoute);
}

function setupRoute(fielder) {
fielder = merge(baseFielder, fielder);
app[fielder.method].call(app, path, [], function(req, res, next) {
for (var view in fielder.views) {
if (req.accepts(view)) {
fielder.action.call(app, req, res, function(error, result) {
if (error) {
throw error;
}
fielder.views[view].render(req, res, result);
});
return;
}
}
});
}

function merge(base, inst) {
var merged = {};
for (var p in base) {
merged[p] = base[p];
}
for (var p in inst) {
merged[p] = inst[p];
}

return merged;
}

function doMerge(target, source) {
for (var p in source) {
target[p] = source[p];
}

return target;
}
var layeredApp = new Layers(app, __dirname + '/lib');
layeredApp.setupRoutes(require('./lib/routes/routes')(app));

// Bootstrap Content
require("./lib/bootstrap")(app);
Expand Down
19 changes: 13 additions & 6 deletions lib/controllers/AuthorController.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
AuthorController = function() {};

AuthorController.prototype.getAuthorList = function(req, res, callback) {
this.services.authorService.getList(callback);
};
AuthorController.prototype = {

displayAuthorList: function(req, res, callback) {
this.services.authorService.getList(callback);
},

AuthorController.prototype.getAuthorByKey = function(req, res, callback) {
var authorKey = req.params.key;
this.services.authorService.getAuthorAndTheirBooks(authorKey, callback);
displayAuthorByKey: function(req, res, callback, next) {
var authorKey = req.params.key;
if (authorKey.indexOf("1") > -1) {
callback(new Error("No ones allowed!"));
} else {
this.services.authorService.getAuthorAndTheirBooks(authorKey, callback);
}
}
};

module.exports = AuthorController;
14 changes: 8 additions & 6 deletions lib/controllers/BookController.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
BookController = function() {};

BookController.prototype.getBookIndex = function(req, res, callback) {
this.services.bookService.getList(callback);
};
BookController.prototype = {

getBookIndex: function(req, res, callback) {
this.services.bookService.getList(callback);
},

BookController.prototype.getBookByKey = function(req, res, callback) {
var bookKey = req.params.key;
this.services.bookService.getBookAndItsAuthor(bookKey, callback);
getBookByKey: function(req, res, callback) {
this.services.bookService.getBookAndItsAuthor(req.params.key, callback);
}
};

module.exports = BookController;
6 changes: 4 additions & 2 deletions lib/controllers/HomeController.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
HomeController = function() {};

HomeController.prototype.getAuthorAndBookList = function(req, res, callback) {
this.services.homeService.getAuthorAndBookList(callback);
HomeController.prototype = {
getAuthorAndBookList: function(req, res, callback) {
this.services.homeService.getAuthorAndBookList(callback);
}
};

module.exports = HomeController;
32 changes: 0 additions & 32 deletions lib/loader.js

This file was deleted.

5 changes: 3 additions & 2 deletions lib/routes/routes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = function(app) {
var controllers = app.controllers,
views = app.views;

return {

"/": [{
Expand Down Expand Up @@ -30,7 +31,7 @@ module.exports = function(app) {
],

"/authors": [{
action: controllers.authorController.getAuthorList,
action: controllers.authorController.displayAuthorList,
views: {
html: views.authorIndexView,
json: views.jsonView
Expand All @@ -39,7 +40,7 @@ module.exports = function(app) {
],

"/author/:key": [{
action: controllers.authorController.getAuthorByKey,
action: controllers.authorController.displayAuthorByKey,
views: {
html: views.authorView
}
Expand Down
20 changes: 12 additions & 8 deletions lib/services/AuthorService.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@ AuthorService.prototype.getAuthorAndTheirBooks = function(authorKey, callback) {
if (error) {
throw error;
}
self.app.services.bookService.getBooksByAuthor(authorKey, function(error, books) {
if (error) {
callback(error);
} else {
author.bookList = books;
callback(null, author);
}
});
if (author) {
self.app.services.bookService.getBooksByAuthor(authorKey, function(error, books) {
if (error) {
callback(error);
} else {
author.bookList = books;
callback(null, author);
}
});
} else {
callback(new self.app.views.NotFound("Author Not Found"));
}
});
};

Expand Down
59 changes: 0 additions & 59 deletions lib/views/AbstractView.js

This file was deleted.

4 changes: 2 additions & 2 deletions lib/views/AuthorView.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var View = require('./AbstractView');
var View = require('./View');

AuthorView = function() {};

AuthorView.prototype = new AbstractView();
AuthorView.prototype = new View();

/**
* Simple example of how the format function can be used.
Expand Down
4 changes: 2 additions & 2 deletions lib/views/BlankView.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var View = require('./AbstractView');
var View = require('./View');

BlankView = function() {};

BlankView.prototype = new AbstractView();
BlankView.prototype = new View();

BlankView.prototype.getTemplate = function() {
return "blank.jade";
Expand Down
4 changes: 2 additions & 2 deletions lib/views/BookView.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var View = require('./AbstractView');
var View = require('./View');

BookView = function() {};

BookView.prototype = new AbstractView();
BookView.prototype = new View();

BookView.prototype.getType = function() {
return "book";
Expand Down
6 changes: 3 additions & 3 deletions lib/views/ErrorView.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var View = require("./AbstractView");
var View = require('./View');

ErrorView = function(options) {
var defaults = {
Expand All @@ -8,12 +8,11 @@ ErrorView = function(options) {
for (var p in options) {
defaults[p] = options[p];
}
this.type = "Error";
this.title = "Error";
this.options = defaults;
};

ErrorView.prototype = new AbstractView();
ErrorView.prototype = new View();

/**
* Renders an Error.
Expand All @@ -34,6 +33,7 @@ ErrorView.prototype.render = function(error, req, res) {
} else {
res.render("error.jade", {
locals: {
type: "Error",
title: outError.message,
error: outError
},
Expand Down
4 changes: 2 additions & 2 deletions lib/views/HomeView.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var View = require('./AbstractView');
var View = require('./View');

HomeView = function() {};

HomeView.prototype = new AbstractView();
HomeView.prototype = new View();

HomeView.prototype.getType = function() {
return "home";
Expand Down
4 changes: 2 additions & 2 deletions lib/views/IndexView.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var View = require('./AbstractView');
var View = require('./View');

IndexView = function() {};

IndexView.prototype = new AbstractView();
IndexView.prototype = new View();

IndexView.prototype.getTemplate = function() {
return "index.jade";
Expand Down
Loading

0 comments on commit 57ec551

Please sign in to comment.