smr/src/lua/endpoints/search_get.lua

69 lines
1.6 KiB
Lua

local sql = require("lsqlite3")
local db = require("db")
local queries = require("queries")
local util = require("util")
local libtags = require("tags")
local pages = require("pages")
local config = require("config")
local search_parser = require("parser_search")
local stmnt_search
local oldconfigure = configure
function configure(...)
stmnt_search = assert(db.conn:prepare(queries.select_post_tags))
return oldconfigure(...)
end
local function search_get(req)
local host = http_request_get_host(req)
local path = http_request_get_path(req)
http_request_populate_qs(req)
local searchq, err = http_argument_get_string(req,"q")
if not searchq then
local ret = pages.search{
domain = config.domain,
results = {},
q = "",
}
http_response(req,200,ret)
return
end
log(LOG_DEBUG,string.format("search: %q",searchq))
local sqltxt, data = search_parser(searchq)
local stmnt = assert(db.conn:prepare(sqltxt), db.conn:errmsg())
local i = 1
for field,values in pairs(data) do
if field ~= "tags" then
for _, value in pairs(values) do
stmnt:bind(i,value[3])
i = i + 1
end
end
end
for _,value in pairs(data.tags) do
stmnt:bind(i,value[3])
i = i + 1
end
local results = {}
for row in stmnt:rows() do
table.insert(results,{
url = util.encode_id(row[1]),
title = row[2],
isanon = row[3] == 1,
author = row[4],
posted = os.date("%B %d %Y",tonumber(row[5])),
tags = libtags.get(row[1]),
hits = row[6]
})
end
local ret = pages.search{
domain = config.domain,
results = results,
q = searchq,
}
http_response(req,200,ret)
end
return search_get