#11 Added endpoint to get enrolled courses

This commit is contained in:
2023-04-06 16:40:40 -04:00
parent 5beaf2dba4
commit 84fb1e9f4b
2 changed files with 17 additions and 1 deletions

View File

@@ -80,6 +80,8 @@ class Course(db.Model):
def to_dict(self) -> dict:
d = {}
for f in ["id", "name", "description", "instructor", "created_at"]:
for f in ["id", "name", "description", "created_at"]:
d[f] = getattr(self, f)
d["instructor"] = User.query.get(self.instructor).username
return d

View File

@@ -95,3 +95,17 @@ def create_course():
return jsonify(c.to_dict())
@bp.route("/course", methods=["GET"])
def get_courses():
data = request.get_json()
if "user_id" not in data:
return error_response(400, "Must supply user_id")
u = User.query.get(data["user_id"])
d = {"courses": []}
for c in u.enrolled_courses.all():
d["courses"].append(c.to_dict())
resp = jsonify(d)
return resp