mirror of
https://github.com/hkalexling/Mango.git
synced 2025-08-03 03:15:31 -04:00
Simplify #json_build
This commit is contained in:
parent
4b464ed361
commit
4eaf271fa4
@ -46,17 +46,19 @@ class Entry
|
|||||||
file.close
|
file.close
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_json(json : JSON::Builder, *, slim = false)
|
def build_json(*, slim = false)
|
||||||
json.object do
|
JSON.build do |json|
|
||||||
{% for str in ["zip_path", "title", "size", "id"] %}
|
json.object do
|
||||||
|
{% for str in ["zip_path", "title", "size", "id"] %}
|
||||||
json.field {{str}}, @{{str.id}}
|
json.field {{str}}, @{{str.id}}
|
||||||
{% end %}
|
{% end %}
|
||||||
json.field "title_id", @book.id
|
json.field "title_id", @book.id
|
||||||
json.field "pages" { json.number @pages }
|
json.field "pages" { json.number @pages }
|
||||||
unless slim
|
unless slim
|
||||||
json.field "display_name", @book.display_name @title
|
json.field "display_name", @book.display_name @title
|
||||||
json.field "cover_url", cover_url
|
json.field "cover_url", cover_url
|
||||||
json.field "mtime" { json.number @mtime.to_unix }
|
json.field "mtime" { json.number @mtime.to_unix }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -65,16 +65,15 @@ class Library
|
|||||||
titles.flat_map &.deep_entries
|
titles.flat_map &.deep_entries
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_json(json : JSON::Builder, *, slim = false, shallow = false)
|
def build_json(*, slim = false, shallow = false)
|
||||||
json.object do
|
JSON.build do |json|
|
||||||
json.field "dir", @dir
|
json.object do
|
||||||
json.field "titles" do
|
json.field "dir", @dir
|
||||||
json.array do
|
json.field "titles" do
|
||||||
self.titles.each do |title|
|
json.array do
|
||||||
raw = JSON.build do |j|
|
self.titles.each do |title|
|
||||||
title.build_json j, slim: slim, shallow: shallow
|
json.raw title.build_json(slim: slim, shallow: shallow)
|
||||||
end
|
end
|
||||||
json.raw raw
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -65,51 +65,47 @@ class Title
|
|||||||
|
|
||||||
alias SortContext = NamedTuple(username: String, opt: SortOptions)
|
alias SortContext = NamedTuple(username: String, opt: SortOptions)
|
||||||
|
|
||||||
def build_json(json : JSON::Builder, *, slim = false, shallow = false,
|
def build_json(*, slim = false, shallow = false,
|
||||||
sort_context : SortContext? = nil)
|
sort_context : SortContext? = nil)
|
||||||
json.object do
|
JSON.build do |json|
|
||||||
{% for str in ["dir", "title", "id"] %}
|
json.object do
|
||||||
|
{% for str in ["dir", "title", "id"] %}
|
||||||
json.field {{str}}, @{{str.id}}
|
json.field {{str}}, @{{str.id}}
|
||||||
{% end %}
|
{% end %}
|
||||||
json.field "signature" { json.number @signature }
|
json.field "signature" { json.number @signature }
|
||||||
unless slim
|
unless slim
|
||||||
json.field "display_name", display_name
|
json.field "display_name", display_name
|
||||||
json.field "cover_url", cover_url
|
json.field "cover_url", cover_url
|
||||||
json.field "mtime" { json.number @mtime.to_unix }
|
json.field "mtime" { json.number @mtime.to_unix }
|
||||||
end
|
end
|
||||||
unless shallow
|
unless shallow
|
||||||
json.field "titles" do
|
json.field "titles" do
|
||||||
json.array do
|
json.array do
|
||||||
self.titles.each do |title|
|
self.titles.each do |title|
|
||||||
raw = JSON.build do |j|
|
json.raw title.build_json(slim: slim, shallow: shallow)
|
||||||
title.build_json j, slim: slim, shallow: shallow
|
|
||||||
end
|
end
|
||||||
json.raw raw
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
json.field "entries" do
|
||||||
json.field "entries" do
|
json.array do
|
||||||
json.array do
|
_entries = if sort_context
|
||||||
_entries = if sort_context
|
sorted_entries sort_context[:username],
|
||||||
sorted_entries sort_context[:username],
|
sort_context[:opt]
|
||||||
sort_context[:opt]
|
else
|
||||||
else
|
@entries
|
||||||
@entries
|
end
|
||||||
end
|
_entries.each do |entry|
|
||||||
_entries.each do |entry|
|
json.raw entry.build_json(slim: slim)
|
||||||
raw = JSON.build do |j|
|
|
||||||
entry.build_json j, slim: slim
|
|
||||||
end
|
end
|
||||||
json.raw raw
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
json.field "parents" do
|
||||||
json.field "parents" do
|
json.array do
|
||||||
json.array do
|
self.parents.each do |title|
|
||||||
self.parents.each do |title|
|
json.object do
|
||||||
json.object do
|
json.field "title", title.title
|
||||||
json.field "title", title.title
|
json.field "id", title.id
|
||||||
json.field "id", title.id
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -158,10 +158,9 @@ struct APIRouter
|
|||||||
slim = !env.params.query["slim"]?.nil?
|
slim = !env.params.query["slim"]?.nil?
|
||||||
shallow = !env.params.query["shallow"]?.nil?
|
shallow = !env.params.query["shallow"]?.nil?
|
||||||
|
|
||||||
json = JSON.build do |j|
|
send_json env, title.build_json(slim: slim, shallow: shallow,
|
||||||
title.build_json j, slim: slim, shallow: shallow,
|
sort_context: {username: username,
|
||||||
sort_context: {username: username, opt: sort_opt}
|
opt: sort_opt})
|
||||||
end
|
|
||||||
rescue e
|
rescue e
|
||||||
Logger.error e
|
Logger.error e
|
||||||
env.response.status_code = 404
|
env.response.status_code = 404
|
||||||
@ -184,10 +183,7 @@ struct APIRouter
|
|||||||
slim = !env.params.query["slim"]?.nil?
|
slim = !env.params.query["slim"]?.nil?
|
||||||
shallow = !env.params.query["shallow"]?.nil?
|
shallow = !env.params.query["shallow"]?.nil?
|
||||||
|
|
||||||
json = JSON.build do |j|
|
send_json env, Library.default.build_json(slim: slim, shallow: shallow)
|
||||||
Library.default.build_json j, slim: slim, shallow: shallow
|
|
||||||
end
|
|
||||||
send_json env, json
|
|
||||||
end
|
end
|
||||||
|
|
||||||
Koa.describe "Triggers a library scan"
|
Koa.describe "Triggers a library scan"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user