-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend-server.py
More file actions
430 lines (353 loc) · 15.5 KB
/
backend-server.py
File metadata and controls
430 lines (353 loc) · 15.5 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
import logging
from flask import Flask, request, jsonify
from flask_cors import CORS
from flask_mysqldb import MySQL
from flask import make_response
app = Flask(__name__)
CORS(app, origins='http://localhost:4200', allow_headers=["Content-Type", "Authorization"])
# MySQL Configuration
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'admin'
app.config['MYSQL_PASSWORD'] = 'superpass'
app.config['MYSQL_DB'] = 'project'
mysql = MySQL(app)
def execute_query(query, params=None):
cur = mysql.connection.cursor()
cur.execute(query, params)
result = cur.fetchall()
cur.close()
return result
@app.after_request
def after_request(response):
response.headers.add('Access-Control-Allow-Origin', 'http://localhost:4200')
response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
response.headers.add('Access-Control-Allow-Methods', 'GET,POST,DELETE,OPTIONS')
return response
@app.route('/api/data/cases', methods=['GET', 'POST', 'DELETE', 'OPTIONS'])
def handle_data_case():
if request.method == 'GET':
cur = mysql.connection.cursor()
cur.execute('SELECT * FROM `case`')
case_data = cur.fetchall()
cur.close()
return jsonify(case_data)
elif request.method == 'POST':
data = request.json
case_id = data.get('case_id')
location = data.get('location')
time = data.get('time')
description = data.get('description')
is_open = data.get('is_open')
cur = mysql.connection.cursor()
query = "INSERT INTO `case` (case_id, location, time, description, is_open) VALUES (%s, %s, %s, %s, %s)"
try:
cur.execute(query, (case_id, location, time, description, is_open))
mysql.connection.commit()
cur.close()
return jsonify({'message': 'Case added successfully'})
except Exception as e:
logging.error(f"Error executing query: {query}")
logging.error(str(e))
return jsonify({'error': f'Failed to add case. {str(e)}'}), 500
elif request.method == 'DELETE':
data = request.get_json()
case_id = data.get('case_id')
cur = mysql.connection.cursor()
query = "DELETE FROM `case` WHERE case_id = %s"
try:
cur.execute(query, (case_id,))
mysql.connection.commit()
cur.close()
return jsonify({'message': 'Case deleted successfully'})
except Exception as e:
logging.error(f"Error executing query: {query}")
logging.error(str(e))
return jsonify({'error': f'Failed to delete case. {str(e)}'}), 500
elif request.method == 'OPTIONS':
# Handle preflight request
response = make_response()
response.headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE")
return response
@app.route('/api/data/clues', methods=['GET', 'POST', 'DELETE', 'OPTIONS'])
def handle_data_clue():
if request.method == 'GET':
cur = mysql.connection.cursor()
cur.execute('SELECT * FROM clue')
clue_data = cur.fetchall()
cur.close()
return jsonify(clue_data)
elif request.method == 'POST':
data = request.json
clue_id = data.get('clue_id')
is_murder_weapon = data.get('is_murder_weapon')
date = data.get('date')
place = data.get('place')
description = data.get('description')
Case_case_id = data.get('Case_case_id')
type = data.get('type')
cur = mysql.connection.cursor()
query = "INSERT INTO clue (clue_id, is_murder_weapon, date, place, description, Case_case_id, type) VALUES (%s, %s, %s, %s, %s, %s, %s)"
try:
cur.execute(query, (clue_id, is_murder_weapon, date, place, description, Case_case_id, type))
mysql.connection.commit()
cur.close()
return jsonify({'message': 'Clue added successfully'})
except Exception as e:
logging.error(f"Error executing query: {query}")
logging.error(str(e))
return jsonify({'error': f'Failed to add clue. {str(e)}'}), 500
elif request.method == 'DELETE':
data = request.get_json()
clue_id = data.get('clue_id')
cur = mysql.connection.cursor()
query = "DELETE FROM clue WHERE clue_id = %s"
try:
cur.execute(query, (clue_id,))
mysql.connection.commit()
cur.close()
return jsonify({'message': 'Clue deleted successfully'})
except Exception as e:
logging.error(f"Error executing query: {query}")
logging.error(str(e))
return jsonify({'error': f'Failed to delete clue. {str(e)}'}), 500
elif request.method == 'OPTIONS':
# Handle preflight request
response = make_response()
response.headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE")
return response
@app.route('/api/data/suspects', methods=['GET', 'POST', 'DELETE', 'OPTIONS'])
def handle_data_suspect():
if request.method == 'GET':
cur = mysql.connection.cursor()
cur.execute('SELECT * FROM suspect')
suspect_data = cur.fetchall()
cur.close()
return jsonify(suspect_data)
elif request.method == 'POST':
data = request.json
suspect_id = data.get('suspect_id')
name = data.get('name')
phone = data.get('phone')
gender = data.get('gender')
address_street = data.get('address_street')
address_number = data.get('address_number')
birth_date = data.get('birth_date')
Lawyer_lawyer_id = data.get('Lawyer_lawyer_id')
status = data.get('status')
cur = mysql.connection.cursor()
query = "INSERT INTO suspect (suspect_id, name, phone, gender ,address_street, address_number, birth_date ,Lawyer_lawyer_id ,status) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)"
try:
cur.execute(query, (suspect_id, name, phone, gender ,address_street, address_number, birth_date ,Lawyer_lawyer_id ,status))
mysql.connection.commit()
cur.close()
return jsonify({'message': 'Suspect added successfully'})
except Exception as e:
logging.error(f"Error executing query: {query}")
logging.error(str(e))
return jsonify({'error': f'Failed to add Suspect. {str(e)}'}), 500
elif request.method == 'DELETE':
data = request.get_json()
suspect_id = data.get('suspect_id')
cur = mysql.connection.cursor()
query = "DELETE FROM suspect WHERE suspect_id = %s"
try:
cur.execute(query, (suspect_id,))
mysql.connection.commit()
cur.close()
return jsonify({'message': 'Suspect deleted successfully'})
except Exception as e:
logging.error(f"Error executing query: {query}")
logging.error(str(e))
return jsonify({'error': f'Failed to delete Suspect. {str(e)}'}), 500
elif request.method == 'OPTIONS':
# Handle preflight request
response = make_response()
response.headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE")
return response
@app.route('/api/data/witnesses', methods=['GET', 'POST', 'DELETE', 'OPTIONS'])
def handle_data_witness():
if request.method == 'GET':
cur = mysql.connection.cursor()
cur.execute('SELECT * FROM witness')
witness_data = cur.fetchall()
cur.close()
return jsonify(witness_data)
elif request.method == 'POST':
data = request.json
witness_id = data.get('witness_id')
name = data.get('name')
birth_date = data.get('birth_date')
gender = data.get('gender')
phone = data.get('phone')
cur = mysql.connection.cursor()
query = "INSERT INTO policeman (witness_id, name, birth_date, gender, phone) VALUES (%s, %s, %s, %s, %s)"
try:
cur.execute(query, (witness_id, name, birth_date, gender, phone))
mysql.connection.commit()
cur.close()
return jsonify({'message': 'Witness added successfully'})
except Exception as e:
logging.error(f"Error executing query: {query}")
logging.error(str(e))
return jsonify({'error': f'Failed to add Witness. {str(e)}'}), 500
elif request.method == 'DELETE':
data = request.get_json()
witness_id = data.get('witness_id')
cur = mysql.connection.cursor()
query = "DELETE FROM witness WHERE witness_id = %s"
try:
cur.execute(query, (witness_id,))
mysql.connection.commit()
cur.close()
return jsonify({'message': 'Witness deleted successfully'})
except Exception as e:
logging.error(f"Error executing query: {query}")
logging.error(str(e))
return jsonify({'error': f'Failed to delete Witness. {str(e)}'}), 500
elif request.method == 'OPTIONS':
# Handle preflight request
response = make_response()
response.headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE")
return response
@app.route('/api/data/policemen', methods=['GET', 'POST', 'DELETE', 'OPTIONS'])
def handle_data_policeman():
if request.method == 'GET':
cur = mysql.connection.cursor()
cur.execute('SELECT * FROM policeman')
policeman_data = cur.fetchall()
cur.close()
return jsonify(policeman_data)
elif request.method == 'POST':
data = request.json
policeman_id = data.get('policeman_id')
name = data.get('name')
gender = data.get('gender')
phone = data.get('phone')
police_station_id = data.get('police_station_id')
specialty = data.get('specialty')
cur = mysql.connection.cursor()
query = "INSERT INTO policeman (policeman_id, name, gender, phone, police_station_id, specialty) VALUES (%s, %s, %s, %s, %s, %s)"
try:
cur.execute(query, (policeman_id, name, gender, phone, police_station_id, specialty))
mysql.connection.commit()
cur.close()
return jsonify({'message': 'Clue added successfully'})
except Exception as e:
logging.error(f"Error executing query: {query}")
logging.error(str(e))
return jsonify({'error': f'Failed to add clue. {str(e)}'}), 500
elif request.method == 'DELETE':
data = request.get_json()
policeman_id = data.get('policeman_id')
cur = mysql.connection.cursor()
query = "DELETE FROM clue WHERE policeman_id = %s"
try:
cur.execute(query, (policeman_id,))
mysql.connection.commit()
cur.close()
return jsonify({'message': 'Policeman deleted successfully'})
except Exception as e:
logging.error(f"Error executing query: {query}")
logging.error(str(e))
return jsonify({'error': f'Failed to delete Policeman. {str(e)}'}), 500
elif request.method == 'OPTIONS':
# Handle preflight request
response = make_response()
response.headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE")
return response
@app.route('/api/data/victims', methods=['GET', 'POST', 'DELETE', 'OPTIONS'])
def handle_data_victim():
if request.method == 'GET':
cur = mysql.connection.cursor()
cur.execute('SELECT * FROM victim')
victim_data = cur.fetchall()
cur.close()
return jsonify(victim_data)
elif request.method == 'POST':
data = request.json
victim_id = data.get('victim_id')
first_name = data.get('first_name')
last_name = data.get('last_name')
gender = data.get('gender')
birth_date = data.get('birth_date')
death_date = data.get('death_date')
description_of_death = data.get('description_of_death')
nationality = data.get('nationality')
Case_case_id = data.get('Case_case_id')
Medical_Examiner_medical_examiner_id = data.get('Medical_Examiner_medical_examiner_id')
Lawyer_lawyer_id = data.get('Lawyer_lawyer_id')
cur = mysql.connection.cursor()
query = "INSERT INTO policeman (victim_id, first_name, last_name, gender, birth_date, death_date, description_of_death, nationality, Case_case_id, Medical_Examiner_medical_examiner_id, Lawyer_lawyer_id) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
try:
cur.execute(query, (victim_id, first_name, last_name, gender, birth_date, death_date, description_of_death, nationality, Case_case_id, Medical_Examiner_medical_examiner_id, Lawyer_lawyer_id))
mysql.connection.commit()
cur.close()
return jsonify({'message': 'Victim added successfully'})
except Exception as e:
logging.error(f"Error executing query: {query}")
logging.error(str(e))
return jsonify({'error': f'Failed to add Victim. {str(e)}'}), 500
elif request.method == 'DELETE':
data = request.get_json()
victim_id = data.get('victim_id')
cur = mysql.connection.cursor()
query = "DELETE FROM victim WHERE victim_id = %s"
try:
cur.execute(query, (victim_id,))
mysql.connection.commit()
cur.close()
return jsonify({'message': 'Victim deleted successfully'})
except Exception as e:
logging.error(f"Error executing query: {query}")
logging.error(str(e))
return jsonify({'error': f'Failed to delete Victim. {str(e)}'}), 500
elif request.method == 'OPTIONS':
# Handle preflight request
response = make_response()
response.headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE")
return response
@app.route('/api/data/case-details', methods=['GET'])
def handle_data_case_details():
# data = request.get_json()
case_id = request.args.get('case_id')
# case_id = data.get('case_id', None)
# case_id = "C-491"
# Query for victims
query_victims = 'SELECT victim_id, last_name FROM victim WHERE Case_case_id = %s'
victims_data = execute_query(query_victims, (case_id,))
# Query for clues
query_clues = 'SELECT clue_id, description FROM clue WHERE Case_case_id = %s'
clues_data = execute_query(query_clues, (case_id,))
# Query for suspects
query_suspects = '''
SELECT suspect_id, name
FROM suspect
JOIN case_has_suspect ON suspect.suspect_id = case_has_suspect.Suspect_suspect_id
WHERE Case_case_id = %s
'''
suspects_data = execute_query(query_suspects, (case_id,))
# Query for witnesses
query_witnesses = '''
SELECT witness_id, name
FROM witness
JOIN case_has_witness ON witness.witness_id = case_has_witness.Witness_witness_id
WHERE Case_case_id = %s
'''
witnesses_data = execute_query(query_witnesses, (case_id,))
# Query for policemen
query_policemen = '''
SELECT policeman_id, name
FROM policeman
JOIN case_has_policeman ON policeman.policeman_id = case_has_policeman.Policeman_policeman_id
WHERE Case_case_id = %s
'''
policemen_data = execute_query(query_policemen, (case_id,))
return jsonify({
'victims': victims_data,
'clues': clues_data,
'suspects': suspects_data,
'witnesses': witnesses_data,
'policemen': policemen_data
})
if __name__ == '__main__':
app.run(debug=True)