Commit fc66554a authored by Zach Perkins's avatar Zach Perkins

Fixed the booboos

parent fe2dd688
Pipeline #2954 passed with stage
in 2 minutes and 12 seconds
......@@ -15,7 +15,7 @@ class CoursesController < ApplicationController
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.fetch(params).all
@sections = CourseSection.where(course_id: params[:id]).all
render json: @sections
end
......
......@@ -16,21 +16,21 @@ class Course < ApplicationRecord
def course_sections
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 = base_query.where("courses.course_number = ?", course_number)
base_query.where("courses.course_number = ?", course_number)
end
def self.from_title(base_query, title)
# Temporary really disgusting regex that I hate with all my heart
title = (title + " ").gsub(" 1", " I").gsub(" 2", " II").gsub(" 3", " III").upcase.gsub(/(I+) +/, '\1$').gsub(/ +/, "% ").gsub('$', ' ')
title = (title + " ").gsub(" 1", " I").gsub(" 2", " II").gsub(" 3", " III").upcase.gsub(/(I+) +/, '\1$').gsub(/ +/, "% ").tr('$', ' ')
base_query.where("UPPER(courses.title) LIKE UPPER(?) or UPPER(courses.title) LIKE UPPER(?)", "%#{title.strip}", "%#{title}%")
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)
......@@ -50,19 +50,19 @@ class Course < ApplicationRecord
query = from_title(query, value)
end
end
query
end
# Splits a generic string (i.e. "CS 211") into a series of components that can
# be used to run a query with fetch()
def self.parse_generic_query(query)
# In the future when there is more info, this will be more complex to
# include class names/descriptions
filters = {}
q = query.gsub(" ", "")
q = query.delete(" ")
/[a-zA-Z]+/.match(q) { |a| filters["subject"] = a.to_s }
/\d+/.match(q) { |a| filters["course_number"] = a.to_s }
return filters
filters
end
end
......@@ -16,22 +16,22 @@ class CourseSection < ApplicationRecord
def self.with_instructor(name: "")
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)
base_query.where("course_sections.crn = ?", crn)
end
def self.from_course_id(base_query, course_id)
base_query.where("course_sections.course_id = ?")
base_query.where("course_sections.course_id = ?", course_id)
end
# Select all revelevant course sections given the provided filters
def self.fetch(filters)
query = CourseSection.joins(:course).select("course_sections.*")
if filters.include? "query"
filters = CourseSection.parse_generic_query(filters["query"])
end
filters.each do |filter, value|
case filter
when "crn"
......@@ -46,15 +46,15 @@ class CourseSection < ApplicationRecord
query = Course.from_title(query, value)
end
end
query
end
def self.parse_generic_query(query)
def self.parse_generic_query(query)
filters = {}
# If there is a number in the query
/\d+/.match(query) { |a|
/\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
......@@ -62,14 +62,13 @@ class CourseSection < ApplicationRecord
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 title
filters["title"] = 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