1. Install phyton
sudo dnf install python3 python3-pip -y
pip3 install flask mysql-connector-python
2. create database sample & insert data
CREATE DATABASE hris;
USE hris;
CREATE TABLE buy_period (
id INT AUTO_INCREMENT PRIMARY KEY,
period_year INT NOT NULL,
period_month INT NOT NULL,
start_date DATE NOT NULL,
end_date DATE NOT NULL
);
INSERT INTO buy_period (period_year, period_month, start_date, end_date)
VALUES (2025, 1, '2025-01-01', '2025-01-31');
INSERT INTO buy_period (period_year, period_month, start_date, end_date)
VALUES (2025, 5, '2025-05-01', '2025-05-31');
3. create phyton script API
cat /data/api/delete_api_buy.py
[root@teguhth api]# pwd
/data/api
[root@teguhth api]# cat /data/api/delete_api_buy.py
from flask import Flask, request, jsonify
import mysql.connector
app = Flask(__name__)
def get_db():
return mysql.connector.connect(
host="localhost",
user="admin",
password="admin",
database="hris"
)
@app.route('/delete-buy-period', methods=['DELETE'])
def delete_buy_period():
data = request.get_json()
if not data or "year" not in data or "month" not in data:
return jsonify({"MESSAGE": "Unprocessable Entity"}), 422
year = data["year"]
month = data["month"]
db = get_db()
cursor = db.cursor(buffered=True) # ? FIX PENTING
# Cek data
cursor.execute("""
SELECT id FROM buy_period
WHERE period_year=%s AND period_month=%s
""", (year, month))
row = cursor.fetchone()
if row is None:
return jsonify({"MESSAGE": "NOT FOUND"}), 422
# Delete
cursor.execute("""
DELETE FROM buy_period
WHERE period_year=%s AND period_month=%s
""", (year, month))
db.commit()
return jsonify({
"MESSAGE": "SUCCESS",
"DELETED": {"year": year, "month": month}
})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8080)
[root@teguhth api]#
4. run phyton script for test in new console
python3 /data/api/delete_api_buy.py
5. simulation true
curl -X DELETE http://localhost:8080/delete-buy-period -H "Content-Type: application/json" -d '{"year":2025,"month":1}'
[root@teguhth ~]# curl -X DELETE http://localhost:8080/delete-buy-period -H "Content-Type: application/json" -d '{"year":2025,"month":1}'
{"DELETED":{"month":1,"year":2025},"MESSAGE":"SUCCESS"}
[root@teguhth ~]#
6. Uji Simulasi (menghasilkan ERROR yang sama) Contoh request yang salah (tanpa parameter)
curl -X DELETE http://localhost:8080/delete-buy-period -H "Content-Type: application/json" -d '{"year":2025,"month":10}'
[root@teguhth ~]# curl -X DELETE http://localhost:8080/delete-buy-period -H "Content-Type: application/json" -d '{"year":2025,"month":10}'
{"MESSAGE":"NOT FOUND"}
[root@teguhth ~]#
7. Contoh request dengan periode yang tidak ada
curl -X DELETE http://localhost:8080/delete-buy-period -H "Content-Type: application/json" -d '{"year":2025,"month":1}'
[root@teguhth ~]# curl -X DELETE http://localhost:8080/delete-buy-period -H "Content-Type: application/json" -d '{"year":2025,"month":1}'
{"MESSAGE":"NOT FOUND"}
[root@teguhth ~]#
8. simulasi jika database & table tidak ada
curl -X DELETE http://localhost:8080/delete-buy-period -H "Content-Type: application/json" -d '{"year":2025,"month":1}'
[root@teguhth ~]# curl -X DELETE http://localhost:8080/delete-buy-period -H "Content-Type: application/json" -d '{"year":2025,"month":1}'
<!doctype html>
<html lang=en>
<title>500 Internal Server Error</title>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.</p>
[root@teguhth ~]#
9. simulasi jika table data masih belum ada isi
curl -X DELETE http://localhost:8080/delete-buy-period -H "Content-Type: application/json" -d '{"year":2025,"month":1}'
[root@teguhth ~]# curl -X DELETE http://localhost:8080/delete-buy-period -H "Content-Type: application/json" -d '{"year":2025,"month":1}'
{"MESSAGE":"NOT FOUND"}
[root@teguhth ~]#











No comments:
Post a Comment