follow UAS spec
This commit is contained in:
parent
03b2647f1e
commit
5a83c3d4c7
@ -13,6 +13,7 @@ post {
|
||||
body:json {
|
||||
{
|
||||
"name": "Test Lagi",
|
||||
"phone": "080000000002"
|
||||
"price": "10000",
|
||||
"qty": 10
|
||||
}
|
||||
}
|
||||
|
@ -13,7 +13,8 @@ put {
|
||||
body:json {
|
||||
{
|
||||
"id": 4,
|
||||
"name": "Syahdan",
|
||||
"phone": "082112586824"
|
||||
"name": "Test Update",
|
||||
"price": "15000",
|
||||
"qty": 5
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,8 @@
|
||||
db_main = {
|
||||
'host' : 'localhost',
|
||||
'user' : 'root',
|
||||
'password' : '',
|
||||
'database' : 'your_db',
|
||||
'password' : 'anjaymaria',
|
||||
'database' : 'inventory',
|
||||
'port' : 3306,
|
||||
'autocommit' : True,
|
||||
}
|
||||
|
@ -18,22 +18,10 @@ app = Bottle()
|
||||
# Sample data to simulate a database
|
||||
# also serves as a model reminder
|
||||
data = {
|
||||
"member": [
|
||||
{
|
||||
"id": 1,
|
||||
"name": "Syahdan",
|
||||
"phone": "081234567890"
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"name": "Dita",
|
||||
"phone": "081234567891"
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
"name": "Aji",
|
||||
"phone": "081234567892"
|
||||
}
|
||||
"items": [
|
||||
{"id": 1, "name": "Kopi", "price": 200.00, "qty": 10},
|
||||
{"id": 2, "name": "Teh", "price": 200.00, "qty": 20},
|
||||
{"id": 3, "name": "Susu", "price": 300.00, "qty": 30}
|
||||
]
|
||||
}
|
||||
|
||||
@ -60,76 +48,81 @@ def index(crud):
|
||||
payload = request.json
|
||||
if crud == "create":
|
||||
name = payload["name"]
|
||||
phone = payload["phone"]
|
||||
price = payload["price"]
|
||||
qty = payload["qty"]
|
||||
|
||||
if not name or not phone:
|
||||
if not name or not price or not qty:
|
||||
result["status"] = "error"
|
||||
result["message"] = "Name and phone are required."
|
||||
result["message"] = "Name, price, and quantity are required."
|
||||
return json.dumps(result, indent = 2).encode()
|
||||
|
||||
data_id = cursor.execute("INSERT INTO `member` (name, phone) VALUES (%s, %s);", (name, phone))
|
||||
|
||||
data_id = cursor.execute("INSERT INTO `items` (name, price, qty) VALUES (%s, %s, %s);", (name, price, qty))
|
||||
|
||||
result["status"] = "success"
|
||||
result["message"] = f'{payload["name"]} is added to the list.'
|
||||
|
||||
result["data"] = {
|
||||
"name": name,
|
||||
"phone": phone
|
||||
"price": price,
|
||||
"qty": qty
|
||||
}
|
||||
|
||||
elif crud == "read":
|
||||
cursor.execute("SELECT * FROM `member`;")
|
||||
members = cursor.fetchall()
|
||||
cursor.execute("SELECT * FROM `items`;")
|
||||
items = cursor.fetchall()
|
||||
|
||||
if not members:
|
||||
if not items:
|
||||
result["status"] = "error"
|
||||
result["message"] = "No members found."
|
||||
result["message"] = "No items found."
|
||||
return json.dumps(result, indent = 2).encode()
|
||||
|
||||
result["status"] = "success"
|
||||
result["message"] = "Members collected."
|
||||
result["data"] = members
|
||||
result["message"] = "Items collected."
|
||||
result["data"] = items
|
||||
|
||||
elif crud == "update":
|
||||
member_id = payload["id"]
|
||||
item_id = payload["id"]
|
||||
name = payload["name"]
|
||||
phone = payload["phone"]
|
||||
price = payload["price"]
|
||||
qty = payload["qty"]
|
||||
|
||||
cursor.execute("UPDATE `member` SET `name` = %s, `phone` = %s WHERE `id` = %s ;" , (name, phone, member_id) )
|
||||
cursor.execute("UPDATE `items` SET `name` = %s, `price` = %s, `qty` = %s WHERE `id` = %s ;" , (name, price, qty, item_id) )
|
||||
db_main.commit()
|
||||
cursor.execute("SELECT * FROM `member` WHERE `id` = %s;", (member_id,))
|
||||
member = cursor.fetchone()
|
||||
cursor.execute("SELECT * FROM `items` WHERE `id` = %s;", (item_id,))
|
||||
item = cursor.fetchone()
|
||||
|
||||
if not member:
|
||||
if not item:
|
||||
result["status"] = "error"
|
||||
result["message"] = f'Member with ID {member_id} not found.'
|
||||
return json.dumps(result, indent = 2).encode()
|
||||
result["message"] = f'Item with ID {item_id} not found.'
|
||||
return json.dumps(result, indent = 2).encode()
|
||||
|
||||
result["status"] = "success"
|
||||
result["message"] = f'Member with ID {member_id} is updated.'
|
||||
result["message"] = f'Item with ID {item_id} is updated.'
|
||||
result["data"] = {
|
||||
"id" : member["id"],
|
||||
"name" : member["name"],
|
||||
"phone": member["phone"]
|
||||
"id" : item["id"],
|
||||
"name" : item["name"],
|
||||
"price": item["price"],
|
||||
"qty" : item["qty"]
|
||||
}
|
||||
|
||||
elif crud == "delete":
|
||||
member_id = payload["id"]
|
||||
cursor.execute("SELECT * FROM `member` WHERE `id` = %s;", (member_id,))
|
||||
member = cursor.fetchone()
|
||||
if not member:
|
||||
item_id = payload["id"]
|
||||
cursor.execute("SELECT * FROM `items` WHERE `id` = %s;", (item_id,))
|
||||
item = cursor.fetchone()
|
||||
if not item:
|
||||
result["status"] = "error"
|
||||
result["message"] = f'Member with ID {member_id} not found.'
|
||||
result["message"] = f'Item with ID {item_id} not found.'
|
||||
return json.dumps(result, indent = 2).encode()
|
||||
|
||||
cursor.execute("DELETE FROM `member` WHERE `id` = %s;", (member_id,))
|
||||
|
||||
cursor.execute("DELETE FROM `items` WHERE `id` = %s;", (item_id,))
|
||||
|
||||
result["status"] = "success"
|
||||
result["message"] = f'Member with ID {member_id} is deleted.'
|
||||
result["message"] = f'Item with ID {item_id} is deleted.'
|
||||
result["data"] = {
|
||||
"id" : member["id"],
|
||||
"name" : member["name"],
|
||||
"phone": member["phone"]
|
||||
"id" : item["id"],
|
||||
"name" : item["name"],
|
||||
"price": item["price"],
|
||||
"qty" : item["qty"]
|
||||
}
|
||||
|
||||
return json.dumps(result, indent = 2).encode()
|
@ -1,11 +1,13 @@
|
||||
CREATE TABLE member (
|
||||
CREATE DATABASE IF NOT EXISTS inventory;
|
||||
|
||||
CREATE TABLE items (
|
||||
id int(11) not null auto_increment primary key,
|
||||
name varchar(36) not null,
|
||||
phone varchar(14) null
|
||||
price int(10) not null,
|
||||
qty int(11) not null
|
||||
);
|
||||
|
||||
INSERT INTO member VALUES
|
||||
(DEFAULT, 'Syahdan', '081111111111'),
|
||||
(DEFAULT, 'Hafiz', '082222222222'),
|
||||
(DEFAULT, 'Ashari', '083333333333');
|
||||
|
||||
INSERT INTO items VALUES
|
||||
(DEFAULT, 'Kopi', 200, 10),
|
||||
(DEFAULT, 'Teh', 200, 20),
|
||||
(DEFAULT, 'Susu', 300, 30);
|
||||
|
Loading…
Reference in New Issue
Block a user