product list

This commit is contained in:
metamuffin 2024-10-31 02:00:42 +01:00
parent a98f08d6c2
commit bccab824ba
No known key found for this signature in database
GPG key ID: 718F9749DCDBD654
2 changed files with 28 additions and 2 deletions

View file

@ -14,3 +14,5 @@ useful for development or proxyless deployments.
- `log` stores the transaction log as CSV (`time,user,amount,comment`) - `log` stores the transaction log as CSV (`time,user,amount,comment`)
- `products` stores the product list as CSV (`barcode,price,name`) - `products` stores the product list as CSV (`barcode,price,name`)
- `config` stores configuration parameters as ESV (`key=value`)
- `transaction_sound`: URL to sound played when creating a transaction

View file

@ -3,10 +3,12 @@
local function escape(s) local function escape(s)
return s:gsub("<", "&lt;"):gsub("<", "&lt;") return s:gsub("<", "&lt;"):gsub("<", "&lt;")
end end
local function urldecode(s) local function urldecode(s)
if s == nil then return nil end if s == nil then return nil end
return s:gsub("+", " "):gsub("%%20", " ") return s:gsub("+", " "):gsub("%%20", " ")
end end
local function urlencode(s) local function urlencode(s)
if s == nil then return nil end if s == nil then return nil end
return s:gsub(" ", "%%20") return s:gsub(" ", "%%20")
@ -90,7 +92,8 @@ local function respond(status, title, body)
<body> <body>
<nav> <nav>
<h2><a href="/">Strichliste v2</a></h2> <h2><a href="/">Strichliste v2</a></h2>
<span><a href="/?log">View Log</a></span> <span><a href="/?log">Log</a></span>
<span><a href="/?products">Products</a></span>
<span><a href="https://codeberg.org/metamuffin/strichliste">Source</a></span> <span><a href="https://codeberg.org/metamuffin/strichliste">Source</a></span>
</nav> </nav>
]], escape(title), stylesheet, script, config.head_extra or "")) ]], escape(title), stylesheet, script, config.head_extra or ""))
@ -136,6 +139,7 @@ local function read_log()
return tonumber(time), username, tonumber(amount), comment return tonumber(time), username, tonumber(amount), comment
end end
end end
local function read_products() local function read_products()
local log = io.open("products", "r") local log = io.open("products", "r")
if log == nil then if log == nil then
@ -159,6 +163,7 @@ local function balances()
end end
return users return users
end end
local function last_txns() local function last_txns()
local users = {} local users = {}
for time, username, _, _ in read_log() do for time, username, _, _ in read_log() do
@ -338,6 +343,23 @@ local function r_create_user()
return redirect(string.format("/%s", urlencode(username))) return redirect(string.format("/%s", urlencode(username)))
end end
local function r_products()
respond(200, "Product List", function()
print("<h1>Product List</h1>")
print("<table><tr><th>Name</th><th>Price</th><th>Amount</th></tr>")
for barcode, price, name in read_products() do
print(string.format([[
<tr><td>%s</td><td class="amount-%s">%.02f</td><td>%s</td></tr>
]],
name,
price >= 0 and "pos" or "neg", price / 100,
barcode
))
end
print("</table>")
end)
end
local function extract_username() local function extract_username()
if path == nil then if path == nil then
return respond_error("no path") return respond_error("no path")
@ -350,7 +372,9 @@ local function extract_username()
end end
if path == "/" then if path == "/" then
if query.log then if query.products then
return r_products()
elseif query.log then
return r_log() return r_log()
elseif query.create_user then elseif query.create_user then
return r_create_user() return r_create_user()