#11 Added endpoint to get enrolled courses
This commit is contained in:
@@ -80,6 +80,8 @@ class Course(db.Model):
|
|||||||
|
|
||||||
def to_dict(self) -> dict:
|
def to_dict(self) -> dict:
|
||||||
d = {}
|
d = {}
|
||||||
for f in ["id", "name", "description", "instructor", "created_at"]:
|
for f in ["id", "name", "description", "created_at"]:
|
||||||
d[f] = getattr(self, f)
|
d[f] = getattr(self, f)
|
||||||
|
|
||||||
|
d["instructor"] = User.query.get(self.instructor).username
|
||||||
return d
|
return d
|
||||||
|
|||||||
@@ -95,3 +95,17 @@ def create_course():
|
|||||||
|
|
||||||
return jsonify(c.to_dict())
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user