course_sections_controller.rb 1.77 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
    @sections = CourseSection
Zac Wood's avatar
Zac Wood committed
14 15 16 17
                .where(semester: @semester)
                .joins(:course)
                .joins(:instructor)
                .select('course_sections.*, instructors.name AS instructor_name')
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

    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

    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,
Zac Wood's avatar
Zac Wood committed
40 41 42
        instructor_url: instructor_url(s.instructor),
        teaching_rating: s.instructor.rating,
        course_rating: s.course_rating,
43 44 45 46 47 48 49 50 51 52
        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
53 54
  end
end