handle cors

This commit is contained in:
Syahdan Hafiz Ashari 2025-07-15 14:13:12 +07:00
parent 5a83c3d4c7
commit e25bb5594c

View File

@ -46,7 +46,9 @@ def index(crud):
cursor = db_main.cursor(dictionary=True)
payload = request.json
if crud == "create":
if request.method == 'OPTIONS':
return None
elif crud == "create":
name = payload["name"]
price = payload["price"]
qty = payload["qty"]
@ -57,15 +59,16 @@ def index(crud):
return json.dumps(result, indent = 2).encode()
data_id = cursor.execute("INSERT INTO `items` (name, price, qty) VALUES (%s, %s, %s);", (name, price, qty))
db_main.commit()
# Get the newly created item with its ID
cursor.execute("SELECT * FROM `items` WHERE `id` = LAST_INSERT_ID();")
new_item = cursor.fetchone()
result["status"] = "success"
result["message"] = f'{payload["name"]} is added to the list.'
result["data"] = {
"name": name,
"price": price,
"qty": qty
}
result["data"] = new_item
elif crud == "read":
cursor.execute("SELECT * FROM `items`;")