Skip to content

Commit

Permalink
Get pagination through document list to work again
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfgangmm committed Jul 17, 2024
1 parent f42b8f6 commit ca478d5
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 9 deletions.
47 changes: 47 additions & 0 deletions profiles/base/modules/lib/api.tpl.xql
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
xquery version "3.1";

declare namespace api="https://tei-publisher.com/xquery/api";
declare namespace output="http://www.w3.org/2010/xslt-xquery-serialization";
declare namespace tei="http://www.tei-c.org/ns/1.0";

import module namespace roaster="http://e-editiones.org/roaster";
import module namespace auth="http://e-editiones.org/roaster/auth";
import module namespace rutil="http://e-editiones.org/roaster/util";
import module namespace dapi="http://teipublisher.com/api/documents" at "api/document.xql";
import module namespace capi="http://teipublisher.com/api/collection" at "api/collection.xql";
import module namespace sapi="http://teipublisher.com/api/search" at "api/search.xql";
import module namespace deploy="http://teipublisher.com/api/generate" at "api/generate.xql";
import module namespace dts="http://teipublisher.com/api/dts" at "api/dts.xql";
import module namespace iapi="http://teipublisher.com/api/info" at "api/info.xql";
import module namespace vapi="http://teipublisher.com/api/view" at "api/view.xql";
import module namespace anno="http://teipublisher.com/api/annotations" at "api/annotations.xql";
import module namespace iiif="https://e-editiones.org/api/iiif" at "api/iiif.xql";
import module namespace custom="http://teipublisher.com/api/custom" at "../custom-api.xql";
import module namespace nlp="http://teipublisher.com/api/nlp" at "api/nlp.xql";
import module namespace rapi="http://teipublisher.com/api/registers" at "../registers.xql";

declare option output:indent "no";

let $lookup := function($name as xs:string) {
try {
let $cfun := custom:lookup($name, 1)
return
if (empty($cfun)) then
function-lookup(xs:QName($name), 1)
else
$cfun
} catch * {
()
}
}
let $resp := roaster:route(
(
"modules/custom-api.json",
[% for $api in $apis?* %]
"[[$api]]",
[% endfor %]
"modules/lib/api.json"
),
$lookup)
return
$resp
36 changes: 36 additions & 0 deletions profiles/base10/modules/lib/api.tpl.json
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,24 @@
"example": "test"
}
},
{
"name": "start",
"in": "query",
"schema": {
"type": "integer",
"minimum": 1,
"default": 1
}
},
{
"name": "per-page",
"in": "query",
"schema": {
"type": "integer",
"minimum": 1,
"default": 10
}
},
{
"name": "format",
"in": "query",
Expand Down Expand Up @@ -446,6 +464,24 @@
"enum": ["html", "json"],
"default": "html"
}
},
{
"name": "start",
"in": "query",
"schema": {
"type": "integer",
"minimum": 1,
"default": 1
}
},
{
"name": "per-page",
"in": "query",
"schema": {
"type": "integer",
"minimum": 1,
"default": 10
}
}
],
"responses": {
Expand Down
22 changes: 15 additions & 7 deletions profiles/base10/modules/lib/api/collection.xql
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,38 @@ module namespace capi="http://teipublisher.com/api/collection";
import module namespace errors = "http://e-editiones.org/roaster/errors";
import module namespace config="http://www.tei-c.org/tei-simple/config" at "../../config.xqm";
import module namespace browse="http://www.tei-c.org/tei-simple/templates" at "../browse.xql";
import module namespace pages="http://www.tei-c.org/tei-simple/pages" at "../pages.xql";
import module namespace tpu="http://www.tei-c.org/tei-publisher/util" at "../util.xql";
import module namespace templates="http://exist-db.org/xquery/html-templating";
import module namespace lib="http://exist-db.org/xquery/html-templating/lib";
import module namespace vapi="http://teipublisher.com/api/view" at "view.xql";
import module namespace docx="http://existsolutions.com/teipublisher/docx";
import module namespace pm-config="http://www.tei-c.org/tei-simple/pm-config" at "../../pm-config.xql";
import module namespace custom="http://teipublisher.com/api/custom" at "../../custom-api.xql";
import module namespace query="http://www.tei-c.org/tei-simple/query" at "../query.xql";
import module namespace nav="http://www.tei-c.org/tei-simple/navigation" at "../navigation.xql";
import module namespace router="http://e-editiones.org/roaster";
import module namespace tmpl="http://e-editiones.org/xquery/templates";

declare function capi:list($request as map(*)) {
let $per-page := $request?parameters?per-page
let $path := if ($request?parameters?path) then xmldb:decode($request?parameters?path) else ()
let $params := capi:params2map($path)
let $cached := session:get-attribute($config:session-prefix || ".works")
let $useCached := capi:use-cache($params, $cached)
let $works := capi:list-works($path, if ($useCached) then $cached else (), $params)
return
let $worksAll := capi:list-works($path, if ($useCached) then $cached else (), $params)
let $total := count($worksAll?all)
let $start :=
if ($request?parameters?start > $total) then
($total idiv $per-page) * $per-page + 1
else
$request?parameters?start
let $works := subsequence($worksAll?all, $start, $per-page)
return (
response:set-header("pb-start", xs:string($start)),
response:set-header("pb-total", xs:string($total)),
if ($request?parameters?format = "html") then
let $templatePath := $config:data-root || "/" || $path || "/collection.html"
let $templateAvail := doc-available($templatePath) or util:binary-doc-available($templatePath)
let $path :=
if ($templateAvail and $works?mode = 'browse') then
if ($templateAvail and $worksAll?mode = 'browse') then
$templatePath
else
$config:app-root || "/templates/documents.html"
Expand All @@ -40,7 +47,7 @@ declare function capi:list($request as map(*)) {
util:binary-doc($path) => util:binary-to-string()
else
error($errors:NOT_FOUND, "HTML file " || $path || " not found")
let $model := map:merge((vapi:load-config-json($request), map { "browse": $works }))
let $model := map:merge((vapi:load-config-json($request), map { "documents": $works }))
return
tmpl:process($template, $model, map {
"plainText": false(),
Expand All @@ -54,6 +61,7 @@ declare function capi:list($request as map(*)) {
})
else
router:response(200, "application/json", $works)
)
};

declare
Expand Down
5 changes: 3 additions & 2 deletions profiles/base10/templates/documents.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<div class="collection">
[# Receives a list of documents to display in $documents #]
<ul class="documents">
[% if empty($browse?all) %]
[% if empty($documents) %]
<li>No documents</li>
[% else %]
[% for $doc in $browse?all %]
[% for $doc in $documents %]
[% let $options = browse:document-options($doc) %]
<li class="document">
<div class="document-info">
Expand Down

0 comments on commit ca478d5

Please sign in to comment.