From 7cc3a0e0bce421393cd7dee1b8e3f7d83d17c22f Mon Sep 17 00:00:00 2001 From: "Billy.Zheng" Date: Thu, 13 Jun 2024 09:52:39 +0800 Subject: [PATCH] Add is_marked back --- ...dd_index_to_is_marked_for_chong_wen_bao.cr | 9 ++++ .../universities/htmx/clear_2020_mark.cr | 2 +- .../universities/htmx/clear_2021_mark.cr | 2 +- .../universities/htmx/clear_2022_mark.cr | 2 +- .../universities/htmx/clear_2023_mark.cr | 2 +- src/actions/universities/htmx/clear_mark.cr | 11 +++++ src/actions/universities/htmx/marked.cr | 15 ++++++ src/actions/universities/htmx/marked_2020.cr | 7 +-- src/actions/universities/htmx/marked_2021.cr | 7 +-- src/actions/universities/htmx/marked_2022.cr | 7 +-- src/actions/universities/htmx/marked_2023.cr | 7 +-- src/actions/universities/index.cr | 6 ++- src/components/universities/main.cr | 27 +++++++++++ src/models/university.cr | 10 ++++ src/pages/me/show_page.cr | 48 ++++++++++++------- 15 files changed, 126 insertions(+), 36 deletions(-) create mode 100644 db/migrations/20240613010329_add_index_to_is_marked_for_chong_wen_bao.cr create mode 100644 src/actions/universities/htmx/clear_mark.cr create mode 100644 src/actions/universities/htmx/marked.cr diff --git a/db/migrations/20240613010329_add_index_to_is_marked_for_chong_wen_bao.cr b/db/migrations/20240613010329_add_index_to_is_marked_for_chong_wen_bao.cr new file mode 100644 index 0000000..59622b1 --- /dev/null +++ b/db/migrations/20240613010329_add_index_to_is_marked_for_chong_wen_bao.cr @@ -0,0 +1,9 @@ +class AddIndexToIsMarkedForChongWenBao::V20240613010329 < Avram::Migrator::Migration::V1 + def migrate + create_index table_for(ChongWenBao), [:is_marked] + end + + def rollback + drop_index table_for(ChongWenBao), [:is_marked], if_exists: true + end +end diff --git a/src/actions/universities/htmx/clear_2020_mark.cr b/src/actions/universities/htmx/clear_2020_mark.cr index ea7407f..b22b74a 100644 --- a/src/actions/universities/htmx/clear_2020_mark.cr +++ b/src/actions/universities/htmx/clear_2020_mark.cr @@ -1,6 +1,6 @@ class Universities::Htmx::Clear2020Mark < HtmxAction put "/universities/clear_2020_mark" do - chong_wen_baos = ChongWenBaoQuery.new.user_id(current_user.id.not_nil!) + chong_wen_baos = ChongWenBaoQuery.new.user_id(current_user.id) chong_wen_baos.update(is_marked_2020: false) if chong_wen_baos diff --git a/src/actions/universities/htmx/clear_2021_mark.cr b/src/actions/universities/htmx/clear_2021_mark.cr index 075c5e7..b3890f0 100644 --- a/src/actions/universities/htmx/clear_2021_mark.cr +++ b/src/actions/universities/htmx/clear_2021_mark.cr @@ -1,6 +1,6 @@ class Universities::Htmx::Clear2021Mark < HtmxAction put "/universities/clear_2021_mark" do - chong_wen_baos = ChongWenBaoQuery.new.user_id(current_user.id.not_nil!) + chong_wen_baos = ChongWenBaoQuery.new.user_id(current_user.id) chong_wen_baos.update(is_marked_2021: false) if chong_wen_baos diff --git a/src/actions/universities/htmx/clear_2022_mark.cr b/src/actions/universities/htmx/clear_2022_mark.cr index 50674fb..4443d40 100644 --- a/src/actions/universities/htmx/clear_2022_mark.cr +++ b/src/actions/universities/htmx/clear_2022_mark.cr @@ -1,6 +1,6 @@ class Universities::Htmx::Clear2022Mark < HtmxAction put "/universities/clear_2022_mark" do - chong_wen_baos = ChongWenBaoQuery.new.user_id(current_user.id.not_nil!) + chong_wen_baos = ChongWenBaoQuery.new.user_id(current_user.id) chong_wen_baos.update(is_marked_2022: false) if chong_wen_baos diff --git a/src/actions/universities/htmx/clear_2023_mark.cr b/src/actions/universities/htmx/clear_2023_mark.cr index 9a44ee6..90e815c 100644 --- a/src/actions/universities/htmx/clear_2023_mark.cr +++ b/src/actions/universities/htmx/clear_2023_mark.cr @@ -1,6 +1,6 @@ class Universities::Htmx::Clear2023Mark < HtmxAction put "/universities/clear_2023_mark" do - chong_wen_baos = ChongWenBaoQuery.new.user_id(current_user.id.not_nil!) + chong_wen_baos = ChongWenBaoQuery.new.user_id(current_user.id) chong_wen_baos.update(is_marked_2023: false) if chong_wen_baos diff --git a/src/actions/universities/htmx/clear_mark.cr b/src/actions/universities/htmx/clear_mark.cr new file mode 100644 index 0000000..68e4122 --- /dev/null +++ b/src/actions/universities/htmx/clear_mark.cr @@ -0,0 +1,11 @@ +class Universities::Htmx::ClearMark < HtmxAction + put "/universities/clear_mark" do + chong_wen_baos = ChongWenBaoQuery.new.user_id(current_user.id) + + chong_wen_baos.update(is_marked: false) if chong_wen_baos + + context.response.headers["HX-Refresh"] = "true" + + plain_text "ok" + end +end diff --git a/src/actions/universities/htmx/marked.cr b/src/actions/universities/htmx/marked.cr new file mode 100644 index 0000000..3f39daa --- /dev/null +++ b/src/actions/universities/htmx/marked.cr @@ -0,0 +1,15 @@ +class Universities::Htmx::Marked < HtmxAction + put "/universities/:university_id/marked" do + user_id = current_user.id + + chong_wen_bao = ChongWenBaoQuery.new.user_id(user_id).university_id(university_id).first? + + chong_wen_bao = SaveChongWenBao.create!(user_id: user_id, university_id: university_id.to_i64) if chong_wen_bao.nil? + + SaveChongWenBao.update!(chong_wen_bao, is_marked: !chong_wen_bao.is_marked) + + context.response.headers["HX-Refresh"] = "true" + + plain_text "ok" + end +end diff --git a/src/actions/universities/htmx/marked_2020.cr b/src/actions/universities/htmx/marked_2020.cr index 7ac13f0..283e3f3 100644 --- a/src/actions/universities/htmx/marked_2020.cr +++ b/src/actions/universities/htmx/marked_2020.cr @@ -1,11 +1,12 @@ class Universities::Htmx::Marked2020 < HtmxAction put "/universities/:university_id/marked_2020" do - user_id = current_user.id.not_nil! - university = UniversityQuery.new.preload_chong_wen_baos.find(university_id) + user_id = current_user.id chong_wen_bao = ChongWenBaoQuery.new.user_id(user_id).university_id(university_id).first? - SaveChongWenBao.update!(chong_wen_bao, is_marked_2020: !chong_wen_bao.is_marked_2020) if chong_wen_bao + chong_wen_bao = SaveChongWenBao.create!(user_id: user_id, university_id: university_id.to_i64) if chong_wen_bao.nil? + + SaveChongWenBao.update!(chong_wen_bao, is_marked_2020: !chong_wen_bao.is_marked_2020) context.response.headers["HX-Refresh"] = "true" diff --git a/src/actions/universities/htmx/marked_2021.cr b/src/actions/universities/htmx/marked_2021.cr index 873b1b5..dde5f59 100644 --- a/src/actions/universities/htmx/marked_2021.cr +++ b/src/actions/universities/htmx/marked_2021.cr @@ -1,11 +1,12 @@ class Universities::Htmx::Marked2021 < HtmxAction put "/universities/:university_id/marked_2021" do - user_id = current_user.id.not_nil! - university = UniversityQuery.new.preload_chong_wen_baos.find(university_id) + user_id = current_user.id chong_wen_bao = ChongWenBaoQuery.new.user_id(user_id).university_id(university_id).first? - SaveChongWenBao.update!(chong_wen_bao, is_marked_2021: !chong_wen_bao.is_marked_2021) if chong_wen_bao + chong_wen_bao = SaveChongWenBao.create!(user_id: user_id, university_id: university_id.to_i64) if chong_wen_bao.nil? + + SaveChongWenBao.update!(chong_wen_bao, is_marked_2021: !chong_wen_bao.is_marked_2021) context.response.headers["HX-Refresh"] = "true" diff --git a/src/actions/universities/htmx/marked_2022.cr b/src/actions/universities/htmx/marked_2022.cr index d378fbb..768588d 100644 --- a/src/actions/universities/htmx/marked_2022.cr +++ b/src/actions/universities/htmx/marked_2022.cr @@ -1,11 +1,12 @@ class Universities::Htmx::Marked2022 < HtmxAction put "/universities/:university_id/marked_2022" do - user_id = current_user.id.not_nil! - university = UniversityQuery.new.preload_chong_wen_baos.find(university_id) + user_id = current_user.id chong_wen_bao = ChongWenBaoQuery.new.user_id(user_id).university_id(university_id).first? - SaveChongWenBao.update!(chong_wen_bao, is_marked_2022: !chong_wen_bao.is_marked_2022) if chong_wen_bao + chong_wen_bao = SaveChongWenBao.create!(user_id: user_id, university_id: university_id.to_i64) if chong_wen_bao.nil? + + SaveChongWenBao.update!(chong_wen_bao, is_marked_2022: !chong_wen_bao.is_marked_2022) context.response.headers["HX-Refresh"] = "true" diff --git a/src/actions/universities/htmx/marked_2023.cr b/src/actions/universities/htmx/marked_2023.cr index 6239192..216b9c7 100644 --- a/src/actions/universities/htmx/marked_2023.cr +++ b/src/actions/universities/htmx/marked_2023.cr @@ -1,11 +1,12 @@ class Universities::Htmx::Marked2023 < HtmxAction put "/universities/:university_id/marked_2023" do - user_id = current_user.id.not_nil! - university = UniversityQuery.new.preload_chong_wen_baos.find(university_id) + user_id = current_user.id chong_wen_bao = ChongWenBaoQuery.new.user_id(user_id).university_id(university_id).first? - SaveChongWenBao.update!(chong_wen_bao, is_marked_2023: !chong_wen_bao.is_marked_2023) if chong_wen_bao + chong_wen_bao = SaveChongWenBao.create!(user_id: user_id, university_id: university_id.to_i64) if chong_wen_bao.nil? + + SaveChongWenBao.update!(chong_wen_bao, is_marked_2023: !chong_wen_bao.is_marked_2023) context.response.headers["HX-Refresh"] = "true" diff --git a/src/actions/universities/index.cr b/src/actions/universities/index.cr index 87b7d0c..26227a5 100644 --- a/src/actions/universities/index.cr +++ b/src/actions/universities/index.cr @@ -4,6 +4,7 @@ class Universities::Index < BrowserAction param is_211 : Bool = false param is_good : Bool = false param is_exists_remark : Bool = false + param is_marked : Bool = false param is_marked_2023 : Bool = false param is_marked_2022 : Bool = false param is_marked_2021 : Bool = false @@ -58,12 +59,13 @@ class Universities::Index < BrowserAction query = query.where_chong_wen_baos(user_chong_wen_bao_query.university_remark.is_not_nil) if is_exists_remark - if is_marked_2023 || is_marked_2022 || is_marked_2021 || is_marked_2020 || + if is_marked_2023 || is_marked_2022 || is_marked_2021 || is_marked_2020 || is_marked || chong_2023 || chong_2022 || chong_2021 || chong_2020 || wen_2023 || wen_2022 || wen_2021 || wen_2020 || bao_2023 || bao_2022 || bao_2021 || bao_2020 cwb_query = user_chong_wen_bao_query + cwb_query = cwb_query.is_marked(true) if is_marked cwb_query = cwb_query.is_marked_2023(true) if is_marked_2023 cwb_query = cwb_query.is_marked_2022(true) if is_marked_2022 cwb_query = cwb_query.is_marked_2021(true) if is_marked_2021 @@ -94,7 +96,7 @@ class Universities::Index < BrowserAction pages, universities = paginate(query.id.desc_order, per_page: 50) all_name_inputs = [ - "q", "is_985", "is_211", "is_good", "is_exists_remark", + "q", "is_985", "is_211", "is_good", "is_exists_remark", "is_marked", "is_marked_2023", "is_marked_2022", "is_marked_2021", "is_marked_2020", "order_by", "batch_level", "filter_by_column", "range_min_value", "range_max_value", diff --git a/src/components/universities/main.cr b/src/components/universities/main.cr index 4589342..a1c6a6c 100644 --- a/src/components/universities/main.cr +++ b/src/components/universities/main.cr @@ -232,6 +232,9 @@ then set (next ).value to my.value2 div class: "col m2" do mount CheckBox, "is_exists_remark", "仅显示含备注学校", all_name_inputs end + div class: "col m2" do + mount CheckBox, "is_marked", "仅显示手动标记的学校", all_name_inputs + end end private def render_batch_level_dropdown @@ -357,7 +360,9 @@ then set (next ).value to my.value2 marked_2022: university.marked_2022(current_user), marked_2021: university.marked_2021(current_user), marked_2020: university.marked_2020(current_user), + marked: university.marked(current_user), script: "on click set @href of to '#{Edit.with(university).path}' +then set @hx-put of to '#{Universities::Htmx::Marked.with(university.id).path}' then set @hx-put of to '#{Universities::Htmx::Marked2023.with(university.id).path}' then set @hx-put of to '#{Universities::Htmx::Marked2022.with(university.id).path}' then set @hx-put of to '#{Universities::Htmx::Marked2021.with(university.id).path}' @@ -383,6 +388,11 @@ then if @marked-2020 as String == 'true' else js document.getElementById('marked_2020').checked = false; end end +then if @marked as String == 'true' + js document.getElementById('marked').checked = true; end +else + js document.getElementById('marked').checked = false; end +end " ) end @@ -539,6 +549,23 @@ end span "标记2020" end end + + li do + label do + input(type: "hidden", name: "university:is_marked", value: "false", id: "marked_unmark") + input( + type: "checkbox", + name: "university:is_marked", + value: "true", + id: "marked", + "hx-put": "data_marked_url", + "hx-swap": "#main", + "hx-indicator": "#spinner", + "hx-include": "[name='_csrf'],input#marked_unmark" + ) + span "手动标记" + end + end end end end diff --git a/src/models/university.cr b/src/models/university.cr index d1b1daa..5b56962 100644 --- a/src/models/university.cr +++ b/src/models/university.cr @@ -97,6 +97,16 @@ class University < BaseModel end end + def marked(user) + cwb = chong_wen_bao_for(user) + + if cwb.nil? + false + else + cwb.is_marked + end + end + def marked_2023(user) cwb = chong_wen_bao_for(user) diff --git a/src/pages/me/show_page.cr b/src/pages/me/show_page.cr index 88c1956..4cdaaa6 100644 --- a/src/pages/me/show_page.cr +++ b/src/pages/me/show_page.cr @@ -28,9 +28,9 @@ class Me::ShowPage < MainLayout private def clear_chong_wen_bao div class: "row" do div class: "col m4" do - link( + a( "删除 冲 数据", - Me::ClearChong, + href: "#!", class: "btn", "hx-put": Me::ClearChong.path, "hx-swap": "none", @@ -40,9 +40,9 @@ class Me::ShowPage < MainLayout end div class: "col m4" do - link( + a( "删除 稳 数据", - Me::ClearWen, + href: "#!", class: "btn", "hx-put": Me::ClearWen.path, "hx-swap": "none", @@ -52,9 +52,9 @@ class Me::ShowPage < MainLayout end div class: "col m4" do - link( + a( "删除 保 数据", - Me::ClearChong, + href: "#!", class: "btn", "hx-put": Me::ClearBao.path, "hx-swap": "none", @@ -67,10 +67,10 @@ class Me::ShowPage < MainLayout br div class: "row" do - div class: "col m3" do - link( + div class: "col m2" do + a( "删除 2023 标记数据", - Universities::Htmx::Clear2023Mark, + href: "#!", class: "btn", "hx-put": Universities::Htmx::Clear2023Mark.path, "hx-swap": "none", @@ -79,10 +79,10 @@ class Me::ShowPage < MainLayout ) end - div class: "col m3" do - link( + div class: "col m2" do + a( "删除 2022 标记数据", - Universities::Htmx::Clear2022Mark, + href: "#!", class: "btn", "hx-put": Universities::Htmx::Clear2022Mark.path, "hx-swap": "none", @@ -91,10 +91,10 @@ class Me::ShowPage < MainLayout ) end - div class: "col m3" do - link( + div class: "col m2" do + a( "删除 2021 标记数据", - Universities::Htmx::Clear2021Mark, + href: "#!", class: "btn", "hx-put": Universities::Htmx::Clear2021Mark.path, "hx-swap": "none", @@ -103,10 +103,10 @@ class Me::ShowPage < MainLayout ) end - div class: "col m3" do - link( + div class: "col m2" do + a( "删除 2020 标记数据", - Universities::Htmx::Clear2020Mark, + href: "#!", class: "btn", "hx-put": Universities::Htmx::Clear2020Mark.path, "hx-swap": "none", @@ -114,6 +114,18 @@ class Me::ShowPage < MainLayout "hx-confirm": "确认要删除 2020 标记数据吗?", ) end + + div class: "col m2" do + a( + "删除 手动 标记数据", + href: "#!", + class: "btn", + "hx-put": Universities::Htmx::ClearMark.path, + "hx-swap": "none", + "hx-include": "[name='_csrf']", + "hx-confirm": "确认要删除 手动 标记数据吗?", + ) + end end end