This repository was archived by the owner on Mar 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathapp.py
More file actions
101 lines (86 loc) · 3.3 KB
/
Copy pathapp.py
File metadata and controls
101 lines (86 loc) · 3.3 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
from flask import Flask, jsonify, request
import sqlite3
app = Flask(__name__)
def get_db_connection():
conn = sqlite3.connect('students.db')
conn.row_factory = sqlite3.Row
return conn
def init_db():
conn = get_db_connection()
conn.execute('''CREATE TABLE IF NOT EXISTS students
(id INTEGER PRIMARY KEY, firstName TEXT, lastName TEXT, birthDate TEXT, age INTEGER)''')
conn.commit()
conn.close()
@app.route('/search/<term>', methods=['GET'])
def search(term):
conn = get_db_connection()
cur = conn.cursor()
cur.execute("SELECT * FROM students WHERE firstName LIKE ? OR lastName LIKE ?",
('%' + term + '%', '%' + term + '%'))
last_names = cur.fetchall()
conn.close()
return jsonify([row[0] for row in last_names])
@app.route('/add_student', methods=['POST'])
def add_student():
student_data = request.json
conn = get_db_connection()
cur = conn.cursor()
cur.execute("INSERT INTO students (firstName, lastName, birthDate, age) VALUES (?, ?, ?, ?)",
(student_data['firstName'], student_data['lastName'], student_data['birthDate'], student_data['age']))
conn.commit()
conn.close()
return jsonify({"success": True, "message": "Student added successfully"}), 201
@app.route('/students', methods=['GET'])
def get_students():
conn = get_db_connection()
cur = conn.cursor()
cur.execute("SELECT * FROM students")
students = cur.fetchall()
conn.close()
return jsonify([dict(row) for row in students])
@app.route('/student/<int:id>', methods=['GET'])
def get_student(id):
conn = get_db_connection()
cur = conn.cursor()
cur.execute("SELECT * FROM students WHERE id = ?", (id,))
student = cur.fetchone()
conn.close()
return jsonify(dict(student))
@app.route('/update_student/<int:id>', methods=['PUT'])
def update_student(id):
student_data = request.json
conn = get_db_connection()
cur = conn.cursor()
cur.execute("UPDATE students SET firstName = ?, lastName = ?, birthDate = ?, age = ? WHERE id = ?",
(student_data['firstName'], student_data['lastName'], student_data['birthDate'], student_data['age'], id))
conn.commit()
conn.close()
return jsonify({"success": True, "message": "Student updated successfully"})
@app.route('/delete_student/<int:id>', methods=['DELETE'])
def delete_student(id):
conn = get_db_connection()
cur = conn.cursor()
cur.execute("SELECT * FROM students WHERE id = ?", (id,))
student = cur.fetchone()
conn.close()
if student is None:
return jsonify({"success": False, "message": "Student not found"}), 404
conn = get_db_connection()
cur = conn.cursor()
cur.execute("DELETE FROM students WHERE id = ?", (id,))
conn.commit()
conn.close()
return jsonify({"success": True, "message": "Student deleted successfully"})
@app.route('/searchById/<int:id>', methods=['GET'])
def search_by_id(id):
conn = get_db_connection()
cur = conn.cursor()
cur.execute("SELECT * FROM students WHERE id = ?", (id,))
student = cur.fetchone()
conn.close()
if student is None:
return jsonify({"success": False, "message": "Student not found"}), 404
return jsonify({"success": True, "student": student})
if __name__ == '__main__':
init_db()
app.run(debug=True)