From 1eace2c64ca61117fdf4b901d6875f548c385c8b Mon Sep 17 00:00:00 2001 From: Alex Ling Date: Wed, 30 Dec 2020 08:15:20 +0000 Subject: [PATCH] Add the /tags/:tag page --- src/library/library.cr | 25 ++----------------------- src/routes/main.cr | 25 +++++++++++++++++++++++++ src/util/util.cr | 25 +++++++++++++++++++++++++ src/views/components/tags.html.ecr | 2 +- src/views/tags.html.ecr | 30 ++++++++++++++++++++++++++++++ 5 files changed, 83 insertions(+), 24 deletions(-) create mode 100644 src/views/tags.html.ecr diff --git a/src/library/library.cr b/src/library/library.cr index 35d9e89..a7e1c49 100644 --- a/src/library/library.cr +++ b/src/library/library.cr @@ -68,29 +68,8 @@ class Library end end - # This is a hack to bypass a compiler bug - ary = titles - - case opt.not_nil!.method - when .time_modified? - ary.sort! { |a, b| (a.mtime <=> b.mtime).or \ - compare_numerically a.title, b.title } - when .progress? - ary.sort! do |a, b| - (a.load_percentage(username) <=> b.load_percentage(username)).or \ - compare_numerically a.title, b.title - end - else - unless opt.method.auto? - Logger.warn "Unknown sorting method #{opt.not_nil!.method}. Using " \ - "Auto instead" - end - ary.sort! { |a, b| compare_numerically a.title, b.title } - end - - ary.reverse! unless opt.not_nil!.ascend - - ary + # Helper function from src/util/util.cr + sort_titles titles, opt.not_nil!, username end def deep_titles diff --git a/src/routes/main.cr b/src/routes/main.cr index cb919cf..67719b8 100644 --- a/src/routes/main.cr +++ b/src/routes/main.cr @@ -114,6 +114,31 @@ class MainRouter < Router end end + get "/tags/:tag" do |env| + begin + username = get_username env + tag = env.params.url["tag"] + + sort_opt = SortOptions.new + get_sort_opt + + title_ids = Storage.default.get_tag_titles tag + + raise "Tag #{tag} not found" if title_ids.empty? + + titles = title_ids.map { |id| @context.library.get_title id } + .select Title + + titles = sort_titles titles, sort_opt, username + percentage = titles.map &.load_percentage username + + layout "tags" + rescue e + @context.error e + env.response.status_code = 404 + end + end + get "/api" do |env| render "src/views/api.html.ecr" end diff --git a/src/util/util.cr b/src/util/util.cr index 3fc1b2d..d7c0412 100644 --- a/src/util/util.cr +++ b/src/util/util.cr @@ -67,3 +67,28 @@ def env_is_true?(key : String) : Bool return false unless val val.downcase.in? "1", "true" end + +def sort_titles(titles : Array(Title), opt : SortOptions, username : String) + ary = titles + + case opt.method + when .time_modified? + ary.sort! { |a, b| (a.mtime <=> b.mtime).or \ + compare_numerically a.title, b.title } + when .progress? + ary.sort! do |a, b| + (a.load_percentage(username) <=> b.load_percentage(username)).or \ + compare_numerically a.title, b.title + end + else + unless opt.method.auto? + Logger.warn "Unknown sorting method #{opt.not_nil!.method}. Using " \ + "Auto instead" + end + ary.sort! { |a, b| compare_numerically a.title, b.title } + end + + ary.reverse! unless opt.not_nil!.ascend + + ary +end diff --git a/src/views/components/tags.html.ecr b/src/views/components/tags.html.ecr index a443393..7795dcf 100644 --- a/src/views/components/tags.html.ecr +++ b/src/views/components/tags.html.ecr @@ -6,7 +6,7 @@ Tags: diff --git a/src/views/tags.html.ecr b/src/views/tags.html.ecr new file mode 100644 index 0000000..58300e1 --- /dev/null +++ b/src/views/tags.html.ecr @@ -0,0 +1,30 @@ +

Tag: <%= tag %>

+

<%= titles.size %> titles tagged

+
+
+ +
+
+ <% hash = { + "auto" => "Auto", + "time_modified" => "Date Modified", + "progress" => "Progress" + } %> + <%= render_component "sort-form" %> +
+
+
+ <% titles.each_with_index do |item, i| %> + <% progress = percentage[i] %> + <%= render_component "card" %> + <% end %> +
+ +<% content_for "script" do %> + <%= render_component "dots-scripts" %> + + +<% end %>