#6 Refactored both functionalities to one function that accepts both request methods

This commit is contained in:
2023-04-06 21:25:34 -04:00
parent b71c23d706
commit a789c2eb83
2 changed files with 9 additions and 20 deletions

View File

@@ -108,7 +108,7 @@ def get_courses(id):
return resp return resp
@bp.route("/user/<int:uid>/enroll/<int:cid>", methods=["POST"]) @bp.route("/user/<int:uid>/enroll/<int:cid>", methods=["POST", "DELETE"])
@login_required @login_required
def enroll_student(uid, cid): def enroll_student(uid, cid):
u = User.query.get(uid) u = User.query.get(uid)
@@ -119,25 +119,14 @@ def enroll_student(uid, cid):
if not c: if not c:
return error_response(400, f"Course with id {cid} does not exist") return error_response(400, f"Course with id {cid} does not exist")
if request.method == "POST":
if not u.enroll(c): if not u.enroll(c):
return error_response(400, f"User {uid} is already enrolled in course {cid}") return error_response(400, f"User {uid} is already enrolled in course {cid}")
resp = {"user": u.to_dict(), "course": c.to_dict()} elif request.method == "DELETE":
return jsonify(resp)
@bp.route("/user/<int:uid>/enroll/<int:cid>", methods=["DELETE"])
@login_required
def unenroll_student(uid, cid):
u = User.query.get(uid)
if not u:
return error_response(400, f"User with id {uid} does not exist")
c = Course.query.get(cid)
if not c:
return error_response(400, f"Course with id {cid} does not exist")
if not u.unenroll(c): if not u.unenroll(c):
return error_response(400, f"User {uid} is not enrolled in course {cid}") return error_response(400, f"User {uid} is not enrolled in course {cid}")
resp = {"user": u.to_dict(), "course": c.to_dict()} resp = {"user": u.to_dict(), "course": c.to_dict()}
return jsonify(resp) return jsonify(resp)

View File

@@ -22,7 +22,7 @@
work correctly both with client-side routing and a non-root public URL. work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`. Learn how to configure a non-root public URL by running `npm run build`.
--> -->
<title>React App</title> <title>LearningTree</title>
</head> </head>
<body> <body>