Commit f5c37ea2 authored by Zach Perkins's avatar Zach Perkins
Browse files

Implemented more complex filtering methods

parent e68cc60f
......@@ -12,11 +12,7 @@ class CourseSectionsController < ApplicationController
param :query, String, desc: 'A generic query ex. "CS 110"'
def index
@sections = CourseSection.with_instructor(name: params[:instructor])
@sections = @sections.where(course_id: params[:course_id]) if params.key?(:course_id)
@sections = @sections.where(crn: params[:crn]) if params.key?(:crn)
@sections = CourseSection.fetch(params).all
render json: @sections
end
end
......@@ -7,16 +7,15 @@ class CoursesController < ApplicationController
api :GET, '/courses', "Get a list of courses."
param :subject, String, desc: 'Course subject, e.g. "CS" or "ACCT"'
param :course_number, Integer, desc: 'Course number, e.g. "112"'
param :query, String, desc: 'A generic query ex. "CS 110"'
def index
@courses = Course.fetch(params)
@courses = Course.fetch(params).all
render json: @courses
end
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
def show
@sections = CourseSection.where(course_id: params[:id])
@sections = CourseSection.fetch(params).all
render json: @sections
end
......
......@@ -17,24 +17,34 @@ class Course < ApplicationRecord
CourseSection.where course_id: id
end
def self.from_subject(base_query, subject)
base_query.where("courses.subject = ?", subject.upcase)
end
def self.from_course_number(base_query, course_number)
query = query.where("courses.course_number = ?", value)
end
# Given a list of filters, collect a list of matching elements. This makes it
# so you can just pass the arguments straight thru
def self.fetch(filters)
query = Course.select("*")
filter_list = Course.parse_generic_query(filters["query"]) if filters.include? "query" else filters
filter_list.each do |filter, value|
if filters.include? "query"
filters = Course.parse_generic_query(filters["query"])
end
filters.each do |filter, value|
if Course.column_names.include? filter
case filter
when "subject"
query = query.where("subject = ?", value.upcase)
query = from_subject(query, value)
when "course_number"
query = query.where("course_number = ?", value)
query = from_course_number(query, value)
end
end
end
return query.all
query
end
# Splits a generic string (i.e. "CS 211") into a series of components that can
......
......@@ -17,25 +17,61 @@ class CourseSection < ApplicationRecord
joins(:instructor).where("instructors.name LIKE ?", "%#{name}%").select('course_sections.*, instructors.name as instructor_name')
end
def self.from_crn(base_query, crn)
base_query.where("course_sections.crn = ?", value)
end
def self.from_course_id(base_query, course_id)
base_query.where("course_sections.course_id = ?")
end
def self.from_name(base_query, name)
base_query.where("name LIKE ?", "%#{name}%")
end
# Select all revelevant course sections given the provided filters
def self.fetch(filters)
query = CourseSection.select("*")
filter_list = CourseSection.parse_generic_query(filters["query"]) if filters.include? "query" else filters
query = CourseSection.joins(:course).select("course_sections.*")
if filters.include? "query"
filters = CourseSection.parse_generic_query(filters["query"])
end
filter_list.each do |filter, value|
if CourseSection.column_names.include? filter
case filter
when "crn"
query = query.where("crn = ?", value.upcase)
when "course_id"
query = query.where("course_id = ?", value)
end
filters.each do |filter, value|
case filter
when "crn"
query = from_crn(query, value)
when "course_id"
query = from_course_id(query, value)
when "name"
query = from_name(query, value)
when "subject"
query = Course.from_subject(query, value)
end
end
query
end
def self.parse_generic_query(query)
filters = {}
# If there is a number in the query
/\d+/.match(query) { |a|
m = a.to_s
if m.length == query.length # Does the number take up the entire query
if m.length == 5 # Check if it is a CRN
filters["crn"] = m
else # Just assume course_id
filters["course_id"] = Integer(m)
end
return filters
end
}
# If it's not a number, just assume it's the name
filters["name"] = query
filters
end
end
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