#6 Refactored both functionalities to one function that accepts both request methods
This commit is contained in:
@@ -108,7 +108,7 @@ def get_courses(id):
|
||||
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
|
||||
def enroll_student(uid, cid):
|
||||
u = User.query.get(uid)
|
||||
@@ -119,25 +119,14 @@ def enroll_student(uid, cid):
|
||||
if not c:
|
||||
return error_response(400, f"Course with id {cid} does not exist")
|
||||
|
||||
if not u.enroll(c):
|
||||
return error_response(400, f"User {uid} is already enrolled in course {cid}")
|
||||
if request.method == "POST":
|
||||
if not u.enroll(c):
|
||||
return error_response(400, f"User {uid} is already enrolled in course {cid}")
|
||||
|
||||
elif request.method == "DELETE":
|
||||
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)
|
||||
|
||||
|
||||
@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)
|
||||
|
||||
Reference in New Issue
Block a user