Commit f54c072f authored by Zach Perkins's avatar Zach Perkins

Fixed some bugs and got rid of old generic search functionality

parent 02e77ac7
Pipeline #3047 failed with stage
......@@ -8,7 +8,7 @@ class CourseListingsController < ApplicationController
param :number, Integer, desc: 'Course number, e.g. "112"'
def index
# Make a separate list so that we can include sections
@courses = CourseListing.wrap(Course.fetch(params).all)
@courses = CourseListingsHelper::CourseListing.wrap(Course.fetch(params).all)
render json: @courses
end
......
class SearchController < ApplicationController
def index
@courses = Course.fetch(params).select do |course|
course.course_sections.count.positive?
end
puts SearchHelper::GenericItem.fetchall("cs 110")
@courses = []
# @courses = Course.fetch(params).select do |course|
# course.course_sections.count.positive?
# end
end
def update
......
......@@ -2,7 +2,11 @@ module CourseListingsHelper
class CourseListing
def initialize(course)
@course = course
@sections = course
@sections = course.course_sections
end
def self.name
:course
end
def self.wrap(course_list)
......
......@@ -7,8 +7,8 @@ module SearchHelper
def self.fetchall(query_string)
models = []
models.push(fetch_instructors query_string)
models.push(fetch_courses query_string)
models += fetch_instructors query_string
models += fetch_courses query_string
build_list(models)
end
......@@ -19,12 +19,15 @@ module SearchHelper
def self.fetch_courses(query_string)
query_string.upcase!
CourseReplacementHelper.replace!(query_string)
base_query = Course.select("*")
base_query = Course.select("courses.*, count(course_sections.id) AS section_count")
.left_outer_joins(:course_sections)
.having("section_count > 0")
.group("courses.id")
subj = nil
query_string.scan(/(?<= |^)([a-zA-Z]{2,4})(?=$| )/).each do |a|
s = a[0]
if Course.from_subject(select("*"), s).count.positive?
if get_count(Course.from_subject(base_query, s)).positive?
subj = s
base_query = Course.from_subject(base_query, subj)
query_string.remove!(s)
......@@ -33,12 +36,15 @@ module SearchHelper
query_string.scan(/(?<= |^)(\d{3})(?=$| )/).each do |a|
s = a[0]
next unless !subj.nil? && from_course_number(from_subject(select("*"), subj), s).count.positive?
base_query = Course.from_course_number(s)
next unless !subj.nil? && get_count(Course.from_course_number(base_query, s)).positive?
base_query = Course.from_course_number(base_query, s)
.order("courses.course_number ASC")
return base_query.all
end
base_query = Course.from_title(query_string.gsub(/ +/, " ").strip)
base_query = Course.from_title(base_query, query_string.gsub(/ +/, " ").strip)
.order("section_count DESC")
base_query.all
end
......@@ -47,10 +53,19 @@ module SearchHelper
def self.build_list(models)
list = []
models.each do |model|
list.push(GenericItem.new(model.name, model))
list.push(GenericItem.new(model.class.name.underscore, model))
end
list
end
def self.get_count(base_query)
# I think I finally hit a limit of active record
ActiveRecord::Base.connection.execute("SELECT COUNT(*) AS count FROM (#{base_query.to_sql})")[0]["count"]
end
def to_s()
@type
end
end
end
......@@ -39,10 +39,10 @@ class Course < ApplicationRecord
# so you can just pass the arguments straight thru
def self.fetch(filters)
# join with course_sections so that we can get a section count for each course and then sort by that
query = Course.left_outer_joins(:course_sections).select("courses.*, COUNT(course_sections.id) AS section_count").group("courses.id").order("section_count DESC")
if filters.include? "query"
filters = Course.parse_generic_query(filters["query"])
end
query = Course.left_outer_joins(:course_sections)
.select("courses.*, COUNT(course_sections.id) AS section_count")
.group("courses.id")
.order("section_count DESC")
filters.each do |filter, value|
case filter
......@@ -59,31 +59,4 @@ class Course < ApplicationRecord
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)
query.upcase!
CourseReplacementHelper.replace!(query)
filters = {}
query.scan(/(?<= |^)([a-zA-Z]{2,4})(?=$| )/).each do |a|
s = a[0]
if from_subject(select("*"), s).count.positive?
filters["subject"] = s
query.remove!(s)
end
end
query.scan(/(?<= |^)(\d{3})(?=$| )/).each do |a|
s = a[0]
next unless filters.include?("subject") && from_course_number(from_subject(select("*"), filters["subject"]), s).count.positive?
filters["course_number"] = s
query.remove!(s)
return filters
end
filters["title"] = query.gsub(/ +/, " ").strip
filters
end
end
end
\ No newline at end of file
......@@ -28,9 +28,6 @@ class CourseSection < ApplicationRecord
# 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
......@@ -49,26 +46,4 @@ class CourseSection < ApplicationRecord
query
end
def self.parse_generic_query(query)
filters = {}
# If there is a number in the query
/\d+/.match(query) do |a|
m = a.to_s
if m.length == query.length # Does the number take up the entire query
if m.length == 5 && from_crn(select("*"), m).count != 0 # Check if it is a CRN
filters["crn"] = m
else # Just assume course_id
filters["course_id"] = Integer(m)
end
return filters
end
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