Commit 98ef7e23 authored by Zac Wood's avatar Zac Wood

Fixes the broken API methods which didn't filter by semester

parent 432c7d48
Pipeline #3797 passed with stage
in 2 minutes and 51 seconds
...@@ -3,13 +3,13 @@ class API::CourseListingsController < ApplicationController ...@@ -3,13 +3,13 @@ class API::CourseListingsController < ApplicationController
short 'Working with courses and associated sections' short 'Working with courses and associated sections'
end end
api :GET, '/course_listings', "Get all available courses and their sections" # api :GET, '/course_listings', "Get all available courses and their sections"
param :subject, String, desc: 'Course subject, e.g. "CS" or "ACCT"' # param :subject, String, desc: 'Course subject, e.g. "CS" or "ACCT"'
param :number, Integer, desc: 'Course number, e.g. "112"' # param :number, Integer, desc: 'Course number, e.g. "112"'
def index # def index
# Make a separate list so that we can include sections # # Make a separate list so that we can include sections
@courses = API::CourseListingsHelper::CourseListing.wrap(Course.fetch(params).all) # @courses = API::CourseListingsHelper::CourseListing.wrap(Course.fetch(params).all)
render json: @courses # render json: @courses
end # end
end end
...@@ -9,10 +9,43 @@ class API::CourseSectionsController < ApplicationController ...@@ -9,10 +9,43 @@ class API::CourseSectionsController < ApplicationController
param :course_id, Integer, desc: "Only get the course sections belonging to the course with this ID" param :course_id, Integer, desc: "Only get the course sections belonging to the course with this ID"
param :crn, String, desc: "Get the course section with this CRN" param :crn, String, desc: "Get the course section with this CRN"
param :instructor, String, desc: "Get course sections being taught by this instructor" param :instructor, String, desc: "Get course sections being taught by this instructor"
param :query, String, desc: 'A generic query ex. "CS 110"'
def index def index
@sections = CourseSection.fetch(params).all @sections = CourseSection
render json: @sections .select('course_sections.*, courses.semester_id, instructors.name AS instructor_name')
.joins(:course).where('courses.semester_id = ?', @semester.id)
.joins(:instructor)
if params.key?(:course_id)
@sections = @sections.where(course_id: params[:course_id])
end
if params.key?(:crn)
@sections = @sections.where(crn: params[:crn])
end
if params.key?(:instructor)
@sections = @sections.where('UPPER(instructors.name) LIKE UPPER(?)', "%#{params[:instructor]}%")
end
# @sections = CourseSection.fetch(params).all
res = @sections.map do |s|
{
id: s.id,
semester_id: s.semester_id,
course_id: s.course_id,
name: s.name,
crn: s.crn,
title: s.title,
instructor_name: s.instructor_name,
section_type: s.section_type,
start_date: s.start_date,
end_date: s.end_date,
days: s.days,
start_time: s.start_time,
end_time: s.end_time,
location: s.location,
}
end
render json: res
end end
end end
...@@ -8,8 +8,30 @@ class API::CoursesController < ApplicationController ...@@ -8,8 +8,30 @@ class API::CoursesController < ApplicationController
param :subject, String, desc: 'Course subject, e.g. "CS" or "ACCT"' param :subject, String, desc: 'Course subject, e.g. "CS" or "ACCT"'
param :course_number, Integer, desc: 'Course number, e.g. "112"' param :course_number, Integer, desc: 'Course number, e.g. "112"'
def index def index
@courses = Course.fetch(params).all @courses = Course.where(semester_id: params[:semester_id])
render json: @courses
if params.key?(:subject)
@courses = @courses.where("UPPER(courses.subject) LIKE ?", "%#{params[:subject]}%")
end
if params.key?(:course_number)
@courses = @courses.where(course_number: params[:course_number])
end
result = @courses.map do |c|
{
id: c.id,
semester_id: c.semester_id,
subject: c.subject,
course_number: c.course_number,
description: c.description,
credits: c.credits,
title: c.title,
prereqs: c.prereqs
}
end
render json: result
end end
api :GET, '/courses/:id', "Get a list of all course sections for the course with the given id." api :GET, '/courses/:id', "Get a list of all course sections for the course with the given id."
......
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
require 'test_helper' require 'test_helper'
class API::CourseListingsControllerTest < ActionDispatch::IntegrationTest class API::CourseListingsControllerTest < ActionDispatch::IntegrationTest
test 'should grab sections for course' do # test 'should grab sections for course' do
get course_listings_url course_id: courses(:cs112).id, semester_id: semesters(:fall2018).id # get course_listings_url course_id: courses(:cs112).id, semester_id: semesters(:fall2018).id
assert_response :success # assert_response :success
listing_returned = JSON.parse @response.body # listing_returned = JSON.parse @response.body
assert listing_returned.size.positive? # assert listing_returned.size.positive?
assert listing_returned[0].include?("sections") # assert listing_returned[0].include?("sections")
assert_equal(listing_returned[0]["sections"].length, 2) # assert_equal(listing_returned[0]["sections"].length, 2)
end # end
end end
...@@ -6,7 +6,10 @@ class API::CourseSectionsControllerTest < ActionDispatch::IntegrationTest ...@@ -6,7 +6,10 @@ class API::CourseSectionsControllerTest < ActionDispatch::IntegrationTest
assert_response :success assert_response :success
sections_returned = JSON.parse @response.body sections_returned = JSON.parse @response.body
num_sections = CourseSection.where(course_id: courses(:cs112).id).count num_sections = CourseSection
.joins(course: :semester)
.where('semesters.id = ?', semesters(:fall2018).id)
.where(course_id: courses(:cs112).id).count
assert_equal num_sections, sections_returned.count assert_equal num_sections, sections_returned.count
end end
......
...@@ -6,7 +6,7 @@ class API::CoursesControllerTest < ActionDispatch::IntegrationTest ...@@ -6,7 +6,7 @@ class API::CoursesControllerTest < ActionDispatch::IntegrationTest
assert_response :success assert_response :success
courses_returned = JSON.parse @response.body courses_returned = JSON.parse @response.body
courses_count = Course.all.count courses_count = Course.where(semester_id: semesters(:fall2018).id).count
assert_equal courses_count, courses_returned.count assert_equal courses_count, courses_returned.count
end end
...@@ -15,7 +15,7 @@ class API::CoursesControllerTest < ActionDispatch::IntegrationTest ...@@ -15,7 +15,7 @@ class API::CoursesControllerTest < ActionDispatch::IntegrationTest
assert_response :success assert_response :success
courses_returned = JSON.parse @response.body courses_returned = JSON.parse @response.body
courses_count = Course.where(subject: "CS").count courses_count = Course.where(subject: "CS", semester_id: semesters(:fall2018).id).count
assert_equal courses_count, courses_returned.count assert_equal courses_count, courses_returned.count
end end
...@@ -25,7 +25,7 @@ class API::CoursesControllerTest < ActionDispatch::IntegrationTest ...@@ -25,7 +25,7 @@ class API::CoursesControllerTest < ActionDispatch::IntegrationTest
assert_response :success assert_response :success
courses_returned = JSON.parse @response.body courses_returned = JSON.parse @response.body
courses_count = Course.where(subject: "CS", course_number: "112").count courses_count = Course.where(subject: "CS", course_number: "112", semester_id: semesters(:fall2018).id).count
assert_equal courses_count, courses_returned.count assert_equal courses_count, courses_returned.count
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment