endpoints serie tag

This commit is contained in:
Meutel 2014-01-22 22:28:19 +01:00
parent a609d80de3
commit 8be80743b4
3 changed files with 149 additions and 1 deletions

View File

@ -4,7 +4,9 @@
"endpoints": {
"library":"endpoint/library.js",
"book":"endpoint/book.js",
"author":"endpoint/author.js"
"author":"endpoint/author.js",
"serie":"endpoint/serie.js",
"tag":"endpoint/tag.js"
},
"urlPrefix": "http://127.0.0.1:8080",
"dbfile": "/home/meutel/metadata.db"

85
lib/endpoint/serie.js Normal file
View File

@ -0,0 +1,85 @@
/**
* Endpoint serie.
*/
var Endpoint = require('./endpoint.js');
function Serie() {
Endpoint.call(this);
this.serieId = null;
}
Serie.prototype = Object.create(Endpoint.prototype, {
bind: {
value: function(action, params, callback) {
switch (action.name) {
case 'show':
action.resId = this.serieId;
action.getRelated = function(res,relcback){
var rels = new Array();
var nbrels = 2; // number of requests for rel
// call callback when no more requests running
var endrels = function(err) {
nbrels--;
if (err) logger.error(err);
if (nbrels == 0) relcback(null, rels);
};
// author
var raq = 'SELECT DISTINCT author FROM books_series_link AS bsl, books_authors_link AS bal WHERE bsl.book = bal.book AND bsl.series = ?';
db.each(raq, res.id, function (err, row) {
if (!err && row.author)
rels.push({ type: 'author', path: '/author/'+row.author });
}, endrels);
// books
var rbq = 'SELECT * FROM books_series_link WHERE series = ?';
db.each(rbq, res.id, function (err, row) {
if (!err && row.book)
rels.push({ type: 'book', path: '/book/'+row.book });
}, endrels);
};
action.loadResource = function(resId, callback) {
logger.debug('loading serie ' + resId);
db.get('SELECT * FROM series WHERE id = '+ resId, function(err, row) {
callback(err, row);
});
};
callback(null, action);
break;
case 'list':
//TODO related
action.loadResources = function(onload, onend) {
var query = 'SELECT * FROM series ';
//TODO factorize
if (!this.perPage) this.perPage = 30;
//TODO sanitize
if (!this.page) this.page = 0;
query += ' LIMIT ? OFFSET ?';
db.each(query, this.perPage, this.page*this.perPage, function (err, row) {
onload(err, row);
}, function(err) {
//TODO err
if (err) logger.error(err);
onend();
});
};
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);
logger.debug('pathname ' + pathname + ' => ' + match);
if (match.length > 2 && match[2]) {
// TODO check integer
this.serieId = match[2];
return false;
}
return true;
}, enumerable: true, configurable: true, writable: true
}
});
exports = module.exports = new Serie();

61
lib/endpoint/tag.js Normal file
View File

@ -0,0 +1,61 @@
/**
* Endpoint tag.
*/
var Endpoint = require('./endpoint.js');
function Tag() {
Endpoint.call(this);
this.tagId = null;
}
Tag.prototype = Object.create(Endpoint.prototype, {
bind: {
value: function(action, params, callback) {
switch (action.name) {
case 'show':
action.resId = this.tagId;
action.loadResource = function(resId, callback) {
logger.debug('loading tag ' + resId);
db.get('SELECT * FROM tags WHERE id = '+ resId, function(err, row) {
callback(err, row);
});
};
callback(null, action);
break;
case 'list':
action.loadResources = function(onload, onend) {
var query = 'SELECT * FROM tags ';
//TODO factorize
if (!this.perPage) this.perPage = 30;
//TODO sanitize
if (!this.page) this.page = 0;
query += ' LIMIT ? OFFSET ?';
db.each(query, this.perPage, this.page*this.perPage, function (err, row) {
onload(err, row);
}, function(err) {
//TODO err
if (err) logger.error(err);
onend();
});
};
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);
logger.debug('pathname ' + pathname + ' => ' + match);
if (match.length > 2 && match[2]) {
// TODO check integer
this.tagId = match[2];
return false;
}
return true;
}, enumerable: true, configurable: true, writable: true
}
});
exports = module.exports = new Tag();