Commit fdfb621b authored by Zac Wood's avatar Zac Wood

namespacing API controllers

parent 83ef49d7
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
......
# 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
......
# 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
......
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
module CourseListingsHelper
module API::CourseListingsHelper
class CourseListing
def initialize(course)
@course = course
......
module API::InstructorsHelper
end
......@@ -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
......
......@@ -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)
......
......@@ -14,3 +14,7 @@
# ActiveSupport::Inflector.inflections(:en) do |inflect|
# inflect.acronym 'RESTful'
# end
ActiveSupport::Inflector.inflections do |inflect|
inflect.acronym 'API'
end
......@@ -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
......
......@@ -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|
......
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
......
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
......
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
......
require 'test_helper'
class SchedulesControllertest < ActionDispatch::IntegrationTest
class API::SchedulesControllerTest < ActionDispatch::IntegrationTest
test "should generate schedule" do
crns = [course_sections(:cs112001).crn, course_sections(:cs112002).crn]
get "/api/schedules?crns=#{crns.join(',')}"
......
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