diff --git a/bruno.json b/bruno.json new file mode 100644 index 0000000..5d23613 --- /dev/null +++ b/bruno.json @@ -0,0 +1,6 @@ +{ + "version": "1", + "name": "costapy", + "type": "collection", + "ignore": ["node_modules", ".git"] +} \ No newline at end of file diff --git a/bruno/Create API.bru b/bruno/Create API.bru new file mode 100644 index 0000000..ac5f394 --- /dev/null +++ b/bruno/Create API.bru @@ -0,0 +1,18 @@ +meta { + name: Create API + type: http + seq: 6 +} + +post { + url: http://{{BASE_URL}}/api/create + body: json + auth: inherit +} + +body:json { + { + "name": "Test Lagi", + "phone": "080000000002" + } +} diff --git a/bruno/Delete API.bru b/bruno/Delete API.bru new file mode 100644 index 0000000..3a74008 --- /dev/null +++ b/bruno/Delete API.bru @@ -0,0 +1,17 @@ +meta { + name: Delete API + type: http + seq: 6 +} + +delete { + url: http://{{BASE_URL}}/api/delete + body: json + auth: inherit +} + +body:json { + { + "id": 8 + } +} diff --git a/bruno/Read API.bru b/bruno/Read API.bru new file mode 100644 index 0000000..e3a653b --- /dev/null +++ b/bruno/Read API.bru @@ -0,0 +1,11 @@ +meta { + name: Read API + type: http + seq: 5 +} + +get { + url: http://{{BASE_URL}}/api/read + body: none + auth: inherit +} diff --git a/bruno/Update API.bru b/bruno/Update API.bru new file mode 100644 index 0000000..185d082 --- /dev/null +++ b/bruno/Update API.bru @@ -0,0 +1,19 @@ +meta { + name: Update API + type: http + seq: 6 +} + +put { + url: http://{{BASE_URL}}/api/update + body: json + auth: inherit +} + +body:json { + { + "id": 4, + "name": "Syahdan", + "phone": "082112586824" + } +} diff --git a/environments/costapy local.bru b/environments/costapy local.bru new file mode 100644 index 0000000..bee9106 --- /dev/null +++ b/environments/costapy local.bru @@ -0,0 +1,3 @@ +vars { + BASE_URL: localhost:11000 +} diff --git a/handler.py b/handler.py index aeae502..745b596 100644 --- a/handler.py +++ b/handler.py @@ -7,18 +7,33 @@ import json from bottle import Bottle, route, response, request -from config import directory +from config import directory, database +import mysql.connector as mariadb import templates.plain.main as template_public import modules.public.home as public_home app = Bottle() +# Sample data to simulate a database +# also serves as a model reminder data = { - "foods": [ - "Pizza", - "Burger", - "Pasta", + "member": [ + { + "id": 1, + "name": "Syahdan", + "phone": "081234567890" + }, + { + "id": 2, + "name": "Dita", + "phone": "081234567891" + }, + { + "id": 3, + "name": "Aji", + "phone": "081234567892" + } ] } @@ -31,7 +46,7 @@ def index(): } return public_home.main().html(params) -@app.route('/api/', methods=['GET', 'POST', 'PUT', 'DELETE']) +@app.route('/api/', method=['OPTIONS', 'GET', 'POST', 'PUT', 'DELETE']) def index(crud): response.content_type = 'application/json' result = { @@ -39,48 +54,82 @@ def index(crud): "message": f"API endpoint for /api/{crud} is not implemented yet." } + db_main = mariadb.connect(**database.db_main) + cursor = db_main.cursor(dictionary=True) + payload = request.json if crud == "create": - data["foods"].append(payload["food"]) - id = len(data["foods"]) - 1 + name = payload["name"] + phone = payload["phone"] + + if not name or not phone: + result["status"] = "error" + result["message"] = "Name and phone are required." + return json.dumps(result, indent = 2).encode() + + data_id = cursor.execute("INSERT INTO `member` (name, phone) VALUES (%s, %s);", (name, phone)) result["status"] = "success" - result["message"] = f'{payload["food"]} is added to the list.' + result["message"] = f'{payload["name"]} is added to the list.' + result["data"] = { - "id": id, - "food": data["foods"][id] + "name": name, + "phone": phone } elif crud == "read": - result["status"] = "success" - result["message"] = "Reading foods." - result["data"] = data["foods"] + cursor.execute("SELECT * FROM `member`;") + members = cursor.fetchall() - elif crud == "update": - food_id = payload["id"] - food = payload["food"] - - if len(data["foods"]) <= food_id: + if not members: result["status"] = "error" - result["message"] = f"{food_id} does not exist." + result["message"] = "No members found." return json.dumps(result, indent = 2).encode() - data["foods"][food_id] = food + result["status"] = "success" + result["message"] = "Members collected." + result["data"] = members + + elif crud == "update": + member_id = payload["id"] + name = payload["name"] + phone = payload["phone"] + + cursor.execute("UPDATE `member` SET `name` = %s, `phone` = %s WHERE `id` = %s ;" , (name, phone, member_id) ) + db_main.commit() + cursor.execute("SELECT * FROM `member` WHERE `id` = %s;", (member_id,)) + member = cursor.fetchone() + + if not member: + result["status"] = "error" + result["message"] = f'Member with ID {member_id} not found.' + return json.dumps(result, indent = 2).encode() result["status"] = "success" - result["message"] = f'{food} is updated.' + result["message"] = f'Member with ID {member_id} is updated.' result["data"] = { - "food": data["foods"][food_id] + "id" : member["id"], + "name" : member["name"], + "phone": member["phone"] } elif crud == "delete": - food_id = payload["id"] - food = data["foods"].pop(food_id) + member_id = payload["id"] + cursor.execute("SELECT * FROM `member` WHERE `id` = %s;", (member_id,)) + member = cursor.fetchone() + if not member: + result["status"] = "error" + result["message"] = f'Member with ID {member_id} not found.' + return json.dumps(result, indent = 2).encode() + + cursor.execute("DELETE FROM `member` WHERE `id` = %s;", (member_id,)) result["status"] = "success" - result["message"] = f'{food} is deleted' + result["message"] = f'Member with ID {member_id} is deleted.' result["data"] = { - "food": food + "id" : member["id"], + "name" : member["name"], + "phone": member["phone"] } - + return json.dumps(result, indent = 2).encode() \ No newline at end of file