Pertemuan ke 10: CRUD API Selesai
This commit is contained in:
parent
d267f4d953
commit
80492d466c
42
handler.py
42
handler.py
@ -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
|
||||||
@ -37,36 +38,51 @@ def list(crud):
|
|||||||
"endpoint":f"/api/{crud}",
|
"endpoint":f"/api/{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()
|
||||||
|
Loading…
Reference in New Issue
Block a user