Pertemuan ke 10: CRUD API Selesai

This commit is contained in:
Dita Aji Pratama 2025-06-07 15:18:24 +07:00
parent d267f4d953
commit 80492d466c

View File

@ -6,8 +6,9 @@
# You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/. # You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/.
from bottle import Bottle, route, request, response from bottle import Bottle, route, request, response
from config import directory from config import directory, database
import mysql.connector as mariadb
import json import json
import templates.plain.main as template_public import templates.plain.main as template_public
@ -38,35 +39,50 @@ def list(crud):
"data":[] "data":[]
} }
db_main = mariadb.connect(**database.db_main)
cursor = db_main.cursor(dictionary=True)
if crud == "create": if crud == "create":
name = params["name" ]
phone = params["phone" ]
cursor.execute("INSERT INTO `member` VALUES (DEFAULT, %s, %s) ;" , (name, phone) )
keluaran["status"] = "Berhasil" keluaran["status"] = "Berhasil"
keluaran["message"] = "Dummy: Selamat data anda telah ditambah" keluaran["message"] = "Selamat data anda telah ditambah"
keluaran["data"] = { keluaran["data"] = {
"food":params["food"] "name":name,
"phone":phone
} }
elif crud == "read": elif crud == "read":
cursor.execute("SELECT * FROM `member`; ")
member_list = cursor.fetchall()
keluaran["status"] = "Berhasil" keluaran["status"] = "Berhasil"
keluaran["message"] = "Dummy: Data collected" keluaran["message"] = "Data collected"
keluaran["data"] = [ keluaran["data"] = member_list
"Banana",
"Apple",
"Marshmallow"
]
elif crud == "update": elif crud == "update":
key = params["key" ]
name = params["name" ]
phone = params["phone" ]
cursor.execute("UPDATE `member` SET `name` = %s, `phone` = %s WHERE `id` = %s ;" , (name, phone, key) )
keluaran["status"] = "Berhasil" keluaran["status"] = "Berhasil"
keluaran["message"] = "Dummy: Belum dibikin" keluaran["message"] = "Belum dibikin"
keluaran["data"] = { keluaran["data"] = {
"old":params["key"], "key":params["key"],
"new":params["change"] "name":params["name"],
"phone":params["phone"]
} }
elif crud == "delete": elif crud == "delete":
key = params["key" ]
cursor.execute("DELETE FROM `member` WHERE `id` = %s ;" , (key,) )
keluaran["status"] = "Berhasil" keluaran["status"] = "Berhasil"
keluaran["message"] = "Dummy: Belum dibikin" keluaran["message"] = "Data sudah terhapus selamanya!"
else: else:
pass pass
cursor.close()
db_main.close()
return json.dumps(keluaran, indent = 2).encode() return json.dumps(keluaran, indent = 2).encode()