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

58 lines
1.4 KiB
Lua
Raw Normal View History

2018-09-05 19:07:37 +02:00
BaseController:subclass(
"ServiceController",
{
registry = {},
models = {"subscribers"}
}
)
function fail(msg)
std.json()
std.t(JSON.encode({error = msg}))
end
function result(obj)
std.json()
std.t(JSON.encode({result = obj, error = false}))
end
function ServiceController:sendmail()
2018-10-10 14:35:00 +02:00
if not REQUEST.json then
2018-09-05 19:07:37 +02:00
fail("unknown request")
end
2018-10-10 14:35:00 +02:00
local rq = (JSON.decodeString(REQUEST.json))
2020-06-17 19:12:49 +02:00
local to = "mrsang@lxsang.me"
2018-09-05 19:07:37 +02:00
local from = "From: " .. rq.email .. "\n"
local suject = "Subject: " .. rq.subject .. "\n"
local content = "Contact request from:" .. rq.name .. "\n Email: " .. rq.email .. "\n" .. rq.content .. "\n"
local cmd = 'echo "' .. utils.escape(from .. suject .. content) .. '"| sendmail ' .. to
--print(cmd)
local r = os.execute(cmd)
if r then
result(r)
else
fail("Cannot send email at the moment, the service may be down")
end
return false
end
function ServiceController:subscribe()
2018-10-10 14:35:00 +02:00
if not REQUEST.json then
2018-09-05 19:07:37 +02:00
fail("unknown request")
end
2018-10-10 14:35:00 +02:00
local rq = (JSON.decodeString(REQUEST.json))
2018-09-05 19:07:37 +02:00
-- check if email is exist
local data = self.subscribers:find({exp = {["="] = {email = rq.email}}})
if data and #data > 0 then
fail("You are already/previously subscribed")
else
-- save to database
self.subscribers:create(rq)
result("Ok")
end
return false
end