diff --git a/src/actions/universities/index.cr b/src/actions/universities/index.cr index 604ae24..f9d7be7 100644 --- a/src/actions/universities/index.cr +++ b/src/actions/universities/index.cr @@ -33,6 +33,105 @@ class Universities::Index < BrowserAction query = query.is_good(true) if is_good + range_max, range_min, query = filter_by_column_action(query) + + query = order_by_action(query) + + query = query.batch_level(batch_level) if batch_level.presence + + pages, universities = paginate(query.id.desc_order, per_page: 50) + all_name_inputs = [ + "q", "is_985", "is_211", "is_good", + "order_by", "batch_level", "filter_by_column", + "range_min_value", "range_max_value", + ] + + if request.headers["HX-Trigger"]? + component( + Main, + universities: universities, + pages: pages, + range_max: range_max.to_i, + range_min: range_min.to_i, + all_name_inputs: all_name_inputs, + ) + else + html( + IndexPage, + universities: universities, + pages: pages, + range_max: range_max.to_i, + range_min: range_min.to_i, + all_name_inputs: all_name_inputs + ) + end + end + + memoize def fetch_range_max_min(column : String, query : UniversityQuery) : Tuple(Float64 | Int32, Float64 | Int32) + case column + when "ranking_2023" + max = query.ranking_2023_min.select_max + min = query.ranking_2023_min.select_min + when "ranking_2022" + max = query.ranking_2022_min.select_max + min = query.ranking_2022_min.select_min + when "ranking_2021" + max = query.ranking_2021_min.select_max + min = query.ranking_2021_min.select_min + when "ranking_2020" + max = query.ranking_2020_min.select_max + min = query.ranking_2021_min.select_min + when "score_2023" + max = query.score_2023_min.select_max + min = query.score_2023_min.select_min + when "score_2022" + max = query.score_2022_min.select_max + min = query.score_2022_min.select_min + when "score_2021" + max = query.score_2021_min.select_max + min = query.score_2021_min.select_min + when "score_2020" + max = query.score_2020_min.select_max + min = query.score_2020_min.select_min + end + + {max || 0, min || 0} + end + + def filter_by_column_action(query) + range_max, range_min = fetch_range_max_min(filter_by_column, query) + + min_value = range_min_value.zero? ? range_min.to_i : range_min_value + max_value = range_max_value.zero? ? range_max.to_i : range_max_value + + if filter_by_column.presence + params.from_query["range_min_value"] = min_value.to_s + params.from_query["range_max_value"] = max_value.to_s + + case filter_by_column + when "ranking_2023" + query = query.ranking_2023_min.gte(min_value).ranking_2023_min.lte(max_value) if !min_value.nil? && !max_value.nil? + when "ranking_2022" + query = query.ranking_2022_min.gte(min_value).ranking_2022_min.lte(max_value) if !min_value.nil? && !max_value.nil? + when "ranking_2021" + query = query.ranking_2021_min.gte(min_value).ranking_2021_min.lte(max_value) if !min_value.nil? && !max_value.nil? + when "ranking_2020" + query = query.ranking_2020_min.gte(min_value).ranking_2020_min.lte(max_value) if !min_value.nil? && !max_value.nil? + when "score_2023" + query = query.score_2023_min.gte(min_value).score_2023_min.lte(max_value) if !min_value.nil? && !max_value.nil? + when "score_2022" + query = query.score_2022_min.gte(min_value).score_2022_min.lte(max_value) if !min_value.nil? && !max_value.nil? + when "score_2021" + query = query.score_2021_min.gte(min_value).score_2021_min.lte(max_value) if !min_value.nil? && !max_value.nil? + when "score_2020" + query = query.score_2020_min.gte(min_value).score_2020_min.lte(max_value) if !min_value.nil? && !max_value.nil? + end + end + + {range_max, range_min, query} + end + + def order_by_action(query) # 检测在哪一个 tab head 上点击, 只要每次点击一次, 就判断有没有 cookie # 如果没有, 就默认排序, 如果有, 就反转 if click_on.presence @@ -145,93 +244,6 @@ class Universities::Index < BrowserAction cookies.delete("order_by") end - query = query.batch_level(batch_level) if batch_level.presence - - range_max, range_min = fetch_range_max_min(filter_by_column, query) - - min_value = range_min_value.zero? ? range_min.to_i : range_min_value - max_value = range_max_value.zero? ? range_max.to_i : range_max_value - - if filter_by_column.presence - params.from_query["range_min_value"] = min_value.to_s - params.from_query["range_max_value"] = max_value.to_s - - case filter_by_column - when "ranking_2023" - query = query.ranking_2023_min.gte(min_value).ranking_2023_min.lte(max_value) if !min_value.nil? && !max_value.nil? - when "ranking_2022" - query = query.ranking_2022_min.gte(min_value).ranking_2022_min.lte(max_value) if !min_value.nil? && !max_value.nil? - when "ranking_2021" - query = query.ranking_2021_min.gte(min_value).ranking_2021_min.lte(max_value) if !min_value.nil? && !max_value.nil? - when "ranking_2020" - query = query.ranking_2020_min.gte(min_value).ranking_2020_min.lte(max_value) if !min_value.nil? && !max_value.nil? - when "score_2023" - query = query.score_2023_min.gte(min_value).score_2023_min.lte(max_value) if !min_value.nil? && !max_value.nil? - when "score_2022" - query = query.score_2022_min.gte(min_value).score_2022_min.lte(max_value) if !min_value.nil? && !max_value.nil? - when "score_2021" - query = query.score_2021_min.gte(min_value).score_2021_min.lte(max_value) if !min_value.nil? && !max_value.nil? - when "score_2020" - query = query.score_2020_min.gte(min_value).score_2020_min.lte(max_value) if !min_value.nil? && !max_value.nil? - end - end - - pages, universities = paginate(query.id.desc_order, per_page: 50) - all_name_inputs = [ - "q", "is_985", "is_211", "is_good", - "order_by", "batch_level", "filter_by_column", - "range_min_value", "range_max_value", - ] - - if request.headers["HX-Trigger"]? - component( - Main, - universities: universities, - pages: pages, - range_max: range_max.to_i, - range_min: range_min.to_i, - all_name_inputs: all_name_inputs, - ) - else - html( - IndexPage, - universities: universities, - pages: pages, - range_max: range_max.to_i, - range_min: range_min.to_i, - all_name_inputs: all_name_inputs - ) - end - end - - memoize def fetch_range_max_min(column : String, query : UniversityQuery) : Tuple(Float64 | Int32, Float64 | Int32) - case column - when "ranking_2023" - max = query.ranking_2023_min.select_max - min = query.ranking_2023_min.select_min - when "ranking_2022" - max = query.ranking_2022_min.select_max - min = query.ranking_2022_min.select_min - when "ranking_2021" - max = query.ranking_2021_min.select_max - min = query.ranking_2021_min.select_min - when "ranking_2020" - max = query.ranking_2020_min.select_max - min = query.ranking_2021_min.select_min - when "score_2023" - max = query.score_2023_min.select_max - min = query.score_2023_min.select_min - when "score_2022" - max = query.score_2022_min.select_max - min = query.score_2022_min.select_min - when "score_2021" - max = query.score_2021_min.select_max - min = query.score_2021_min.select_min - when "score_2020" - max = query.score_2020_min.select_max - min = query.score_2020_min.select_min - end - - {max || 0, min || 0} + query end end