Commit a7687ca0 authored by Zac Wood's avatar Zac Wood
Browse files

Merge branch 'fix-api' into 'master'

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

See merge request !37
parents 432c7d48 98ef7e23
Pipeline #3804 failed with stages
in 33 minutes and 23 seconds
......@@ -3,13 +3,13 @@ class API::CourseListingsController < ApplicationController
short 'Working with courses and associated sections'
end
api :GET, '/course_listings', "Get all available courses and their sections"
param :subject, String, desc: 'Course subject, e.g. "CS" or "ACCT"'
param :number, Integer, desc: 'Course number, e.g. "112"'
def index
# Make a separate list so that we can include sections
@courses = API::CourseListingsHelper::CourseListing.wrap(Course.fetch(params).all)
# api :GET, '/course_listings', "Get all available courses and their sections"
# param :subject, String, desc: 'Course subject, e.g. "CS" or "ACCT"'
# param :number, Integer, desc: 'Course number, e.g. "112"'
# def index
# # Make a separate list so that we can include sections
# @courses = API::CourseListingsHelper::CourseListing.wrap(Course.fetch(params).all)
render json: @courses
end
# render json: @courses
# end
end
......@@ -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 :crn, String, desc: "Get the course section with this CRN"
param :instructor, String, desc: "Get course sections being taught by this instructor"
param :query, String, desc: 'A generic query ex. "CS 110"'
def index
@sections = CourseSection.fetch(params).all
render json: @sections
@sections = CourseSection
.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
......@@ -8,8 +8,30 @@ class API::CoursesController < ApplicationController
param :subject, String, desc: 'Course subject, e.g. "CS" or "ACCT"'
param :course_number, Integer, desc: 'Course number, e.g. "112"'
def index
@courses = Course.fetch(params).all
render json: @courses
@courses = Course.where(semester_id: params[:semester_id])
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
api :GET, '/courses/:id', "Get a list of all course sections for the course with the given id."
......
This diff is collapsed.
This diff is collapsed.
require 'test_helper'
class API::CourseListingsControllerTest < ActionDispatch::IntegrationTest
test 'should grab sections for course' do
get course_listings_url course_id: courses(:cs112).id, semester_id: semesters(:fall2018).id
assert_response :success
# test 'should grab sections for course' do
# get course_listings_url course_id: courses(:cs112).id, semester_id: semesters(:fall2018).id
# assert_response :success
listing_returned = JSON.parse @response.body
assert listing_returned.size.positive?
# listing_returned = JSON.parse @response.body
# 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)
end
# assert_equal(listing_returned[0]["sections"].length, 2)
# 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