Commit 8970cc67 authored by Zac Wood's avatar Zac Wood
Browse files

Merge branch '12-instructor-model' into 'master'

Resolve "Add Instructor Model"

Closes #12 and #21

See merge request !23
parents 512d3abf 9982dd99
Pipeline #2888 passed with stage
in 2 minutes and 13 seconds
...@@ -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
has_many :course_sections
def self.named(name)
where("name LIKE ?", "%#{name}%")
end
end
class CreateInstructors < ActiveRecord::Migration[5.1]
def change
create_table :instructors do |t|
t.string :name
t.timestamps
end
end
end
class AddInstructorToCourseSections < ActiveRecord::Migration[5.1]
def change
remove_column :course_sections, :instructor
add_reference :course_sections, :instructor, foreign_key: true
end
end
...@@ -117,7 +117,9 @@ module PatriotWeb ...@@ -117,7 +117,9 @@ module PatriotWeb
data[:end_date] = dates[1] data[:end_date] = dates[1]
data[:type] = details[5].text data[:type] = details[5].text
data[:instructor] = details[6].text
# Get rid of extra spaces and parentheses at the end of prof. names
data[:instructor] = details[6].text.gsub(/ *\(.*\).*/, "").gsub(/ +/, " ")
result << data result << data
i += 5 # skip to what we think is the next title i += 5 # skip to what we think is the next title
......
...@@ -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: 20180619011649) 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,6 @@ ActiveRecord::Schema.define(version: 20180619011649) do ...@@ -25,7 +25,6 @@ ActiveRecord::Schema.define(version: 20180619011649) do
t.string "crn" t.string "crn"
t.string "section_type" t.string "section_type"
t.string "title" t.string "title"
t.string "instructor"
t.date "start_date" t.date "start_date"
t.date "end_date" t.date "end_date"
t.string "days" t.string "days"
...@@ -39,7 +38,9 @@ ActiveRecord::Schema.define(version: 20180619011649) do ...@@ -39,7 +38,9 @@ ActiveRecord::Schema.define(version: 20180619011649) do
t.integer "course_id" t.integer "course_id"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.integer "instructor_id"
t.index ["course_id"], name: "index_course_sections_on_course_id" t.index ["course_id"], name: "index_course_sections_on_course_id"
t.index ["instructor_id"], name: "index_course_sections_on_instructor_id"
end end
create_table "courses", force: :cascade do |t| create_table "courses", force: :cascade do |t|
...@@ -51,6 +52,12 @@ ActiveRecord::Schema.define(version: 20180619011649) do ...@@ -51,6 +52,12 @@ ActiveRecord::Schema.define(version: 20180619011649) do
t.index ["semester_id"], name: "index_courses_on_semester_id" t.index ["semester_id"], name: "index_courses_on_semester_id"
end end
create_table "instructors", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "semesters", force: :cascade do |t| create_table "semesters", force: :cascade do |t|
t.string "season" t.string "season"
t.string "year" t.string "year"
......
...@@ -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
...@@ -71,20 +71,22 @@ total.each do |subject, sections| ...@@ -71,20 +71,22 @@ total.each do |subject, sections|
course_number: section[:course_number], course_number: section[:course_number],
semester: semester }, all_courses) semester: semester }, all_courses)
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]}"
all_sections.push(name: section_name, all_sections.push(name: section_name,
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
CourseSection.create!(all_sections) CourseSection.create!(all_sections)
......
...@@ -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
luke:
name: Sean Luke
otten:
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'
class InstructorTest < ActiveSupport::TestCase
test "Instructor#named filters correctly" do
assert_equal instructors(:luke).id, Instructor.named("luke").first.id
end
end
...@@ -17,7 +17,7 @@ export const searchCourseSections = (crn: string) => async (dispatch: any) => { ...@@ -17,7 +17,7 @@ export const searchCourseSections = (crn: string) => async (dispatch: any) => {
name: object.name, name: object.name,
title: object.title, title: object.title,
crn: object.crn, crn: object.crn,
instructor: object.instructor, instructor: object.instructor_name,
location: object.location, location: object.location,
days: object.days, days: object.days,
startTime: object.start_time, startTime: object.start_time,
......
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