API sorting improvements:

- Add endpoints for getting/updating sorting methods
- Results from library and title endpoints are now sorted
This commit is contained in:
Alex Ling
2022-03-22 10:46:38 +00:00
parent a5a7396edd
commit 7ecdb1c0dd
4 changed files with 116 additions and 11 deletions
+10 -3
View File
@@ -139,14 +139,21 @@ class Library
titles.flat_map &.deep_entries
end
def build_json(*, slim = false, depth = -1)
def build_json(*, slim = false, depth = -1, sort_context = nil)
JSON.build do |json|
json.object do
json.field "dir", @dir
json.field "titles" do
json.array do
self.titles.each do |title|
json.raw title.build_json(slim: slim, depth: depth)
_titles = if sort_context
sorted_titles sort_context[:username],
sort_context[:opt]
else
self.titles
end
_titles.each do |title|
json.raw title.build_json(slim: slim, depth: depth,
sort_context: sort_context)
end
end
end
+9 -2
View File
@@ -218,9 +218,16 @@ class Title
unless depth == 0
json.field "titles" do
json.array do
self.titles.each do |title|
_titles = if sort_context
sorted_titles sort_context[:username],
sort_context[:opt]
else
self.titles
end
_titles.each do |title|
json.raw title.build_json(slim: slim,
depth: depth > 0 ? depth - 1 : depth)
depth: depth > 0 ? depth - 1 : depth,
sort_context: sort_context)
end
end
end
+7
View File
@@ -55,6 +55,13 @@ class SortOptions
def to_tuple
{@method.to_s.underscore, ascend}
end
def to_json
{
"method" => method.to_s.underscore,
"ascend" => ascend,
}.to_json
end
end
struct Image