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