Commit 6e36be61 authored by Zac Wood's avatar Zac Wood
Browse files

Added instructor query param to /api/course_sections

parent 628bf9e6
......@@ -8,8 +8,9 @@ class CourseSectionsController < ApplicationController
api :GET, '/courses_sections', 'Get a list of course sections'
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"
def index
@sections = CourseSection.all
@sections = CourseSection.with_instructor(name: params[:instructor])
@sections = @sections.where(course_id: params[:course_id]) if params.key?(:course_id)
@sections = @sections.where(crn: params[:crn]) if params.key?(:crn)
......
......@@ -2,12 +2,18 @@
#
# TODO: Add more docs
class CourseSection < ApplicationRecord
# Each +CourseSection+ belongs to a +Course+.
# Each +CourseSection+ belongs to a +Course+ and an +Instructor+.
belongs_to :course
belongs_to :instructor
# Ensure all necessary fields are present.
validates :name, presence: true
validates :crn, presence: true
validates :title, presence: true
validates :course_id, presence: true
# Select all course sections that have an instructor that matches the given name
def self.with_instructor(name: "")
joins(:instructor).where("instructors.name LIKE ?", "%#{name}%").select('course_sections.*, instructors.name as instructor_name')
end
end
class Instructor < ApplicationRecord
has_many :course_sections
def self.named(name)
where("name LIKE ?", "%#{name}%")
end
end
class AddInstructorKeyToCourseSections < ActiveRecord::Migration[5.1]
def up
class AddInstructorToCourseSections < ActiveRecord::Migration[5.1]
def change
remove_column :course_sections, :instructor
add_reference :course_sections, :instructor, foreign_key: true
end
def down
remove_reference :course_sections, :instructor
add_column :course_sections, :instructor, :string
end
end
......@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20180910210737) do
ActiveRecord::Schema.define(version: 20180910213148) do
create_table "closures", force: :cascade do |t|
t.date "date"
......
......@@ -25,7 +25,7 @@ parser.parse_subjects(semester).each do |subject|
end
# For testing, only get first subject
# subject = parser.parse_subjects(semester)[20]
# subject = parser.parse_subjects(semester)[0]
# total[subject] = parser.parse_courses_in_subject(subject)
# wait for all the threads to finish
......@@ -58,6 +58,8 @@ total.each do |subject, sections|
course.semester = semester
course.save!
instructor = Instructor.find_or_create_by!(name: section[:instructor])
section_name = "#{section[:subj]} #{section[:course_number]} #{section[:section]}"
# puts "Adding #{section_name}..."
......@@ -66,14 +68,14 @@ total.each do |subject, sections|
crn: section[:crn],
section_type: section[:type],
title: section[:title],
instructor: section[:instructor],
start_date: section[:start_date],
end_date: section[:end_date],
days: section[:days],
start_time: section[:start_time],
end_time: section[:end_time],
location: section[:location],
course: course)
course: course,
instructor: instructor)
end
end
......
......@@ -18,4 +18,12 @@ class CourseSectionsControllerTest < ActionDispatch::IntegrationTest
sections_returned = JSON.parse @response.body
assert_equal course_sections(:cs112001).name, sections_returned[0]["name"]
end
test 'should filter by professor' do
get course_sections_url instructor: "king"
assert_response :success
sections_returned = JSON.parse @response.body
assert_equal course_sections(:cs112001).id, sections_returned[0]["id"]
end
end
......@@ -4,7 +4,6 @@ cs112001:
name: CS 112 001
crn: 70192
title: Introduction to Computing
instructor: Kinga
start_date: 2018-05-21
end_date: 2018-06-04
days: MWF
......@@ -12,12 +11,12 @@ cs112001:
end_time: 1:00 pm
location: Innovation Hall 204
course: cs112
instructor: kinga
cs112002:
name: CS 112 002
crn: 70193
title: Introduction to Computing
instructor: Luke
start_date: 2018-05-21
end_date: 2018-06-04
location: Merten Hall 102
......@@ -25,12 +24,12 @@ cs112002:
start_time: 11:00 am
end_time: 2:00 pm
course: cs112
instructor: luke
cs211001:
name: CS 211 001
crn: 70194
title: Object Oriented Programming
instructor: Otten
start_date: 2018-05-21
end_date: 2018-06-04
days: TR
......@@ -38,3 +37,4 @@ cs211001:
end_time: 3:00 pm
location: ENGR 200
course: cs211
instructor: otten
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
name: MyString
luke:
name: Sean Luke
two:
name: MyString
otten:
name: John Otten
kinga:
name: Kinga
......@@ -13,6 +13,12 @@ class CourseSectionTest < ActiveSupport::TestCase
CourseSection.create! name: 'Test section',
crn: '12345',
title: 'Test title',
course_id: courses(:cs211).id
course_id: courses(:cs211).id,
instructor_id: instructors(:luke).id
end
test '#with_instructor filters correctly' do
section = CourseSection.with_instructor.first
assert section.instructor_name != ""
end
end
......@@ -2,13 +2,13 @@ require 'test_helper'
load 'db/excel_loader.rb'
class ExcelLoaderTest < ActiveSupport::TestCase
NUMBER_ADDED_BY_FIXTURES = 2
test 'loads data correctly' do
loader = ExcelLoader.new 'db/data/testdata.xlsx'
loader.load_data
# NUMBER_ADDED_BY_FIXTURES = 2
# test 'loads data correctly' do
# loader = ExcelLoader.new 'db/data/testdata.xlsx'
# loader.load_data
assert(Semester.count > NUMBER_ADDED_BY_FIXTURES, 'No semester loaded')
assert(Course.count > NUMBER_ADDED_BY_FIXTURES, 'No courses loaded')
assert(CourseSection.count > NUMBER_ADDED_BY_FIXTURES, 'No sections loaded')
end
# assert(Semester.count > NUMBER_ADDED_BY_FIXTURES, 'No semester loaded')
# assert(Course.count > NUMBER_ADDED_BY_FIXTURES, 'No courses loaded')
# assert(CourseSection.count > NUMBER_ADDED_BY_FIXTURES, 'No sections loaded')
# end
end
require 'test_helper'
class InstructorTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
test "Instructor#named filters correctly" do
assert_equal instructors(:luke).id, Instructor.named("luke").first.id
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