Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
SRCT
schedules
Commits
6e36be61
Commit
6e36be61
authored
Sep 10, 2018
by
Zac Wood
Browse files
Added instructor query param to /api/course_sections
parent
628bf9e6
Changes
12
Hide whitespace changes
Inline
Side-by-side
schedules_api/app/controllers/course_sections_controller.rb
View file @
6e36be61
...
...
@@ -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
)
...
...
schedules_api/app/models/course_section.rb
View file @
6e36be61
...
...
@@ -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
schedules_api/app/models/instructor.rb
View file @
6e36be61
class
Instructor
<
ApplicationRecord
has_many
:course_sections
def
self
.
named
(
name
)
where
(
"name LIKE ?"
,
"%
#{
name
}
%"
)
end
end
schedules_api/db/migrate/2018091021
0737
_add_instructor_
key_
to_course_sections.rb
→
schedules_api/db/migrate/2018091021
3148
_add_instructor_to_course_sections.rb
View file @
6e36be61
class
AddInstructor
Key
ToCourseSections
<
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
schedules_api/db/schema.rb
View file @
6e36be61
...
...
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord
::
Schema
.
define
(
version:
2018091021
0737
)
do
ActiveRecord
::
Schema
.
define
(
version:
2018091021
3148
)
do
create_table
"closures"
,
force: :cascade
do
|
t
|
t
.
date
"date"
...
...
schedules_api/db/seeds.rb
View file @
6e36be61
...
...
@@ -25,7 +25,7 @@ parser.parse_subjects(semester).each do |subject|
end
# For testing, only get first subject
# subject = parser.parse_subjects(semester)[
2
0]
# 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
...
...
schedules_api/test/controllers/course_sections_controller_test.rb
View file @
6e36be61
...
...
@@ -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
schedules_api/test/fixtures/course_sections.yml
View file @
6e36be61
...
...
@@ -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
schedules_api/test/fixtures/instructors.yml
View file @
6e36be61
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
on
e
:
name
:
MyString
luk
e
:
name
:
Sean Luke
two
:
name
:
MyString
otten
:
name
:
John Otten
kinga
:
name
:
Kinga
schedules_api/test/models/course_section_test.rb
View file @
6e36be61
...
...
@@ -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
schedules_api/test/models/excel_loader_test.rb
View file @
6e36be61
...
...
@@ -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
schedules_api/test/models/instructor_test.rb
View file @
6e36be61
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment