-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
118 lines (87 loc) · 3.25 KB
/
app.py
File metadata and controls
118 lines (87 loc) · 3.25 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
from flask import Flask, jsonify, render_template
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__, template_folder='main')
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://davidgaither@localhost/school'
db = SQLAlchemy(app)
class Students(db.Model):
id = db.Column(db.Integer, primary_key=True)
first_name = db.Column(db.String(20))
last_name= db.Column(db.String(20))
age = db.Column(db.Integer)
subject = db.Column(db.Integer)
class Subjects(db.Model):
id = db.Column(db.Integer, primary_key=True)
subject = db.Column(db.String(30))
class Teachers(db.Model):
id = db.Column(db.Integer, primary_key=True)
first_name = db.Column(db.String(20))
last_name= db.Column(db.String(20))
age = db.Column(db.Integer)
subject = db.Column(db.Integer)
#GET - graabbing info
#PUT - updating info within db
#DELETE - deleting an entry within the db
#POST - creating an entry to the db
@app.route("/")
def index():
return render_template("index.html")
@app.route('/api/v1/students/', methods=['GET'])
def get_all_students():
students = Students.query.all()
json_students = []
for stud in students:
subject = Subjects.query.get(stud.subject)
subject_name = subject.subject
teacher = Teachers.query.filter_by(subject=stud.subject).first()
teacher_name = f"{teacher.first_name} {teacher.last_name}"
student_data = {
'id': stud.id,
'first_name': stud.first_name,
'last_name': stud.last_name,
'age': stud.age,
'class': {
'subject': subject_name,
'teacher': teacher_name
}
}
json_students.append(student_data)
response = jsonify(json_students)
return response
@app.route('/api/v1/teachers/', methods=['GET'])
def get_all_teachers():
teachers = Teachers.query.all()
json_teachers = []
for teach in teachers:
subject = Subjects.query.get(teach.subject)
subject_name = subject.subject
students = Students.query.filter_by(subject=teach.subject).all()
student_names = [f"{student.first_name} {student.last_name}" for student in students]
teacher_data = {
'first_name': teach.first_name,
'last_name': teach.last_name,
'age': teach.age,
'subject': {
'subject': subject_name,
'students': student_names
}
}
json_teachers.append(teacher_data)
response = jsonify(json_teachers)
return response
@app.route('/api/v1/subjects/', methods=['GET'])
def get_all_subjects():
subjects = Subjects.query.all()
json_subjects = []
for subj in subjects:
teacher = Teachers.query.filter_by(subject=subj.id).first()
students = Students.query.filter_by(subject=subj.id).all()
student_data = [f"{student.first_name} {student.last_name}" for student in students]
subject_data = {
'subject': subj.subject,
'teacher': f"{teacher.first_name} {teacher.last_name}",
'students': student_data
}
json_subjects.append(subject_data)
response = jsonify(json_subjects)
return response
app.run(port=8000, debug=True)