Compare commits

..

No commits in common. "44a6dffba37ffdc02ec411bb9d94788daa728524" and "c2af1dcc36af2541401880bcdd43ba39fc8b8bc0" have entirely different histories.

8 changed files with 27 additions and 161 deletions

View File

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

View File

@ -1,18 +0,0 @@
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"
}
}

View File

@ -1,17 +0,0 @@
meta {
name: Delete API
type: http
seq: 6
}
delete {
url: http://{{BASE_URL}}/api/delete
body: json
auth: inherit
}
body:json {
{
"id": 8
}
}

View File

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

View File

@ -1,19 +0,0 @@
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

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

View File

@ -7,33 +7,18 @@
import json import json
from bottle import Bottle, route, response, request from bottle import Bottle, route, response, request
from config import directory, database from config import directory
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 = {
"member": [ "foods": [
{ "Pizza",
"id": 1, "Burger",
"name": "Syahdan", "Pasta",
"phone": "081234567890"
},
{
"id": 2,
"name": "Dita",
"phone": "081234567891"
},
{
"id": 3,
"name": "Aji",
"phone": "081234567892"
}
] ]
} }
@ -46,7 +31,7 @@ def index():
} }
return public_home.main().html(params) return public_home.main().html(params)
@app.route('/api/<crud>', method=['OPTIONS', 'GET', 'POST', 'PUT', 'DELETE']) @app.route('/api/<crud>', methods=['GET', 'POST', 'PUT', 'DELETE'])
def index(crud): def index(crud):
response.content_type = 'application/json' response.content_type = 'application/json'
result = { result = {
@ -54,82 +39,48 @@ 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":
name = payload["name"] data["foods"].append(payload["food"])
phone = payload["phone"] id = len(data["foods"]) - 1
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["name"]} is added to the list.' result["message"] = f'{payload["food"]} is added to the list.'
result["data"] = { result["data"] = {
"name": name, "id": id,
"phone": phone "food": data["foods"][id]
} }
elif crud == "read": elif crud == "read":
cursor.execute("SELECT * FROM `member`;")
members = cursor.fetchall()
if not members:
result["status"] = "error"
result["message"] = "No members found."
return json.dumps(result, indent = 2).encode()
result["status"] = "success" result["status"] = "success"
result["message"] = "Members collected." result["message"] = "Reading foods."
result["data"] = members result["data"] = data["foods"]
elif crud == "update": elif crud == "update":
member_id = payload["id"] food_id = payload["id"]
name = payload["name"] food = payload["food"]
phone = payload["phone"]
cursor.execute("UPDATE `member` SET `name` = %s, `phone` = %s WHERE `id` = %s ;" , (name, phone, member_id) ) if len(data["foods"]) <= food_id:
db_main.commit()
cursor.execute("SELECT * FROM `member` WHERE `id` = %s;", (member_id,))
member = cursor.fetchone()
if not member:
result["status"] = "error" result["status"] = "error"
result["message"] = f'Member with ID {member_id} not found.' result["message"] = f"{food_id} does not exist."
return json.dumps(result, indent = 2).encode() return json.dumps(result, indent = 2).encode()
data["foods"][food_id] = food
result["status"] = "success" result["status"] = "success"
result["message"] = f'Member with ID {member_id} is updated.' result["message"] = f'{food} is updated.'
result["data"] = { result["data"] = {
"id" : member["id"], "food": data["foods"][food_id]
"name" : member["name"],
"phone": member["phone"]
} }
elif crud == "delete": elif crud == "delete":
member_id = payload["id"] food_id = payload["id"]
cursor.execute("SELECT * FROM `member` WHERE `id` = %s;", (member_id,)) food = data["foods"].pop(food_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'Member with ID {member_id} is deleted.' result["message"] = f'{food} is deleted'
result["data"] = { result["data"] = {
"id" : member["id"], "food": food
"name" : member["name"],
"phone": member["phone"]
} }
return json.dumps(result, indent = 2).encode() return json.dumps(result, indent = 2).encode()

View File

@ -1,11 +0,0 @@
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');