-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
153 lines (114 loc) · 5.09 KB
/
app.py
File metadata and controls
153 lines (114 loc) · 5.09 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
#Software Engineering Internship Challenge
#Candidate: Renzo Prats Silva Souza
#Importing libraries that will be used for develop the API
from flask import Flask, Response, request
from flask_sqlalchemy import SQLAlchemy
import json
import os
#Configuring the flask aplication and database
app = Flask(__name__)
app.app_context().push()
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:////{os.path.abspath("")}/students.db'
db = SQLAlchemy(app)
#Creating the object Student
class Student(db.Model):
#field that represent the identifier(id)
id = db.Column(db.Integer, primary_key=True)
#field that represent the name of the student
name = db.Column(db.String(200))
#field that represent the email of the student
email = db.Column(db.String(100), unique=True)
#field that represent the registration of the student
registration = db.Column(db.String(20), unique=True)
#method that converts the fields of the object to json
def to_json(self):
return {"id": self.id, "name": self.name, "email": self.email, "registration": self.registration}
db.drop_all()
db.create_all()
#function that generate the response for the CRUD functions
def generate_response(status, content_name, content, message=False):
body = {}
body[content_name] = content
if message:
body["message"] = message
return Response(json.dumps(body), status=status, mimetype="application/json")
#Routes
#route /students that return all students in the json format
@app.route("/students", methods=["GET"])
def select_students():
try:
#select all students objects
students_objects = Student.query.all()
#convert all students objects to json
students_json = [student.to_json() for student in students_objects]
#return a list of all student and a sucessfully message
return generate_response(200, "students", students_json, "OK")
#if try fails then returns empty and a error message
except:
return generate_response(400, "students", {}, "Error when filtering")
#route /student/<id> that return a student by passing its id
@app.route("/student/<id>", methods=["GET"])
def select_student(id):
#select student by its id
student_object = Student.query.filter_by(id=id).first()
#Try return the student filtered by its id
try:
student_json = student_object.to_json()
#return the student filtred and a sucessfully message
return generate_response(200, "student", student_json, "OK")
#if try fails then returns empty and a error message
except:
return generate_response(400, "student", {}, "Error when filtering")
#route /student that creates a student by the method HTTP POST
@app.route("/student", methods=["POST"])
def create_student():
body = request.get_json()
#try creates a student by passing the parameters name, email and registration
try:
student = Student(name=body["name"], email=body["email"], registration=body["registration"])
db.session.add(student)
db.session.commit()
#return the student created and a sucessfully message
return generate_response(201, "student", student.to_json(), "Successfully created")
#if try fails then returns empty and a error message
except:
return generate_response(400, "student", {}, "Error when registering")
#route /student/<id> that update a student by the method HTTP PUT
@app.route("/student/<id>", methods=["PUT"])
def update_student(id):
#filter the student by id
student_object = Student.query.filter_by(id=id).first()
body = request.get_json()
#try to update this student by the parameters that could be passed
try:
if('name' in body):
student_object.name = body['name']
if('email' in body):
student_object.email = body['email']
if('registration' in body):
student_object.registration = body['registration']
db.session.add(student_object)
db.session.commit()
#return the student updated and a sucessfully message
return generate_response(200, "student", student_object.to_json(), "Successfully updated")
#if try fails then returns empty and a error message
except:
return generate_response(400, "student", {}, "Error when updating")
#route /student/<id> that delete a student by the method HTTP DELETE
@app.route("/student/<id>", methods=["DELETE"])
def delete_student(id):
#filter the student by id
student_object = Student.query.filter_by(id=id).first()
#try to delete this student
try:
db.session.delete(student_object)
db.session.commit()
#return the deleted student and a sucessfully message
return generate_response(200, "student", student_object.to_json(), "Successfully deleted")
#if try fails then returns empty and a error message
except:
return generate_response(400, "student", {}, "Error deleting")
#run the app
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')