-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
237 lines (201 loc) · 7.37 KB
/
app.py
File metadata and controls
237 lines (201 loc) · 7.37 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
from flask import Flask, request, jsonify
import controllers.customer_controller as customer_controller
import controllers.engineer_controller as engineer_controller
import controllers.complaint_controller as complaint_controller
import controllers.job_controller as job_controller
app = Flask(__name__)
# -------> Customers endpoints routes <-------
@app.route('/customers', methods=['POST'])
def create_customer():
try:
data = request.json
customer_controller.create_customer(data['name'], data['email'], data['phone_number'])
return jsonify({"Success":"Customer added into the database!!"}), 201
except Exception as e:
return jsonify({"Error": str(e)}), 400
@app.route('/customers', methods=['GET'])
def get_all_customers():
try:
customers = customer_controller.get_all_customers()
if customers:
return jsonify(customers)
return jsonify({"Error": "Customers not found"}), 404
except Exception as e:
return jsonify({"Error": str(e)}), 400
@app.route('/customers/<int:customer_id>', methods=['GET'])
def get_customer(customer_id):
try:
customer = customer_controller.get_customer(customer_id)
if customer:
return jsonify(customer)
return jsonify({"Error": "Customer not found"}), 404
except Exception as e:
return jsonify({"Error": str(e)}), 400
@app.route('/customers/<int:customer_id>', methods=['PUT'])
def update_customer(customer_id):
try:
data = request.json
customer = customer_controller.update_customer(
customer_id=customer_id,
name=data['name'],
email=data['email'],
phone_number=data['phone_number'],
)
if customer:
return jsonify(customer)
return jsonify({"Error": "Customer not found"}), 404
except Exception as e:
return jsonify({"Error": str(e)}), 400
@app.route('/customers/<int:customer_id>', methods=['DELETE'])
def api_delete_customer(customer_id):
try:
customer_controller.delete_customer(customer_id)
return jsonify({"Success": "Customer deleted"}), 200
except Exception as e:
return jsonify({"Error": str(e)}), 400
# -------> Engineers endpoints routes <-------
@app.route('/engineers', methods=['POST'])
def create_engineer():
try:
data = request.json
engineer = engineer_controller.create_engineer(data['name'], data['designation'])
return jsonify(engineer), 201
except Exception as e:
return jsonify({"Error": str(e)}), 400
@app.route('/engineers/<int:engineer_id>', methods=['GET'])
def get_engineer(engineer_id):
try:
engineer = engineer_controller.get_engineer(engineer_id)
if engineer:
return jsonify(engineer)
return jsonify({"Error": "Engineer not found"}), 404
except Exception as e:
return jsonify({"Error": str(e)}), 400
@app.route('/engineers/<int:engineer_id>', methods=['PUT'])
def update_engineer(engineer_id):
try:
data = request.json
engineer = engineer_controller.update_engineer(
engineer_id,
name=data['name'],
designation=data['designation'],
)
if engineer:
return jsonify(engineer)
return jsonify({"Error": "Engineer not found"}), 404
except Exception as e:
return jsonify({"Error": str(e)}), 400
# -------> Complaints endpoints routes <-------
@app.route('/complaints', methods=['POST'])
def create_complaint():
try:
data = request.json
complaint_controller.create_complaint(
customer_id=data['customer_id'],
category=data['category'],
description=data['description'],
priority=data['priority'],
engineer_id=data['engineer_id'],
)
return jsonify({"Success":"Complaint added into the database!!"}), 201
except Exception as e:
return jsonify({"Error": str(e)}), 400
@app.route('/complaints', methods=['GET'])
def get_complaints():
try:
complaints = complaint_controller.get_all_complaints()
if complaints:
return jsonify(complaints)
return jsonify({"Error": "Complaint not found"}), 404
except Exception as e:
return jsonify({"Error": str(e)}), 400
@app.route('/complaints/<int:complaint_id>', methods=['GET'])
def get_complaint(complaint_id):
try:
complaint = complaint_controller.get_complaint(complaint_id)
if complaint:
return jsonify(complaint)
return jsonify({"Error": "Complaint not found"}), 404
except Exception as e:
return jsonify({"Error": str(e)}), 400
@app.route('/complaints/<int:complaint_id>', methods=['PUT'])
def update_complaint(complaint_id):
try:
data = request.json
complaint = complaint_controller.update_complaint(
complaint_id,
status=data['status'],
engineer_id=data['engineer_id'],
)
if complaint:
return jsonify(complaint)
return jsonify({"Error": "Complaint not found"}), 404
except Exception as e:
return jsonify({"Error": str(e)}), 400
@app.route('/complaints/<int:complaint_id>', methods=['DELETE'])
def delete_complaint(complaint_id):
try:
complaint = complaint_controller.delete_complaint(complaint_id)
if complaint:
return jsonify(complaint)
return jsonify({"Error": "Complaint not found"}), 404
except Exception as e:
return jsonify({"Error": str(e)}), 400
# -------> Jobs endpoints routes <-------
@app.route('/jobs', methods=['POST'])
def create_job():
try:
data = request.json
job = job_controller.create_job(
complaint_id=data['complaint_id'],
engineer_id=data['engineer_id'],
status=data['status'],
comment=data['comment'],
)
return jsonify(job), 201
except Exception as e:
return jsonify({"Error": str(e)}), 400
@app.route('/jobs/<int:job_id>', methods=['GET'])
def get_job(job_id):
try:
job = job_controller.get_job(job_id)
if job:
return jsonify(job)
return jsonify({"Error": "Job not found"}), 404
except Exception as e:
return jsonify({"Error": str(e)}), 400
@app.route('/jobs', methods=['GET'])
def get_jobs():
job_status = request.args.get('status')
if job_status:
try:
jobs = job_controller.get_status_jobs(job_status)
if jobs:
return jsonify(jobs)
return jsonify({"Error": "Job not found"}), 404
except Exception as e:
return jsonify({"Error": str(e)}), 400
else:
try:
jobs = job_controller.get_all_jobs()
if jobs:
return jsonify(jobs)
return jsonify({"Error": "Job not found"}), 404
except Exception as e:
return jsonify({"Error": str(e)}), 400
@app.route('/jobs/<int:job_id>', methods=['PUT'])
def update_job(job_id):
try:
data = request.json
job = job_controller.update_job(
job_id,
status=data['status'],
comment=data['comment'],
)
if job:
return jsonify(job)
return jsonify({"Error": "Job not found"}), 404
except Exception as e:
return jsonify({"Error": str(e)}), 400
if __name__ == "__main__":
app.run(debug=True)