-
Notifications
You must be signed in to change notification settings - Fork 1
/
course_db.py
78 lines (70 loc) · 2.89 KB
/
course_db.py
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
from flask import Flask, jsonify, request, Response
from courseModel import *
from settings import *
import json
@app.route('/courses')
def get_courses():
return jsonify({'courses': Course.get_all_courses()})
@app.route('/courses/<string:code>')
def get_course(code):
result = Course.get_course(code=code)
return jsonify(result)
@app.route('/courses', methods=['POST'])
def post_course():
data = request.get_json()
# validate the data
if(data["code"] != ""):
Course.add_course(code=data["code"],instructor=data["instructor"],name=data["name"])
response = Response("", status=201, mimetype="application/json")
response.headers["Location"] = "/courses/"+str(data["code"])
else:
error_message = {
"error": "course code must be provided"
}
response = Response(json.dumps(error_message), status=400, mimetype="application/json")
return response
# PUT request need to send all data to update, but PATCH can do partial update
@app.route('/courses/<string:code>', methods=['PUT'])
def put_course(code):
data = request.get_json()
if("instructor" in data and "name" in data):
Course.update_course(code=code,instructor=data["instructor"],name=data["name"])
response = Response("", status=204, mimetype="application/json")
response.headers["Location"] = "/courses/"+str(code)
else:
error_message = {
"error": "Course name and instructor's name must be provided"
}
response = Response(json.dumps(error_message), status=400, mimetype="application/json")
return response
# PUT request need to send all data to update, but PATCH can do partial update
@app.route('/courses/<string:code>', methods=['PATCH'])
def patch_course(code):
data = request.get_json()
if("instructor" in data):
Course.update_course(code=code,instructor=data["instructor"])
elif("name" in data):
Course.update_course(code=code,name=data["name"])
else:
error_message = {
"error": "Course name or instructor's name must be provided"
}
response = Response(json.dumps(error_message), status=400, mimetype="application/json")
return response
response = Response("", status=204, mimetype="application/json")
response.headers["Location"] = "/courses/"+str(code)
return response
@app.route('/courses/<string:code>', methods=['DELETE'])
def delete_course(code):
if(Course.delete_course(code=code)):
response = Response("", status=204, mimetype="application/json")
response.headers["Location"] = "/courses/"
return response
# if course is not found
error_message = {
"error": "Course code is not found in the courses"
}
response = Response(json.dumps(error_message), status=400, mimetype="application/json")
return response
# Run the app in port 5000
app.run(port=5000, host="0.0.0.0")