2014-01-22 17:49:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Endpoint author.
|
|
|
|
*/
|
|
|
|
var Endpoint = require('./endpoint.js');
|
2014-01-22 19:20:43 +00:00
|
|
|
|
2014-01-22 17:49:50 +00:00
|
|
|
function Author() {
|
|
|
|
Endpoint.call(this);
|
|
|
|
this.authorId = null;
|
|
|
|
}
|
|
|
|
Author.prototype = Object.create(Endpoint.prototype, {
|
|
|
|
|
|
|
|
bind: {
|
2014-01-22 21:08:21 +00:00
|
|
|
value: function(action, params, callback) {
|
2014-01-22 17:49:50 +00:00
|
|
|
switch (action.name) {
|
|
|
|
case 'show':
|
|
|
|
action.resId = this.authorId;
|
2014-01-22 21:08:21 +00:00
|
|
|
action.getRelated = function(res, relcback){
|
|
|
|
// books of this autor
|
|
|
|
// TODO authors writing same book
|
|
|
|
relcback(null, new Array({ type: 'books', path: '/books?author='+this.resId }));
|
|
|
|
};
|
|
|
|
action.loadResource = function(resId, loadcback) {
|
2014-01-22 17:49:50 +00:00
|
|
|
logger.debug('loading author ' + resId);
|
2014-01-22 19:20:43 +00:00
|
|
|
db.get('SELECT * FROM authors WHERE id = '+ resId, function(err, row) {
|
2014-01-22 21:08:21 +00:00
|
|
|
loadcback(err, row);
|
2014-01-22 17:49:50 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
callback(null, action);
|
2014-01-22 18:38:47 +00:00
|
|
|
break;
|
|
|
|
case 'list':
|
2014-01-22 21:08:21 +00:00
|
|
|
//TODO related
|
2014-01-22 18:38:47 +00:00
|
|
|
action.loadResources = function(onload, onend) {
|
2014-01-22 21:08:21 +00:00
|
|
|
var query = 'SELECT * FROM authors ';
|
2014-01-22 19:39:31 +00:00
|
|
|
if (!this.perPage) this.perPage = 30;
|
|
|
|
//TODO sanitize
|
|
|
|
if (!this.page) this.page = 0;
|
2014-01-22 21:08:21 +00:00
|
|
|
query += ' LIMIT ? OFFSET ?';
|
2014-01-22 19:39:31 +00:00
|
|
|
db.each(query, this.perPage, this.page*this.perPage, function (err, row) {
|
2014-01-22 19:20:43 +00:00
|
|
|
onload(err, row);
|
2014-01-22 19:39:31 +00:00
|
|
|
}, function(err) {
|
|
|
|
//TODO err
|
|
|
|
if (err) logger.error(err);
|
2014-01-22 19:20:43 +00:00
|
|
|
onend();
|
|
|
|
});
|
2014-01-22 18:38:47 +00:00
|
|
|
};
|
|
|
|
callback(null, action);
|
|
|
|
break;
|
2014-01-22 17:49:50 +00:00
|
|
|
default:
|
|
|
|
callback(new Error('action not implemented'));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
writable: true
|
|
|
|
},
|
|
|
|
targetCollection : {
|
|
|
|
value: function(pathname) {
|
|
|
|
var match = PATH_RE.exec(pathname);
|
2014-01-22 18:38:47 +00:00
|
|
|
logger.debug('pathname ' + pathname + ' => ' + match);
|
|
|
|
if (match.length > 2 && match[2]) {
|
2014-01-22 17:49:50 +00:00
|
|
|
// TODO check integer
|
|
|
|
this.authorId = match[2];
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
writable: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
exports = module.exports = new Author();
|