diff --git a/src/library/entry.cr b/src/library/entry.cr index 4ff01ee..92f4def 100644 --- a/src/library/entry.cr +++ b/src/library/entry.cr @@ -46,6 +46,18 @@ class Entry file.close end + def to_slim_json : String + JSON.build do |json| + json.object do + {% for str in ["zip_path", "title", "size", "id"] %} + json.field {{str}}, @{{str.id}} + {% end %} + json.field "title_id", @book.id + json.field "pages" { json.number @pages } + end + end + end + def to_json(json : JSON::Builder) json.object do {% for str in ["zip_path", "title", "size", "id"] %} diff --git a/src/library/library.cr b/src/library/library.cr index 8fad451..e359847 100644 --- a/src/library/library.cr +++ b/src/library/library.cr @@ -85,6 +85,21 @@ class Library titles + titles.flat_map &.deep_titles end + def to_slim_json : String + 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.to_slim_json + end + end + end + end + end + end + def to_json(json : JSON::Builder) json.object do json.field "dir", @dir diff --git a/src/library/title.cr b/src/library/title.cr index a757d42..61c9813 100644 --- a/src/library/title.cr +++ b/src/library/title.cr @@ -57,6 +57,41 @@ class Title end end + def to_slim_json : String + JSON.build do |json| + json.object do + {% for str in ["dir", "title", "id"] %} + json.field {{str}}, @{{str.id}} + {% end %} + json.field "signature" { json.number @signature } + json.field "titles" do + json.array do + self.titles.each do |title| + json.raw title.to_slim_json + end + end + end + json.field "entries" do + json.array do + @entries.each do |entry| + json.raw entry.to_slim_json + end + end + end + json.field "parents" do + json.array do + self.parents.each do |title| + json.object do + json.field "title", title.title + json.field "id", title.id + end + end + end + end + end + end + end + def to_json(json : JSON::Builder) json.object do {% for str in ["dir", "title", "id"] %} diff --git a/src/routes/api.cr b/src/routes/api.cr index 0102e0d..a66210a 100644 --- a/src/routes/api.cr +++ b/src/routes/api.cr @@ -126,8 +126,11 @@ struct APIRouter end end - Koa.describe "Returns the book with title `tid`" + Koa.describe "Returns the book with title `tid`", <<-MD + Supply the `tid` query parameter to strip away "display_name", "cover_url", and "mtime" from the returned object to speed up the loading time + MD Koa.path "tid", desc: "Title ID" + Koa.query "slim" Koa.response 200, schema: "title" Koa.response 404, "Title not found" Koa.tag "library" @@ -137,7 +140,11 @@ struct APIRouter title = Library.default.get_title tid raise "Title ID `#{tid}` not found" if title.nil? - send_json env, title.to_json + if env.params.query["slim"]? + send_json env, title.to_slim_json + else + send_json env, title.to_json + end rescue e Logger.error e env.response.status_code = 404 @@ -145,14 +152,21 @@ struct APIRouter end end - Koa.describe "Returns the entire library with all titles and entries" + Koa.describe "Returns the entire library with all titles and entries", <<-MD + Supply the `tid` query parameter to strip away "display_name", "cover_url", and "mtime" from the returned object to speed up the loading time + MD + Koa.query "slim" Koa.response 200, schema: { "dir" => String, "titles" => ["title"], } Koa.tag "library" get "/api/library" do |env| - send_json env, Library.default.to_json + if env.params.query["slim"]? + send_json env, Library.default.to_slim_json + else + send_json env, Library.default.to_json + end end Koa.describe "Triggers a library scan"