remove product form

This commit is contained in:
metamuffin 2024-11-03 02:20:52 +01:00
parent bf2ea37ee8
commit 4c6601a232
No known key found for this signature in database
GPG key ID: 718F9749DCDBD654
2 changed files with 68 additions and 4 deletions

View file

@ -157,8 +157,8 @@ local function read_products()
if l == "" or l == nil then if l == "" or l == nil then
return nil return nil
end end
local barcode, amount, name = string.match(l, "([%w_-]+),(-?%d+),([%w_ -]*)") local barcode, price, name = string.match(l, "([%w_-]+),(-?%d+),([%w_ -]*)")
return barcode, tonumber(amount), name return barcode, tonumber(price), name
end end
end end
@ -350,9 +350,74 @@ 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_post()
local data = form_data()
local barcode = data.barcode
if barcode == nil then
return error_box("barcode unset")
end
if barcode:match("^[%w_-]*$") == nil then
return error_box("barcode invalid")
end
if data.delete then
local new_products = io.open("products.new", "w+")
if new_products == nil then
return error_box("failed to open new products")
end
for a_barcode, price, name in read_products() do
if barcode ~= a_barcode then
new_products:write(string.format("%s,%d,%s\n", a_barcode, price, name))
end
end
new_products:flush()
new_products:close()
os.rename("products.new", "products")
else
local price = tonumber(data.price)
local name = data.name
if price == nil then
return error_box("price invalid")
end
if name:match("^[%w_ -]*$") == nil then
return error_box("name invalid")
end
local products = io.open("products", "a+")
if products == nil then
return error_box("failed to open products")
end
products:write(string.format("%s,%d,%s\n", barcode, price, name))
products:flush()
products:close()
end
end
local function r_products() local function r_products()
respond(200, "Abrechenbare Products", function() local notif = nil
if method == "POST" then
notif = r_products_post()
end
respond(200, "Abrechenbare Product List", function()
print("<h1>Product List</h1>") print("<h1>Product List</h1>")
if notif then print(notif) end
print([[
<form action="/?products" method="POST" class="box">
<h3>Add Product</h3>
<label for="barcode">Barcode: </label>
<input type="text" name="barcode" id="barcode" /><br/>
<label for="name">Name: </label>
<input type="text" name="name" id="name" /><br/>
<label for="price">Price: </label>
<input type="number" name="price" id="price" /><br/>
<input type="submit" value="Add" />
</form>
<form action="/?products" method="POST" class="box">
<h3>Remove Product</h3>
<input type="text" name="delete" value="1" hidden />
<label for="barcode">Barcode: </label>
<input type="text" name="barcode" id="barcode" /><br/>
<input type="submit" value="Remove" />
</form>
]])
print("<table><tr><th>Name</th><th>Price</th><th>Barcode</th></tr>") print("<table><tr><th>Name</th><th>Price</th><th>Barcode</th></tr>")
for barcode, price, name in read_products() do for barcode, price, name in read_products() do
print(string.format([[ print(string.format([[

View file

@ -15,7 +15,6 @@ local function read_log()
end end
end end
local function balances() local function balances()
local users = {} local users = {}
for _, username, amount, _ in read_log() do for _, username, amount, _ in read_log() do