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

Fixed the booboos

parent fe2dd688
Pipeline #2954 passed with stage
in 2 minutes and 12 seconds
...@@ -15,7 +15,7 @@ class CoursesController < ApplicationController ...@@ -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." 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 param :id, :number, desc: 'Course ID', required: true
def show def show
@sections = CourseSection.fetch(params).all @sections = CourseSection.where(course_id: params[:id]).all
render json: @sections render json: @sections
end end
......
...@@ -16,21 +16,21 @@ class Course < ApplicationRecord ...@@ -16,21 +16,21 @@ class Course < ApplicationRecord
def course_sections def course_sections
CourseSection.where course_id: id CourseSection.where course_id: id
end end
def self.from_subject(base_query, subject) def self.from_subject(base_query, subject)
base_query.where("courses.subject = ?", subject.upcase) base_query.where("courses.subject = ?", subject.upcase)
end end
def self.from_course_number(base_query, course_number) 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 end
def self.from_title(base_query, title) def self.from_title(base_query, title)
# Temporary really disgusting regex that I hate with all my heart # 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}%") base_query.where("UPPER(courses.title) LIKE UPPER(?) or UPPER(courses.title) LIKE UPPER(?)", "%#{title.strip}", "%#{title}%")
end end
# Given a list of filters, collect a list of matching elements. This makes it # Given a list of filters, collect a list of matching elements. This makes it
# so you can just pass the arguments straight thru # so you can just pass the arguments straight thru
def self.fetch(filters) def self.fetch(filters)
...@@ -50,19 +50,19 @@ class Course < ApplicationRecord ...@@ -50,19 +50,19 @@ class Course < ApplicationRecord
query = from_title(query, value) query = from_title(query, value)
end end
end end
query query
end end
# Splits a generic string (i.e. "CS 211") into a series of components that can # Splits a generic string (i.e. "CS 211") into a series of components that can
# be used to run a query with fetch() # be used to run a query with fetch()
def self.parse_generic_query(query) def self.parse_generic_query(query)
# In the future when there is more info, this will be more complex to # In the future when there is more info, this will be more complex to
# include class names/descriptions # include class names/descriptions
filters = {} filters = {}
q = query.gsub(" ", "") q = query.delete(" ")
/[a-zA-Z]+/.match(q) { |a| filters["subject"] = a.to_s } /[a-zA-Z]+/.match(q) { |a| filters["subject"] = a.to_s }
/\d+/.match(q) { |a| filters["course_number"] = a.to_s } /\d+/.match(q) { |a| filters["course_number"] = a.to_s }
return filters filters
end end
end end
...@@ -16,22 +16,22 @@ class CourseSection < ApplicationRecord ...@@ -16,22 +16,22 @@ class CourseSection < ApplicationRecord
def self.with_instructor(name: "") def self.with_instructor(name: "")
joins(:instructor).where("instructors.name LIKE ?", "%#{name}%").select('course_sections.*, instructors.name as instructor_name') joins(:instructor).where("instructors.name LIKE ?", "%#{name}%").select('course_sections.*, instructors.name as instructor_name')
end end
def self.from_crn(base_query, crn) def self.from_crn(base_query, crn)
base_query.where("course_sections.crn = ?", value) base_query.where("course_sections.crn = ?", crn)
end end
def self.from_course_id(base_query, course_id) 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 end
# Select all revelevant course sections given the provided filters # Select all revelevant course sections given the provided filters
def self.fetch(filters) def self.fetch(filters)
query = CourseSection.joins(:course).select("course_sections.*") query = CourseSection.joins(:course).select("course_sections.*")
if filters.include? "query" if filters.include? "query"
filters = CourseSection.parse_generic_query(filters["query"]) filters = CourseSection.parse_generic_query(filters["query"])
end end
filters.each do |filter, value| filters.each do |filter, value|
case filter case filter
when "crn" when "crn"
...@@ -46,15 +46,15 @@ class CourseSection < ApplicationRecord ...@@ -46,15 +46,15 @@ class CourseSection < ApplicationRecord
query = Course.from_title(query, value) query = Course.from_title(query, value)
end end
end end
query query
end end
def self.parse_generic_query(query) def self.parse_generic_query(query)
filters = {} filters = {}
# If there is a number in the query # If there is a number in the query
/\d+/.match(query) { |a| /\d+/.match(query) { |a|
m = a.to_s m = a.to_s
if m.length == query.length # Does the number take up the entire query if m.length == query.length # Does the number take up the entire query
if m.length == 5 # Check if it is a CRN if m.length == 5 # Check if it is a CRN
...@@ -62,14 +62,13 @@ class CourseSection < ApplicationRecord ...@@ -62,14 +62,13 @@ class CourseSection < ApplicationRecord
else # Just assume course_id else # Just assume course_id
filters["course_id"] = Integer(m) filters["course_id"] = Integer(m)
end end
return filters return filters
end end
} }
# If it's not a number, just assume it's the title # If it's not a number, just assume it's the title
filters["title"] = query filters["title"] = query
filters filters
end end
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