sql operations works! + bruno requests

This commit is contained in:
Syahdan Hafiz Ashari 2025-06-07 16:14:44 +07:00
parent d8da1a6c7a
commit 44a6dffba3
7 changed files with 151 additions and 28 deletions

6
bruno.json Normal file
View File

@ -0,0 +1,6 @@
{
"version": "1",
"name": "costapy",
"type": "collection",
"ignore": ["node_modules", ".git"]
}

18
bruno/Create API.bru Normal file
View File

@ -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"
}
}

17
bruno/Delete API.bru Normal file
View File

@ -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
}
}

11
bruno/Read API.bru Normal file
View File

@ -0,0 +1,11 @@
meta {
name: Read API
type: http
seq: 5
}
get {
url: http://{{BASE_URL}}/api/read
body: none
auth: inherit
}

19
bruno/Update API.bru Normal file
View File

@ -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"
}
}

View File

@ -0,0 +1,3 @@
vars {
BASE_URL: localhost:11000
}

View File

@ -7,18 +7,33 @@
import json import json
from bottle import Bottle, route, response, request 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 templates.plain.main as template_public
import modules.public.home as public_home import modules.public.home as public_home
app = Bottle() app = Bottle()
# Sample data to simulate a database
# also serves as a model reminder
data = { data = {
"foods": [ "member": [
"Pizza", {
"Burger", "id": 1,
"Pasta", "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) return public_home.main().html(params)
@app.route('/api/<crud>', methods=['GET', 'POST', 'PUT', 'DELETE']) @app.route('/api/<crud>', method=['OPTIONS', 'GET', 'POST', 'PUT', 'DELETE'])
def index(crud): def index(crud):
response.content_type = 'application/json' response.content_type = 'application/json'
result = { result = {
@ -39,48 +54,82 @@ def index(crud):
"message": f"API endpoint for /api/{crud} is not implemented yet." "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 payload = request.json
if crud == "create": if crud == "create":
data["foods"].append(payload["food"]) name = payload["name"]
id = len(data["foods"]) - 1 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["status"] = "success"
result["message"] = f'{payload["food"]} is added to the list.' result["message"] = f'{payload["name"]} is added to the list.'
result["data"] = { result["data"] = {
"id": id, "name": name,
"food": data["foods"][id] "phone": phone
} }
elif crud == "read": elif crud == "read":
result["status"] = "success" cursor.execute("SELECT * FROM `member`;")
result["message"] = "Reading foods." members = cursor.fetchall()
result["data"] = data["foods"]
elif crud == "update": if not members:
food_id = payload["id"]
food = payload["food"]
if len(data["foods"]) <= food_id:
result["status"] = "error" result["status"] = "error"
result["message"] = f"{food_id} does not exist." result["message"] = "No members found."
return json.dumps(result, indent = 2).encode() 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["status"] = "success"
result["message"] = f'{food} is updated.' result["message"] = f'Member with ID {member_id} is updated.'
result["data"] = { result["data"] = {
"food": data["foods"][food_id] "id" : member["id"],
"name" : member["name"],
"phone": member["phone"]
} }
elif crud == "delete": elif crud == "delete":
food_id = payload["id"] member_id = payload["id"]
food = data["foods"].pop(food_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["status"] = "success"
result["message"] = f'{food} is deleted' result["message"] = f'Member with ID {member_id} is deleted.'
result["data"] = { result["data"] = {
"food": food "id" : member["id"],
"name" : member["name"],
"phone": member["phone"]
} }
return json.dumps(result, indent = 2).encode() return json.dumps(result, indent = 2).encode()