courses_controller.rb 1.18 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
    @courses = Course.all
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

    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,
        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
34
  end
35

Zac Wood's avatar
Zac Wood committed
36
37
  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
38
  def show
Zach Perkins's avatar
Zach Perkins committed
39
    @sections = CourseSection.where(course_id: params[:id]).all
40
41
42

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