courses_controller.rb 1.25 KB
Newer Older
Zac Wood's avatar
Zac Wood committed
1
# Contains all actions having to do with Courses.
Zac Wood's avatar
Zac Wood committed
2
class API::CoursesController < ApplicationController
Zac Wood's avatar
Zac Wood committed
3
  resource_description do
Zac Wood's avatar
Zac Wood committed
4
    short 'Working with courses, e.g. CS 112'
Zac Wood's avatar
Zac Wood committed
5
  end
Zac Wood's avatar
Zac Wood committed
6

Zac Wood's avatar
Zac Wood committed
7 8
  api :GET, '/courses', "Get a list of courses."
  param :subject, String, desc: 'Course subject, e.g. "CS" or "ACCT"'
Zac Wood's avatar
Zac Wood committed
9
  param :course_number, Integer, desc: 'Course number, e.g. "112"'
Zac Wood's avatar
Zac Wood committed
10
  def index
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
    @courses = Course.where(semester_id: params[:semester_id])

    if params.key?(:subject)
      @courses = @courses.where("UPPER(courses.subject) LIKE ?", "%#{params[:subject]}%")
    end

    if params.key?(:course_number)
      @courses = @courses.where(course_number: params[:course_number])
    end

    result = @courses.map do |c|
      {
        id: c.id,
        semester_id: c.semester_id,
        subject: c.subject,
        course_number: c.course_number,
        description: c.description,
        credits: c.credits,
        title: c.title,
        prereqs: c.prereqs
      }
    end

    render json: result
Zac Wood's avatar
Zac Wood committed
35
  end
36

Zac Wood's avatar
Zac Wood committed
37 38
  api :GET, '/courses/:id', "Get a list of all course sections for the course with the given id."
  param :id, :number, desc: 'Course ID', required: true
39
  def show
Zach Perkins's avatar
Zach Perkins committed
40
    @sections = CourseSection.where(course_id: params[:id]).all
41 42 43

    render json: @sections
  end
Zac Wood's avatar
Zac Wood committed
44
end