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
fdfb621b
Commit
fdfb621b
authored
Oct 27, 2018
by
Zac Wood
Browse files
namespacing API controllers
parent
83ef49d7
Changes
20
Hide whitespace changes
Inline
Side-by-side
schedules/app/controllers/course_listings_controller.rb
→
schedules/app/controllers/
api/
course_listings_controller.rb
View file @
fdfb621b
class
CourseListingsController
<
ApplicationController
class
API::
CourseListingsController
<
ApplicationController
resource_description
do
short
'Working with courses and associated sections'
end
...
...
@@ -8,7 +8,7 @@ class CourseListingsController < ApplicationController
param
:number
,
Integer
,
desc:
'Course number, e.g. "112"'
def
index
# Make a separate list so that we can include sections
@courses
=
CourseListingsHelper
::
CourseListing
.
wrap
(
Course
.
fetch
(
params
).
all
)
@courses
=
API
::
CourseListingsHelper
::
CourseListing
.
wrap
(
Course
.
fetch
(
params
).
all
)
render
json:
@courses
end
...
...
schedules/app/controllers/course_sections_controller.rb
→
schedules/app/controllers/
api/
course_sections_controller.rb
View file @
fdfb621b
# Contains all actions having to do with CourseSections.
# This is a nested controller -- see +config/routes.rb+ for details
class
CourseSectionsController
<
ApplicationController
class
API::
CourseSectionsController
<
ApplicationController
resource_description
do
short
'Working with course sections, e.g. CS 112 001'
end
...
...
schedules/app/controllers/courses_controller.rb
→
schedules/app/controllers/
api/
courses_controller.rb
View file @
fdfb621b
# Contains all actions having to do with Courses.
class
CoursesController
<
ApplicationController
class
API::
CoursesController
<
ApplicationController
resource_description
do
short
'Working with courses, e.g. CS 112'
end
...
...
@@ -7,7 +7,7 @@ class CoursesController < ApplicationController
api
:GET
,
'/courses'
,
"Get a list of courses."
param
:subject
,
String
,
desc:
'Course subject, e.g. "CS" or "ACCT"'
param
:course_number
,
Integer
,
desc:
'Course number, e.g. "112"'
def
index
def
index
@courses
=
Course
.
fetch
(
params
).
all
render
json:
@courses
end
...
...
schedules/app/controllers/api/schedules_controller.rb
0 → 100644
View file @
fdfb621b
class
API::SchedulesController
<
ApplicationController
resource_description
do
short
'Endpoints for generating iCal files'
end
# Render an iCal file containing the schedules of all the
# course sections with the given CRNs.
api
:GET
,
'/schedules'
,
'Generate an iCal file with events for the given CRNs'
param
:crns
,
String
,
desc:
'Comma separated list of CRNs to include as events in the calendar'
,
required:
true
def
index
crns
=
params
[
"crns"
].
split
','
@schedule
=
Schedule
.
new
crns
render
plain:
@schedule
.
to_ical
# render a plaintext iCal file
end
end
schedules/app/helpers/course_listings_helper.rb
→
schedules/app/helpers/
api/
course_listings_helper.rb
View file @
fdfb621b
module
CourseListingsHelper
module
API::
CourseListingsHelper
class
CourseListing
def
initialize
(
course
)
@course
=
course
...
...
schedules/app/helpers/api/courses_helper.rb
0 → 100644
View file @
fdfb621b
module
API::CoursesHelper
end
schedules/app/helpers/api/instructors_helper.rb
0 → 100644
View file @
fdfb621b
module
API::InstructorsHelper
end
schedules/app/helpers/api/sections_helper.rb
0 → 100644
View file @
fdfb621b
module
API::SectionsHelper
end
schedules/app/helpers/courses_helper.rb
deleted
100644 → 0
View file @
83ef49d7
module
CoursesHelper
end
schedules/app/helpers/instructors_helper.rb
deleted
100644 → 0
View file @
83ef49d7
module
InstructorsHelper
end
schedules/app/helpers/sections_helper.rb
deleted
100644 → 0
View file @
83ef49d7
module
SectionsHelper
end
schedules/app/models/course.rb
View file @
fdfb621b
...
...
@@ -12,14 +12,6 @@ class Course < ApplicationRecord
validates
:subject
,
presence:
true
validates
:semester_id
,
presence:
true
def
has_labs?
course_sections
.
reject
(
&
:is_lecture?
).
count
.
positive?
end
def
lab_course?
course_sections
.
select
(
&
:is_lecture?
).
count
.
zero?
end
def
self
.
from_subject
(
base_query
,
subject
)
base_query
.
where
(
"courses.subject = ?"
,
subject
.
upcase
)
end
...
...
schedules/app/models/course_section.rb
View file @
fdfb621b
...
...
@@ -10,27 +10,6 @@ class CourseSection < ApplicationRecord
validates
:title
,
presence:
true
validates
:course_id
,
presence:
true
def
is_lecture?
section_type
==
"Lecture"
end
def
labs
return
nil
unless
is_lecture?
# Lectures have names formatted like "MATH 214 001"
# Labs/recitations have the title format "Recitation for Lecture 001"
# so, match all the sections in the same course which have the same number
# as the last element of their titles
lecture_number
=
name
.
split
[
name
.
split
.
length
-
1
]
labs_for_section
=
course
.
course_sections
.
select
do
|
s
|
s
.
title
.
split
[
s
.
title
.
split
.
length
-
1
]
==
lecture_number
end
labs_for_section
.
map
do
|
lab
|
[
self
,
lab
]
end
end
def
overlaps?
(
other
)
t1_start
,
t1_end
=
Time
.
parse
(
start_time
),
Time
.
parse
(
end_time
)
t2_start
,
t2_end
=
Time
.
parse
(
other
.
start_time
),
Time
.
parse
(
other
.
end_time
)
...
...
schedules/config/initializers/inflections.rb
View file @
fdfb621b
...
...
@@ -14,3 +14,7 @@
# ActiveSupport::Inflector.inflections(:en) do |inflect|
# inflect.acronym 'RESTful'
# end
ActiveSupport
::
Inflector
.
inflections
do
|
inflect
|
inflect
.
acronym
'API'
end
schedules/config/routes.rb
View file @
fdfb621b
...
...
@@ -8,10 +8,10 @@ Rails.application.routes.draw do
resources
:instructors
,
only:
[
:index
,
:show
]
get
'schedule'
,
to:
'schedules#show'
,
as:
'schedule'
scope
:api
do
# Register /api routes
resources
:courses
,
only:
[
:index
,
:show
]
resources
:course_sections
,
only:
[
:index
]
resources
:instructors
,
only:
[
:index
,
:show
]
scope
:api
,
module:
'api'
do
# Register /api routes
resources
:courses
,
only:
[
:index
,
:show
]
,
as:
'api_courses'
resources
:course_sections
,
only:
[
:index
]
,
as:
'api_course_sections'
resources
:instructors
,
only:
[
:index
,
:show
]
,
as:
'api_instructors'
resources
:course_listings
,
only:
[
:index
]
resources
:schedules
,
only:
[
:index
]
end
...
...
schedules/db/seeds.rb
View file @
fdfb621b
...
...
@@ -110,7 +110,7 @@ def main
wipe_db
parser
=
PatriotWeb
::
Parser
.
new
semesters
=
parser
.
parse_semesters
[
0
..
1
]
# expand to include however many semesters you want
semesters
=
parser
.
parse_semesters
[
0
..
6
]
# expand to include however many semesters you want
courses
=
nil
semesters
.
each
do
|
semester
|
...
...
schedules/test/controllers/course_listings_controller_test.rb
→
schedules/test/controllers/
api/
course_listings_controller_test.rb
View file @
fdfb621b
require
'test_helper'
class
CourseListingsControllerTest
<
ActionDispatch
::
IntegrationTest
class
API::
CourseListingsControllerTest
<
ActionDispatch
::
IntegrationTest
test
'should grab sections for course'
do
get
course_listings_url
course_id:
courses
(
:cs112
).
id
assert_response
:success
...
...
schedules/test/controllers/course_sections_controller_test.rb
→
schedules/test/controllers/
api/
course_sections_controller_test.rb
View file @
fdfb621b
require
'test_helper'
class
CourseSectionsControllerTest
<
ActionDispatch
::
IntegrationTest
class
API::
CourseSectionsControllerTest
<
ActionDispatch
::
IntegrationTest
test
'should get index'
do
get
course_sections_url
course_id:
courses
(
:cs112
).
id
get
api_
course_sections_url
course_id:
courses
(
:cs112
).
id
assert_response
:success
sections_returned
=
JSON
.
parse
@response
.
body
...
...
@@ -12,7 +12,7 @@ class CourseSectionsControllerTest < ActionDispatch::IntegrationTest
end
test
'should filter by crn'
do
get
course_sections_url
crn:
course_sections
(
:cs112001
).
crn
get
api_
course_sections_url
crn:
course_sections
(
:cs112001
).
crn
assert_response
:success
sections_returned
=
JSON
.
parse
@response
.
body
...
...
@@ -20,7 +20,7 @@ class CourseSectionsControllerTest < ActionDispatch::IntegrationTest
end
test
'should filter by professor'
do
get
course_sections_url
instructor:
"king"
get
api_
course_sections_url
instructor:
"king"
assert_response
:success
sections_returned
=
JSON
.
parse
@response
.
body
...
...
schedules/test/controllers/courses_controller_test.rb
→
schedules/test/controllers/
api/
courses_controller_test.rb
View file @
fdfb621b
require
'test_helper'
class
CoursesControllerTest
<
ActionDispatch
::
IntegrationTest
class
API::CoursesControllerTest
<
ActionDispatch
::
IntegrationTest
semester_id
=
semesters
(
:fall2018
).
id
test
'#index should return all courses'
do
get
courses_url
get
api_
courses_url
assert_response
:success
courses_returned
=
JSON
.
parse
@response
.
body
...
...
@@ -11,7 +13,7 @@ class CoursesControllerTest < ActionDispatch::IntegrationTest
end
test
'#index should return filtered by subject case insensitive'
do
get
courses_url
subject:
"Cs"
get
api_
courses_url
subject:
"Cs"
assert_response
:success
courses_returned
=
JSON
.
parse
@response
.
body
...
...
@@ -21,7 +23,7 @@ class CoursesControllerTest < ActionDispatch::IntegrationTest
end
test
'#index should return filtered by subject and course number'
do
get
courses_url
subject:
"CS"
,
course_number:
"112"
get
api_
courses_url
subject:
"CS"
,
course_number:
"112"
assert_response
:success
courses_returned
=
JSON
.
parse
@response
.
body
...
...
@@ -33,7 +35,7 @@ class CoursesControllerTest < ActionDispatch::IntegrationTest
test
'#show should return course_sections for course'
do
cs_112_id
=
courses
(
:cs112
).
id
get
course_url
id:
cs_112_id
get
api_
course_url
id:
cs_112_id
assert_response
:success
sections_returned
=
JSON
.
parse
@response
.
body
...
...
schedules/test/controllers/schedules_controller_test.rb
→
schedules/test/controllers/
api/
schedules_controller_test.rb
View file @
fdfb621b
require
'test_helper'
class
SchedulesController
t
est
<
ActionDispatch
::
IntegrationTest
class
API::
SchedulesController
T
est
<
ActionDispatch
::
IntegrationTest
test
"should generate schedule"
do
crns
=
[
course_sections
(
:cs112001
).
crn
,
course_sections
(
:cs112002
).
crn
]
get
"/api/schedules?crns=
#{
crns
.
join
(
','
)
}
"
...
...
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