Commit 261b01a8 authored by Zach Perkins's avatar Zach Perkins
Browse files

Cleaned up the course_listing endpoint

parent 92536944
class CourseListingController < ApplicationController
api :GET, '/course_listing', "Get all available courses and their sections"
param :subject, String, desc:'Course subject, e.g. "CS" or "ACCT"'
param :number, Integer, desc: 'Course number, e.g. "112"'
def index
db_params = {}
params.each do |name, value|
db_params[:subject] = value if name == "subject"
db_params[:course_number] = value if name == "number"
end
db_courses = Course.where(db_params).all
@courses = []
db_courses.each do |course|
c = course.attributes.dup
c[:sections] = CourseSection.where(course_id: course.id).all
@courses.push(c)
end
render json: @courses
end
resource_description do
short 'Working with courses and associated sections'
end
api :GET, '/course_listing', "Get all available courses and their sections"
param :subject, String, desc:'Course subject, e.g. "CS" or "ACCT"'
param :number, Integer, desc: 'Course number, e.g. "112"'
def index
db_params = {}
# Grab all of the params we pass to the query from the GET arguments
params.each do |name, value|
db_params[name.to_sym] = value if Course.column_names.include? name
end
# Make a seperate list so that we can include sections
@courses = []
Course.where(db_params).all.each do |course_obj|
course = course_obj.attributes.dup
course[:sections] = course_obj.course_sections
@courses.push(course)
end
render json: @courses
end
end
# Registers all routes for the app.
Rails.application.routes.draw do
scope :api do # Register /api routes
resources :courses, only: [:index, :show]
resources :courses, only: [:index, :show, :listing]
resources :course_sections, only: [:index]
resources :course_listing, only: [:index]
resources :schedules, only: [:index]
......
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