1
0
mirror of https://github.com/lxsang/antd-web-apps synced 2024-11-20 02:18:20 +01:00

add pagination

This commit is contained in:
Xuan Sang LE 2018-03-07 14:18:25 +01:00
parent d128641998
commit 68284a215d
8 changed files with 98 additions and 15 deletions

View File

@ -1,5 +1,5 @@
local get = {} local get = {}
get.fetch = function(user, cnd, limit) get.fetch = function(user, cnd, limit, order)
local db = require("db.model").get(user,"blogs",nil) local db = require("db.model").get(user,"blogs",nil)
if not db then return nil end if not db then return nil end
local exp = {} local exp = {}
@ -10,10 +10,19 @@ get.fetch = function(user, cnd, limit)
end end
local cond = { exp = {["and"] = exp }, order = { ctime = "DESC" }} local cond = {
exp = {["and"] = exp },
order = { ctime = "DESC" },
fields = {
"id", "title", "utime", "ctime", "utimestr", "ctimestr", "rendered", "tags"
}
}
if limit then if limit then
cond.limit = limit cond.limit = limit
end end
if order then
cond.order = order
end
local data, sort = db:find(cond) local data, sort = db:find(cond)
db:close() db:close()
return data, sort return data, sort
@ -26,8 +35,26 @@ get.id = function(user, id)
return get.fetch(user, { ["="] = { id = id } }, nil) return get.fetch(user, { ["="] = { id = id } }, nil)
end end
get.minid = function(user)
local db = require("db.model").get(user,"blogs",nil)
local cond = { fields = { "MIN(id)" } }
local data = db:find(cond)
db:close()
return data[1]["MIN(id)"]
end
get.maxid = function(user)
local db = require("db.model").get(user,"blogs",nil)
local cond = { fields = { "MAX(id)" }}
local data = db:find(cond)
db:close()
return data[1]["MAX(id)"]
end
get.afterof = function(user, id, limit) get.afterof = function(user, id, limit)
return get.fetch(user, { [">"] = { id = id } }, limit) local data, sort = get.fetch(user, { [">"] = { id = id } }, limit, { ctime = "ASC" })
table.sort(sort, function(a, b) return a > b end)
return data, sort
end end
get.beforeof = function(user, id, limit) get.beforeof = function(user, id, limit)
@ -42,9 +69,22 @@ get.prevof = function(user, id)
return get.beforeof(user, id, 1) return get.beforeof(user, id, 1)
end end
get.bytag = function(user, b64tag, limit) get.bytag = function(user, b64tag, limit, action, id)
LAST_QUERY = b64tag
local tag = bytes.__tostring(std.b64decode(b64tag.."==")) local tag = bytes.__tostring(std.b64decode(b64tag.."=="))
return get.fetch(user, { ["LIKE"] = { tags = "%%"..tag.."%%" } }, limit) local cond = { ["LIKE"] = { tags = "%%"..tag.."%%" } }
local order = nil
if action == "before" then
cond = { ["and"] = { cond, { ["<"] = {id = id} } } }
elseif action == "after" then
cond = { ["and"] = { cond, { [">"] = {id = id} } } }
order = { ctime = "ASC" }
end
local data, sort = get.fetch(user, cond, limit, order)
if(action == "after") then
table.sort(sort, function(a, b) return a > b end)
end
return data, sort
end end
return get return get

View File

@ -442,3 +442,18 @@ div.commentform {
padding-top: 10px; padding-top: 10px;
padding-bottom: 10px; padding-bottom: 10px;
} }
div.time-travel{
margin-top: 10px;
display: flex;
flex-direction: row;
text-align: center;
margin-bottom: 10px;
}
div.time-travel a{
font-weight: bold;
font-size: 16px;
text-decoration: none;
flex:1;
color:#3170B2;
}

View File

@ -1,5 +1,6 @@
BLOG_ROOT = __ROOT__.."/blog" BLOG_ROOT = __ROOT__.."/blog"
MAX_ENTRY = 10 MAX_ENTRY = 15
LAST_QUERY = nil
local user = "mrsang" local user = "mrsang"
local handle = function(p) local handle = function(p)
local args = {} local args = {}
@ -12,6 +13,8 @@ local handle = function(p)
end end
table.sort(sort) table.sort(sort)
local api = require("blog.api") local api = require("blog.api")
local minid = api.minid(user)
local maxid = api.maxid(user)
if #args == 0 or api == nil then if #args == 0 or api == nil then
echo("Unknow request "..p) echo("Unknow request "..p)
elseif not api[args[1]] then elseif not api[args[1]] then
@ -23,13 +26,13 @@ local handle = function(p)
if data == nil then if data == nil then
echo("Cannot query data") echo("Cannot query data")
else else
require("blog.view").render(action, data, sort) require("blog.view").render(action, data, sort, minid, maxid)
end end
end end
end end
std.html() std.html()
local action = REQUEST.query.action local action = REQUEST.query.action
if not action then action = "r:top:10" end if not action then action = "r:top:"..MAX_ENTRY end
local r, s = action:find("^r:") local r, s = action:find("^r:")
if r then if r then
handle(action:sub(s+1)) handle(action:sub(s+1))

View File

@ -9,7 +9,7 @@ view.html = function(name)
end end
end end
view.render = function(action, data, sort) view.render = function(action, data, sort, min, max)
local path = BLOG_ROOT.."/view" local path = BLOG_ROOT.."/view"
local fn = nil local fn = nil
local e local e
@ -25,7 +25,7 @@ view.render = function(action, data, sort)
fn, e = loadscript(path.."/entries.ls") fn, e = loadscript(path.."/entries.ls")
end end
if fn then if fn then
local r,o = pcall(fn, data, sort) local r,o = pcall(fn, data, sort, min, max, action)
if not r then if not r then
echo(o) echo(o)
end end

View File

@ -22,7 +22,7 @@
<?lua <?lua
return return
else else
data = data[0] data = data[1]
content = bytes.__tostring(std.b64decode(data.rendered)):gsub("%%","%%%%") content = bytes.__tostring(std.b64decode(data.rendered)):gsub("%%","%%%%")
local a,b = content:find("<[Hh]1[^>]*>") local a,b = content:find("<[Hh]1[^>]*>")
if a then if a then

View File

@ -2,7 +2,12 @@
local arg = {...} local arg = {...}
local datas = arg[1] local datas = arg[1]
local order = arg[2] local order = arg[2]
local minid = arg[3]
local maxid = arg[4]
local action = arg[5]
local class = "card" local class = "card"
local first_id = nil
local last_id = nil
if HEADER.mobile then if HEADER.mobile then
class = "card mobile" class = "card mobile"
end end
@ -22,6 +27,8 @@
for idx,v in pairs(order) do for idx,v in pairs(order) do
local data = datas[v] local data = datas[v]
if not last_id then last_id = data.id end
first_id = data.id
?> ?>
<div class = "<?=class?>"> <div class = "<?=class?>">
<div class = "side"> <div class = "side">
@ -81,4 +88,22 @@
</div> </div>
<?lua <?lua
end end
local beforelk = "./r:beforeof:"..first_id..":"..MAX_ENTRY
local afterlk = "./r:afterof:"..last_id..":"..MAX_ENTRY
if action == "bytag" or action == "search" then
beforelk = "./r:"..action..":"..LAST_QUERY..":"..MAX_ENTRY..":before:"..first_id
afterlk = "./r:"..action..":"..LAST_QUERY..":"..MAX_ENTRY..":after:"..last_id
end
?> ?>
<div class = "time-travel">
<?lua
if first_id ~= minid then
?>
<a href = "<?=beforelk?>" class = "past"><< Older posts</a>
<?lua
end
if last_id ~= maxid then
?>
<a href = "<?=afterlk?>" class = "future">Newer posts >></a>
<?lua end ?>
</div>

View File

@ -12,8 +12,8 @@
if db == nil then die("cannot get db data") end if db == nil then die("cannot get db data") end
local data, a = db:getAll() local data, a = db:getAll()
db:close() db:close()
if data == nil or data[0] == nil then die("Cannot fetch user info") end if data == nil or data[1] == nil then die("Cannot fetch user info") end
data = data[0] data = data[1]
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">

View File

@ -18,8 +18,8 @@
if db == nil then die("cannot get db data") end if db == nil then die("cannot get db data") end
local data, a = db:getAll() local data, a = db:getAll()
db:close() db:close()
if data == nil or data[0] == nil then die("Cannot fetch user info") end if data == nil or data[1] == nil then die("Cannot fetch user info") end
data = data[0] data = data[1]
?> ?>
<html> <html>
<head> <head>