From 369128b0b120795103b63707e5180953e6af0c6e Mon Sep 17 00:00:00 2001 From: Meutel Date: Wed, 22 Jan 2014 18:49:50 +0100 Subject: [PATCH] author endpoint (show) --- config/config.json | 3 ++- lib/action/action.js | 1 + lib/bouquins.js | 1 + lib/endpoint/author.js | 50 ++++++++++++++++++++++++++++++++++++++++++ lib/router/router.js | 4 +--- 5 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 lib/endpoint/author.js diff --git a/config/config.json b/config/config.json index bb1e97c..661778f 100644 --- a/config/config.json +++ b/config/config.json @@ -2,7 +2,8 @@ "httpPort":8080, "debugLevel":"debug", "endpoints": { - "library":"endpoint/library.js" + "library":"endpoint/library.js", + "author":"endpoint/author.js" }, "urlPrefix": "http://127.0.0.1:8080/bouquins" } diff --git a/lib/action/action.js b/lib/action/action.js index c987e60..625ab0f 100644 --- a/lib/action/action.js +++ b/lib/action/action.js @@ -108,6 +108,7 @@ ShowAction.prototype = Object.create(Action.prototype, { if (this.resId) { this.loadResource(this.resId, function(err, res) { self.res = res; + logger.debug('resource loaded: ' + res); //TODO err var link = ''; var rels = self.getRelated(res); // { type: 'author', path: '/author/id'} diff --git a/lib/bouquins.js b/lib/bouquins.js index bae720b..2872a76 100644 --- a/lib/bouquins.js +++ b/lib/bouquins.js @@ -2,6 +2,7 @@ * TODO license * Bouquins module. */ +GLOBAL.PATH_RE=/\/([a-zA-Z0-9]+)(?:\/|$)([a-zA-Z0-9]+)?/; var config = require('./util/config'), logger = require('./util/logger'), diff --git a/lib/endpoint/author.js b/lib/endpoint/author.js new file mode 100644 index 0000000..85ece18 --- /dev/null +++ b/lib/endpoint/author.js @@ -0,0 +1,50 @@ + +/** + * Endpoint author. + */ +var Endpoint = require('./endpoint.js'); +function Author() { + Endpoint.call(this); + this.authorId = null; +} +Author.prototype = Object.create(Endpoint.prototype, { + + bind: { + value: function(action, callback) { + switch (action.name) { + case 'show': + action.resId = this.authorId; + action.loadResource = function(resId, callback) { + logger.debug('loading author ' + resId); + // TODO load from db + callback(null, { + id: resId, + name: 'Test test' + }); + }; + callback(null, action); + break; + default: + callback(new Error('action not implemented')); + } + }, + enumerable: true, + configurable: true, + writable: true + }, + targetCollection : { + value: function(pathname) { + var match = PATH_RE.exec(pathname); + if (match.length > 2) { + // TODO check integer + this.authorId = match[2]; + return false; + } + return true; + }, + enumerable: true, + configurable: true, + writable: true + } +}); +exports = module.exports = new Author(); diff --git a/lib/router/router.js b/lib/router/router.js index b030c28..235ed81 100644 --- a/lib/router/router.js +++ b/lib/router/router.js @@ -6,7 +6,6 @@ var util = require('util') Endpoint = require('../endpoint/endpoint.js'), Outputter = require('../outputter/outputter'); -var PATH_RE=/\/([a-zA-Z0-9]+)(?:\/|$)([a-zA-Z0-9]+)?/; exports = module.exports = Router; function Router() { @@ -95,9 +94,8 @@ Router.prototype = { //TODO err if (err) { logger.error(err); - // TODO write error in req + // TODO write error in response } - //TODO endpoint.buildAction(req.method, url, function(err, action) { //TODO err