diff --git a/api/handler.py b/api/handler.py index ca10d89..028144b 100644 --- a/api/handler.py +++ b/api/handler.py @@ -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`;")