pagination

This commit is contained in:
Flummi
2016-08-30 10:50:06 +00:00
parent 0621234e50
commit 852af35e46
5 changed files with 63 additions and 9 deletions

View File

@ -50,10 +50,14 @@ function Websrv(tbot, tsql, tlib) {
else if(filePath == "./test") { // (test)mainpage
var tpl = swig.compile(templates.test);
var data = { items: [] };
sql.query("select `id`,`mime` from `f0ck`.`items` order by `id` desc limit 20", (err, rows, fields) => {
var data = {
items: [],
last: 10000
};
sql.query("select `id`,`mime` from `f0ck`.`items` order by `id` desc limit 100", (err, rows, fields) => {
rows.forEach((e,i,a) => {
data.items.push({ "id": e.id, "mime": e.mime });
data.last = e.id;
});
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(tpl(data), 'utf-8');
@ -214,6 +218,26 @@ function Websrv(tbot, tsql, tlib) {
res.end(JSON.stringify(items), 'utf-8');
});
}
else if(url[2] == "p" && Number.isInteger(parseInt(url[3]))) { // pagination
var eps = 50;
var id = url[3];
sql.query("select * from `f0ck`.`items` where `id` < ? order by `id` desc limit ?", [id, eps], (err, rows, fields) => {
var items = {
"items": [],
"last": id
};
rows.forEach((e,i,a) => {
items.items.push({
'id': e.id,
'mime': e.mime
});
items.last = e.id;
});
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(JSON.stringify(items), 'utf-8');
});
}
else if(Number.isInteger(parseInt(url[2]))) { // Item
var query = "select * from `f0ck`.`items` where `id` = ? limit 1; " // get item
+ "select `id` from `f0ck`.`items` where `id` = (select min(`id`) from `f0ck`.`items` where `id` > ?); " // get previous item