course_sections_controller.rb 1.71 KB
Newer Older
Zac Wood's avatar
Zac Wood committed
1
# Contains all actions having to do with CourseSections.
Zac Wood's avatar
Zac Wood committed
2
# This is a nested controller -- see +config/routes.rb+ for details
Zac Wood's avatar
Zac Wood committed
3
class API::CourseSectionsController < ApplicationController
Zac Wood's avatar
Zac Wood committed
4
  resource_description do
Zac Wood's avatar
Zac Wood committed
5
    short 'Working with course sections, e.g. CS 112 001'
Zac Wood's avatar
Zac Wood committed
6
  end
Zac Wood's avatar
Zac Wood committed
7

8
  api :GET, '/course_sections', 'Get a list of course sections'
Zac Wood's avatar
Zac Wood committed
9
  param :course_id, Integer, desc: "Only get the course sections belonging to the course with this ID"
Zac Wood's avatar
Zac Wood committed
10
  param :crn, String, desc: "Get the course section with this CRN"
11
  param :instructor, String, desc: "Get course sections being taught by this instructor"
Zac Wood's avatar
Zac Wood committed
12
  def index
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
    @sections = CourseSection
                .select('course_sections.*, courses.semester_id, instructors.name AS instructor_name')
                .joins(:course).where('courses.semester_id = ?', @semester.id)
                .joins(:instructor)

    if params.key?(:course_id)
      @sections = @sections.where(course_id: params[:course_id])
    end

    if params.key?(:crn)
      @sections = @sections.where(crn: params[:crn])
    end

    if params.key?(:instructor)
      @sections = @sections.where('UPPER(instructors.name) LIKE UPPER(?)', "%#{params[:instructor]}%")
    end

    # @sections = CourseSection.fetch(params).all
    res = @sections.map do |s|
      {
        id: s.id,
        semester_id: s.semester_id,
        course_id: s.course_id,
        name: s.name,
        crn: s.crn,
        title: s.title,
        instructor_name: s.instructor_name,
        section_type: s.section_type,
        start_date: s.start_date,
        end_date: s.end_date,
        days: s.days,
        start_time: s.start_time,
        end_time: s.end_time,
        location: s.location,
      }
    end
    render json: res
Zac Wood's avatar
Zac Wood committed
50 51
  end
end