diff options
author | metamuffin <metamuffin@disroot.org> | 2024-11-03 02:20:52 +0100 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-11-03 18:36:02 +0100 |
commit | 4c6601a232f7b375591f9469385146d08b0b1754 (patch) | |
tree | 9ebfe3fea06605063728fbec73ffe9f2c60a03dd /abrechenbarkeit.lua | |
parent | bf2ea37ee8e0f63a76b83b315b84cafb0b345cea (diff) | |
download | abrechenbarkeit-4c6601a232f7b375591f9469385146d08b0b1754.tar abrechenbarkeit-4c6601a232f7b375591f9469385146d08b0b1754.tar.bz2 abrechenbarkeit-4c6601a232f7b375591f9469385146d08b0b1754.tar.zst |
remove product form
Diffstat (limited to 'abrechenbarkeit.lua')
-rwxr-xr-x | abrechenbarkeit.lua | 71 |
1 files changed, 68 insertions, 3 deletions
diff --git a/abrechenbarkeit.lua b/abrechenbarkeit.lua index dc3685b..68974a4 100755 --- a/abrechenbarkeit.lua +++ b/abrechenbarkeit.lua @@ -157,8 +157,8 @@ local function read_products() if l == "" or l == nil then return nil end - local barcode, amount, name = string.match(l, "([%w_-]+),(-?%d+),([%w_ -]*)") - return barcode, tonumber(amount), name + local barcode, price, name = string.match(l, "([%w_-]+),(-?%d+),([%w_ -]*)") + return barcode, tonumber(price), name end end @@ -350,9 +350,74 @@ local function r_create_user() return redirect(string.format("/%s", urlencode(username))) 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() - 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>") + 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>") for barcode, price, name in read_products() do print(string.format([[ |