mirror of
https://codeberg.org/metamuffin/abrechenbarkeit.git
synced 2024-12-29 00:04:35 +00:00
create user form
This commit is contained in:
parent
585dd5ac36
commit
a2c773b04c
1 changed files with 67 additions and 21 deletions
|
@ -28,6 +28,12 @@ local function respond_error(message)
|
|||
end)
|
||||
end
|
||||
|
||||
local function redirect(path)
|
||||
print("Status: 307")
|
||||
print(string.format("Location: %s", path))
|
||||
print()
|
||||
end
|
||||
|
||||
local function form_data()
|
||||
local data = {}
|
||||
for pair in string.gmatch(io.read(), "([^&]+)") do
|
||||
|
@ -62,27 +68,10 @@ local function balances()
|
|||
return users
|
||||
end
|
||||
|
||||
if path == "/" then
|
||||
if query == "?log" then
|
||||
return respond(200, "Log", function()
|
||||
print("<table>")
|
||||
print("<tr><th>Time</th><th>Username</th><th>Amount</th><th>Comment</th></tr>")
|
||||
for time, username, amount, comment in read_log() do
|
||||
print(string.format("<tr><td>%d</td><td>%s</td><td>%.02f€</td><td>%s</td></tr>", time, escape(username),
|
||||
amount / 100, escape(comment)))
|
||||
end
|
||||
print("</table>")
|
||||
end)
|
||||
local function r_user()
|
||||
if path == nil then
|
||||
return respond_error("no path")
|
||||
end
|
||||
return respond(200, "Users", function()
|
||||
print("<ul>")
|
||||
for username, balance in balances() do
|
||||
print(string.format("<li><a href=\"/%s\">%s</a>: %.02f€</li>", escape(username), escape(username),
|
||||
balance / 100))
|
||||
end
|
||||
print("</ul>")
|
||||
end)
|
||||
elseif path ~= nil then
|
||||
local username = path:sub(2)
|
||||
if username:match("^[%w_-]+$") == nil then
|
||||
return respond_error("username invalid")
|
||||
|
@ -108,8 +97,10 @@ elseif path ~= nil then
|
|||
log:close()
|
||||
end
|
||||
|
||||
respond(200, username, function()
|
||||
return respond(200, username, function()
|
||||
print(string.format("<h1>%s</h1>", username))
|
||||
local balance = balances()[username] or 0
|
||||
print(string.format("Current balance: %.02f", balance / 100))
|
||||
print([[
|
||||
<form action="" method="POST">
|
||||
<label for="amount">Amount: </label>
|
||||
|
@ -132,3 +123,58 @@ elseif path ~= nil then
|
|||
end
|
||||
end)
|
||||
end
|
||||
|
||||
local function r_log()
|
||||
return respond(200, "Log", function()
|
||||
print("<table>")
|
||||
print("<tr><th>Time</th><th>Username</th><th>Amount</th><th>Comment</th></tr>")
|
||||
for time, username, amount, comment in read_log() do
|
||||
print(string.format("<tr><td>%d</td><td>%s</td><td>%.02f€</td><td>%s</td></tr>", time, escape(username),
|
||||
amount / 100, escape(comment)))
|
||||
end
|
||||
print("</table>")
|
||||
end)
|
||||
end
|
||||
|
||||
local function r_index()
|
||||
return respond(200, "Users", function()
|
||||
print([[
|
||||
<form action="/" method="GET">
|
||||
<label for="username">Username: </label>
|
||||
<input type="text" name="create_user" id="username" /><br/>
|
||||
<input type="submit" value="Create" />
|
||||
</form>
|
||||
]])
|
||||
print("<ul>")
|
||||
for username, balance in pairs(balances()) do
|
||||
print(string.format("<li><a href=\"/%s\">%s</a>: %.02f€</li>", escape(username), escape(username),
|
||||
balance / 100))
|
||||
end
|
||||
print("</ul>")
|
||||
end)
|
||||
end
|
||||
|
||||
local function r_create_user()
|
||||
if query == nil then
|
||||
return respond_error("no query")
|
||||
end
|
||||
local username = query:match("^\\?create_user=([%w_-]+)$")
|
||||
if username == nil then
|
||||
return respond_error("invalid username")
|
||||
end
|
||||
return redirect(string.format("/%s", username))
|
||||
end
|
||||
|
||||
if path == "/" then
|
||||
if query == "?log" then
|
||||
return r_log()
|
||||
elseif query ~= nil and query:match("^\\?create_user=") then
|
||||
return r_create_user()
|
||||
else
|
||||
return r_index()
|
||||
end
|
||||
else
|
||||
r_user()
|
||||
end
|
||||
|
||||
return respond_error("unknown route")
|
||||
|
|
Loading…
Reference in a new issue