mirror of
https://github.com/lxsang/antd-web-apps
synced 2024-12-26 17:38:21 +01:00
add pagination
This commit is contained in:
parent
d128641998
commit
68284a215d
50
blog/api.lua
50
blog/api.lua
@ -1,5 +1,5 @@
|
||||
local get = {}
|
||||
get.fetch = function(user, cnd, limit)
|
||||
get.fetch = function(user, cnd, limit, order)
|
||||
local db = require("db.model").get(user,"blogs",nil)
|
||||
if not db then return nil end
|
||||
local exp = {}
|
||||
@ -10,10 +10,19 @@ get.fetch = function(user, cnd, limit)
|
||||
|
||||
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
|
||||
cond.limit = limit
|
||||
end
|
||||
if order then
|
||||
cond.order = order
|
||||
end
|
||||
local data, sort = db:find(cond)
|
||||
db:close()
|
||||
return data, sort
|
||||
@ -26,8 +35,26 @@ get.id = function(user, id)
|
||||
return get.fetch(user, { ["="] = { id = id } }, nil)
|
||||
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)
|
||||
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
|
||||
|
||||
get.beforeof = function(user, id, limit)
|
||||
@ -42,9 +69,22 @@ get.prevof = function(user, id)
|
||||
return get.beforeof(user, id, 1)
|
||||
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.."=="))
|
||||
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
|
||||
|
||||
return get
|
||||
|
@ -441,4 +441,19 @@ div.commentform {
|
||||
color:#3170B2;
|
||||
padding-top: 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;
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
BLOG_ROOT = __ROOT__.."/blog"
|
||||
MAX_ENTRY = 10
|
||||
MAX_ENTRY = 15
|
||||
LAST_QUERY = nil
|
||||
local user = "mrsang"
|
||||
local handle = function(p)
|
||||
local args = {}
|
||||
@ -12,6 +13,8 @@ local handle = function(p)
|
||||
end
|
||||
table.sort(sort)
|
||||
local api = require("blog.api")
|
||||
local minid = api.minid(user)
|
||||
local maxid = api.maxid(user)
|
||||
if #args == 0 or api == nil then
|
||||
echo("Unknow request "..p)
|
||||
elseif not api[args[1]] then
|
||||
@ -23,13 +26,13 @@ local handle = function(p)
|
||||
if data == nil then
|
||||
echo("Cannot query data")
|
||||
else
|
||||
require("blog.view").render(action, data, sort)
|
||||
require("blog.view").render(action, data, sort, minid, maxid)
|
||||
end
|
||||
end
|
||||
end
|
||||
std.html()
|
||||
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:")
|
||||
if r then
|
||||
handle(action:sub(s+1))
|
||||
|
@ -9,7 +9,7 @@ view.html = function(name)
|
||||
end
|
||||
end
|
||||
|
||||
view.render = function(action, data, sort)
|
||||
view.render = function(action, data, sort, min, max)
|
||||
local path = BLOG_ROOT.."/view"
|
||||
local fn = nil
|
||||
local e
|
||||
@ -25,7 +25,7 @@ view.render = function(action, data, sort)
|
||||
fn, e = loadscript(path.."/entries.ls")
|
||||
end
|
||||
if fn then
|
||||
local r,o = pcall(fn, data, sort)
|
||||
local r,o = pcall(fn, data, sort, min, max, action)
|
||||
if not r then
|
||||
echo(o)
|
||||
end
|
||||
|
@ -22,7 +22,7 @@
|
||||
<?lua
|
||||
return
|
||||
else
|
||||
data = data[0]
|
||||
data = data[1]
|
||||
content = bytes.__tostring(std.b64decode(data.rendered)):gsub("%%","%%%%")
|
||||
local a,b = content:find("<[Hh]1[^>]*>")
|
||||
if a then
|
||||
|
@ -2,7 +2,12 @@
|
||||
local arg = {...}
|
||||
local datas = arg[1]
|
||||
local order = arg[2]
|
||||
local minid = arg[3]
|
||||
local maxid = arg[4]
|
||||
local action = arg[5]
|
||||
local class = "card"
|
||||
local first_id = nil
|
||||
local last_id = nil
|
||||
if HEADER.mobile then
|
||||
class = "card mobile"
|
||||
end
|
||||
@ -22,6 +27,8 @@
|
||||
|
||||
for idx,v in pairs(order) do
|
||||
local data = datas[v]
|
||||
if not last_id then last_id = data.id end
|
||||
first_id = data.id
|
||||
?>
|
||||
<div class = "<?=class?>">
|
||||
<div class = "side">
|
||||
@ -81,4 +88,22 @@
|
||||
</div>
|
||||
<?lua
|
||||
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>
|
4
index.ls
4
index.ls
@ -12,8 +12,8 @@
|
||||
if db == nil then die("cannot get db data") end
|
||||
local data, a = db:getAll()
|
||||
db:close()
|
||||
if data == nil or data[0] == nil then die("Cannot fetch user info") end
|
||||
data = data[0]
|
||||
if data == nil or data[1] == nil then die("Cannot fetch user info") end
|
||||
data = data[1]
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
@ -18,8 +18,8 @@
|
||||
if db == nil then die("cannot get db data") end
|
||||
local data, a = db:getAll()
|
||||
db:close()
|
||||
if data == nil or data[0] == nil then die("Cannot fetch user info") end
|
||||
data = data[0]
|
||||
if data == nil or data[1] == nil then die("Cannot fetch user info") end
|
||||
data = data[1]
|
||||
?>
|
||||
<html>
|
||||
<head>
|
||||
|
Loading…
Reference in New Issue
Block a user