-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
168 lines (135 loc) · 5.66 KB
/
Copy pathapi.py
File metadata and controls
168 lines (135 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from flask import Flask, request, jsonify
import mysql.connector
from config import mysql_config
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
def connect_to_mysql():
try:
connection = mysql.connector.connect(
host=mysql_config['host'],
user=mysql_config['user'],
password=mysql_config['password'],
database=mysql_config['database']
)
if connection.is_connected():
return connection
except mysql.connector.Error as e:
print(f"Failed to connect to MySQL Database: {e}")
return None
@app.route('/hobi', methods=['GET'])
def get_hobi():
connection = connect_to_mysql()
cursor = connection.cursor(dictionary=True)
cursor.execute("SELECT * FROM hobi")
result = cursor.fetchall()
return jsonify(result)
@app.route('/hobi', methods=['POST'])
def create_hobi():
connection = connect_to_mysql()
cursor = connection.cursor(dictionary=True)
# Mengambil data dari form HTML
nama = request.form['nama']
cursor.execute("INSERT INTO hobi (nama) VALUES (%s)", (nama,))
connection.commit()
return jsonify({'message': 'Hobi created successfully'})
@app.route('/hobi/<int:id>', methods=['GET'])
def get_hobi_detail(id):
connection = connect_to_mysql()
cursor = connection.cursor(dictionary=True)
cursor.execute("SELECT * FROM hobi WHERE id = %s", (id,))
result = cursor.fetchone()
return jsonify(result)
@app.route('/hobi/<int:id>', methods=['PUT'])
def update_hobi(id):
connection = connect_to_mysql()
cursor = connection.cursor(dictionary=True)
data = request.json
nama = data['nama']
cursor.execute("UPDATE hobi SET nama = %s WHERE id = %s", (nama, id))
connection.commit()
return jsonify({'message': 'Hobi updated successfully'})
@app.route('/hobi/<int:id>', methods=['DELETE'])
def delete_hobi(id):
connection = connect_to_mysql()
cursor = connection.cursor(dictionary=True)
cursor.execute("DELETE FROM hobi WHERE id = %s", (id,))
connection.commit()
return jsonify({'message': 'Hobi deleted successfully'})
@app.route('/anggota', methods=['GET'])
def get_anggota():
connection = connect_to_mysql()
cursor = connection.cursor(dictionary=True)
cursor.execute("SELECT * FROM anggota")
result = cursor.fetchall()
return jsonify(result)
@app.route('/anggota', methods=['POST'])
def create_anggota():
connection = connect_to_mysql()
cursor = connection.cursor(dictionary=True)
# Mengambil data dari form HTML
nama = request.form['nama']
usia = request.form['usia']
email = request.form['email']
deskripsi = request.form['deskripsi']
tanggal_lahir = request.form['tanggal_lahir']
gender = request.form['gender']
status = request.form['status']
cursor.execute("INSERT INTO anggota (nama, usia, email, deskripsi, tanggal_lahir, gender, status) VALUES (%s, %s, %s, %s, %s, %s, %s)", (nama, usia, email, deskripsi, tanggal_lahir, gender, status))
connection.commit()
return jsonify({'message': 'Anggota created successfully'})
@app.route('/anggota/<int:id>', methods=['GET'])
def get_anggota_detail(id):
connection = connect_to_mysql()
cursor = connection.cursor(dictionary=True)
cursor.execute("SELECT * FROM anggota WHERE id = %s", (id,))
result = cursor.fetchone()
return jsonify(result)
@app.route('/anggota/<int:id>', methods=['PUT'])
def update_anggota(id):
connection = connect_to_mysql()
cursor = connection.cursor(dictionary=True)
# Mengambil data dari form HTML
nama = request.form['nama']
usia = request.form['usia']
email = request.form['email']
deskripsi = request.form['deskripsi']
tanggal_lahir = request.form['tanggal_lahir']
gender = request.form['gender']
status = request.form['status']
cursor.execute("UPDATE anggota SET nama = %s, usia = %s, email = %s, deskripsi = %s, tanggal_lahir = %s, gender = %s, status = %s WHERE id = %s", (nama, usia, email, deskripsi, tanggal_lahir, gender, status, id))
connection.commit()
return jsonify({'message': 'Anggota updated successfully'})
@app.route('/anggota/<int:id>', methods=['DELETE'])
def delete_anggota(id):
connection = connect_to_mysql()
cursor = connection.cursor(dictionary=True)
cursor.execute("DELETE FROM anggota WHERE id = %s", (id,))
connection.commit()
return jsonify({'message': 'Anggota deleted successfully'})
@app.route('/hobi_anggota', methods=['GET'])
def get_hobi_anggota():
connection = connect_to_mysql()
cursor = connection.cursor(dictionary=True)
cursor.execute("SELECT * FROM hobi_anggota")
result = cursor.fetchall()
return jsonify(result)
@app.route('/hobi_anggota', methods=['POST'])
def create_hobi_anggota():
connection = connect_to_mysql()
cursor = connection.cursor(dictionary=True)
# Mengambil data dari form HTML
anggota_id = request.form['anggota_id']
hobi_id = request.form['hobi_id']
cursor.execute("INSERT INTO hobi_anggota (anggota_id, hobi_id) VALUES (%s, %s)", (anggota_id, hobi_id))
connection.commit()
return jsonify({'message': 'Hobi-anggota relationship created successfully'})
@app.route('/hobi_anggota/<int:anggota_id>/<int:hobi_id>', methods=['DELETE'])
def delete_hobi_anggota(anggota_id, hobi_id):
connection = connect_to_mysql()
cursor = connection.cursor(dictionary=True)
cursor.execute("DELETE FROM hobi_anggota WHERE anggota_id = %s AND hobi_id = %s", (anggota_id, hobi_id))
connection.commit()
return jsonify({'message': 'Hobi-anggota relationship deleted successfully'})
if __name__ == '__main__':
app.run(debug=True)