Skip to content

Commit e69cfaf

Browse files
authored
Add get course by id endpoint (#98)
1 parent 50397f1 commit e69cfaf

4 files changed

Lines changed: 27 additions & 2 deletions

File tree

src/app/coursegrab/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from flask import Blueprint
2+
from app.coursegrab.controllers.get_course_by_id_controller import *
23
from app.coursegrab.controllers.get_section_controller import *
34
from app.coursegrab.controllers.hello_world_controller import *
45
from app.coursegrab.controllers.initialize_session_controller import *
@@ -16,6 +17,7 @@
1617
coursegrab = Blueprint("coursegrab", __name__, url_prefix="/api")
1718

1819
controllers = [
20+
GetCourseByIDController(),
1921
GetSectionController(),
2022
HelloWorldController(),
2123
InitializeSessionController(),
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from . import *
2+
3+
class GetCourseByIDController(AppDevController):
4+
def get_path(self):
5+
return "/courses/<id>/"
6+
7+
def get_methods(self):
8+
return ["GET"]
9+
10+
@authorize_user_selective
11+
def content(self, **kwargs):
12+
user = kwargs.get("user")
13+
user_id = user.id if user else -1
14+
15+
course_id = int(request.view_args["id"])
16+
17+
course = courses_dao.get_course_by_id(course_id)
18+
return course.serialize_with_user(user_id)

src/app/coursegrab/dao/courses_dao.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
from ..utils.constants import NUM_SEARCH_RESULT
33

44

5+
def get_course_by_id(course_id):
6+
return Course.query.filter_by(id=course_id).first()
7+
58
def get_course_by_subject_and_course_num(subject_code, course_num):
69
return Course.query.filter_by(subject_code=subject_code, course_num=course_num).first()
710

@@ -25,8 +28,8 @@ def search_courses(query):
2528

2629
def find_query_index(course, key):
2730
try:
28-
return course.search_string.lower().index(key)
29-
except(Error):
31+
return course.search_string.lower().index(key.lower())
32+
except(ValueError):
3033
return -1
3134

3235
def clear_table():

src/app/coursegrab/models/course.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def __init__(self, **kwargs):
1818

1919
def serialize(self):
2020
return {
21+
"id": self.id,
2122
"subject_code": self.subject_code,
2223
"course_num": self.course_num,
2324
"title": self.title,
@@ -27,6 +28,7 @@ def serialize(self):
2728
# Adds `is_tracking` field for each section
2829
def serialize_with_user(self, user_id):
2930
return {
31+
"id": self.id,
3032
"subject_code": self.subject_code,
3133
"course_num": self.course_num,
3234
"title": self.title,

0 commit comments

Comments
 (0)