Compare commits

...

2 Commits

Author SHA1 Message Date
44a6dffba3 sql operations works! + bruno requests 2025-06-07 16:14:44 +07:00
d8da1a6c7a add init.sql as database seed 2025-06-07 16:13:38 +07:00
8 changed files with 162 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
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/<crud>', methods=['GET', 'POST', 'PUT', 'DELETE'])
@app.route('/api/<crud>', 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()

11
sql/init.sql Normal file
View File

@ -0,0 +1,11 @@
CREATE TABLE member (
id int(11) not null auto_increment primary key,
name varchar(36) not null,
phone varchar(14) null
);
INSERT INTO member VALUES
(DEFAULT, 'Syahdan', '081111111111'),
(DEFAULT, 'Hafiz', '082222222222'),
(DEFAULT, 'Ashari', '083333333333');