tasks: add route to list tasks

This commit is contained in:
Girish Ramakrishnan
2018-12-08 20:12:23 -08:00
parent d8225ad653
commit 8502bf4bfa
5 changed files with 92 additions and 4 deletions

View File

@@ -4,7 +4,8 @@ exports = module.exports = {
get: get,
add: add,
update: update,
del: del
del: del,
listPaged: listPaged
};
let assert = require('assert'),
@@ -84,3 +85,31 @@ function del(id, callback) {
callback(null);
});
}
function listPaged(type, page, perPage, callback) {
assert(typeof type === 'string' || type === null);
assert.strictEqual(typeof page, 'number');
assert.strictEqual(typeof perPage, 'number');
assert.strictEqual(typeof callback, 'function');
var data = [];
var query = 'SELECT ' + TASKS_FIELDS + ' FROM tasks';
if (type) {
query += ' WHERE TYPE=?';
data.push(type);
}
query += ' ORDER BY creationTime DESC LIMIT ?,?';
data.push((page-1)*perPage);
data.push(perPage);
database.query(query, data, function (error, results) {
if (error) return callback(new DatabaseError(DatabaseError.INTERNAL_ERROR, error));
results.forEach(postProcess);
callback(null, results);
});
}