Commit 1e19f887 authored by Marco Flowers's avatar Marco Flowers

Start of id pagination

Using stream ids to paginate searches through the stream.
parent f8998e0e
......@@ -59,11 +59,11 @@ function respond(res, err, rows, result) {
app.get('/streams/', function(req,res){
res.set('Content-Type', 'application/json');
var params = req.query;
streamsFindBy(params.format, params.genre, params.q, params.order, params.limit, 1, function(err, rows){
if(err || rows[0].array_to_json == null) {
streamsFindBy(params.format, params.genre, params.q, params.order, params.limit, params.next, params.prev, 1, function(err, rows){
if(err || rows[0].streams == null) {
res.send([]);
} else {
res.send(rows[0].array_to_json);
res.send(rows[0].streams);
}
});
});
......
......@@ -9,7 +9,9 @@ function init(q, c, s) {
function byFormat(req, res) {
var format = req.param("format");
streamsFindBy(format, undefined, undefined, 1, 10, 0, function(err, rows) {
var next = req.param("next");
var prev = req.param("prev");
streamsFindBy(format, undefined, undefined, 2, 10, next, prev, 0, function(err, rows) {
if (err) {
res.send(503);
} else {
......@@ -19,10 +21,19 @@ function byFormat(req, res) {
} else {
error = false;
}
var next_url, prev_url;
if (rows.length == 10) {
var last_id = rows[9].id;
var prev_id = rows[0].id;
next_url = req.path+'?next='+last_id;
prev_url = req.path+'?prev='+prev_id;
}
res.render("by_xx", {
title: format,
servers: rows,
error: error
error: error,
next: next_url,
prev: prev_url
});
}
});
......
......@@ -9,7 +9,10 @@ function init(q, c, s) {
function byGenre(req, res) {
var genre = req.param("genre");
streamsFindBy(undefined, genre, undefined, 1, 10, 0, function(err, rows) {
var next = req.param("next");
var prev = req.param("prev");
streamsFindBy(undefined, genre, undefined, 2, 10, next, prev, 0, function(err, rows) {
console.log(req);
if (err) {
res.send(503);
} else {
......@@ -19,10 +22,19 @@ function byGenre(req, res) {
} else {
error = false;
}
var next_url, prev_url;
if (rows.length == 10) {
var last_id = rows[9].id;
var prev_id = rows[0].id;
next_url = req.path+'?next='+last_id;
prev_url = req.path+'?prev='+prev_id;
}
res.render("by_xx", {
title: genre,
servers: rows,
error: error
error: error,
next: next_url,
prev: prev_url
});
}
});
......
......@@ -8,7 +8,7 @@ function init(q, c, s) {
}
function index(req, res) {
streamsFindBy(undefined, undefined, undefined, 0, 20, 0, function(err, rows) {
streamsFindBy(undefined, undefined, undefined, 0, 20, undefined, undefined, 0, function(err, rows) {
if (err) {
res.send(503);
} else {
......
......@@ -8,12 +8,12 @@ function init(q, c) {
}
function getCachedStreams(format, genre, q, order, limit, json, cb) {
var cacheString = JSON.stringify({"format":format,"genre":genre,"q":q,"order":order,"limit":limit,"json":json});
function getCachedStreams(format, genre, q, order, limit, next, prev, json, cb) {
var cacheString = JSON.stringify({"format":format, "genre":genre, "q":q, "order":order, "limit":limit, "next":next, "prev":prev, "json":json});
console.log(cacheString);
cache.wrap(cacheString, function (_cb) {
var params = JSON.parse(cacheString);
findBy(params.format, params.genre, params.q, params.order, params.limit, params.json, _cb);
findBy(params.format, params.genre, params.q, params.order, params.limit, params.next, params.prev, params.json, _cb);
}, 5, cb);
}
......@@ -26,11 +26,11 @@ function getCachedStreams(format, genre, q, order, limit, json, cb) {
Turns parameters into a sql query on streams
*/
function findBy(format, genre, q, order, limit, json, resultCallback)
function findBy(format, genre, q, order, limit, next, prev, json, resultCallback)
{
console.log(format);
console.log(genre);
var queryStringJsonBeg = 'SELECT array_to_json(array_agg(row_to_json(t))) FROM (';
var queryStringJsonBeg = 'SELECT array_to_json(array_agg(row_to_json(t))) AS streams FROM (';
var queryStringJsonEnd = ' ) t';
var queryStringBeg = 'SELECT s.id, s.stream_name, s.stream_type, s.description \
,s.songname, s.url, s.avg_listening_time, s.codec_sub_types, s.bitrate, s.hits, \
......@@ -104,15 +104,45 @@ function findBy(format, genre, q, order, limit, json, resultCallback)
return;
}
}
if(next && (order ==1 || order ==2)) {
searchString = 'WHERE ';
if(countItems > 1) {
searchString = 'AND ';
}
var sign;
if( order==1 ){
sign = '<';
} else {
sign = '>';
}
queryString += searchString + 's.id ' + sign + ' ' + '$' + countItems + ' ';
items.push(next);
countItems++;
}
else if(prev && (order ==1 || order ==2)) {
searchString = 'WHERE ';
if(countItems > 1) {
searchString = 'AND ';
}
var sign;
if( order==1 ){
sign = '>';
} else {
sign = '<';
}
queryString += searchString + 's.id ' + sign + ' ' + '$' + countItems + ' ';
items.push(prev);
countItems++;
}
// add in the Group BY
queryString = queryString + queryStringGroup;
if(order) {
if(order == 0) {
queryString += 'ORDER BY random() ';
} else if(order == 1) {
queryString += 'ORDER BY listeners DESC ';
queryString += 'ORDER BY s.id desc ';
} else if(order == 2) {
queryString += 'ORDER BY listeners ASC ';
queryString += 'ORDER BY s.id asc ';
}
}
if(limit) {
......@@ -124,6 +154,8 @@ function findBy(format, genre, q, order, limit, json, resultCallback)
queryString += queryStringJsonEnd;
}
console.log(queryString);
console.log(items)
console.log(resultCallback)
query(queryString, items, resultCallback);
}
......
......@@ -13,6 +13,12 @@
{% for server in servers %}
{% include "./stream.html" %}
{% endfor %}
{% if prev %}
<a href="{{prev}}">Prev</a>
{% endif %}
{% if next %}
<a href="{{next}}">Next</a>
{% endif %}
{% endif %}
</div>
{% endblock %}
\ No newline at end of file
{% endblock %}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment