#6 Added unenroll endpoint by sending DELETE request to enroll endpoint

This commit is contained in:
2023-04-06 19:06:44 -04:00
parent 8ec489beb3
commit b71c23d706
2 changed files with 24 additions and 0 deletions

View File

@@ -124,3 +124,20 @@ def enroll_student(uid, cid):
resp = {"user": u.to_dict(), "course": c.to_dict()}
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):
return error_response(400, f"User {uid} is not enrolled in course {cid}")
resp = {"user": u.to_dict(), "course": c.to_dict()}
return jsonify(resp)