#35-course-code #36
@@ -46,6 +46,13 @@ class User(UserMixin, db.Model):
|
|||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def unenroll(self, c) -> bool:
|
||||||
|
if self.is_enrolled(c):
|
||||||
|
self.enrolled_courses.remove(c)
|
||||||
|
db.session.commit()
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
def to_dict(self) -> dict:
|
def to_dict(self) -> dict:
|
||||||
return {
|
return {
|
||||||
"id": self.id,
|
"id": self.id,
|
||||||
|
|||||||
@@ -124,3 +124,20 @@ def enroll_student(uid, 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)
|
||||||
|
|
||||||
|
|
||||||
|
@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