From 4c6601a232f7b375591f9469385146d08b0b1754 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Sun, 3 Nov 2024 02:20:52 +0100 Subject: [PATCH] remove product form --- abrechenbarkeit.lua | 71 +++++++++++++++++++++++++++++++++++++++++++-- collapse_log.lua | 1 - 2 files changed, 68 insertions(+), 4 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("

Product List

") + if notif then print(notif) end + print([[ +
+

Add Product

+ +
+ +
+ +
+ +
+
+

Remove Product

+ + +
+ +
+ ]]) print("") for barcode, price, name in read_products() do print(string.format([[ diff --git a/collapse_log.lua b/collapse_log.lua index cfba345..19afe3d 100644 --- a/collapse_log.lua +++ b/collapse_log.lua @@ -15,7 +15,6 @@ local function read_log() end end - local function balances() local users = {} for _, username, amount, _ in read_log() do
NamePriceBarcode