Skip to content

Commit

Permalink
Refactor action
Browse files Browse the repository at this point in the history
  • Loading branch information
zw963 committed May 22, 2024
1 parent a915db1 commit 11b2e1e
Showing 1 changed file with 100 additions and 88 deletions.
188 changes: 100 additions & 88 deletions src/actions/universities/index.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 11b2e1e

Please sign in to comment.