author endpoint (show)

This commit is contained in:
Meutel 2014-01-22 18:49:50 +01:00
parent a8b13820f1
commit 369128b0b1
5 changed files with 55 additions and 4 deletions

View File

@ -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"
}

View File

@ -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'}

View File

@ -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'),

50
lib/endpoint/author.js Normal file
View File

@ -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();

View File

@ -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