about summary refs log tree commit diff stats
path: root/classes/Handler/GetlastreadHandler.php
diff options
context:
space:
mode:
Diffstat (limited to 'classes/Handler/GetlastreadHandler.php')
-rw-r--r--classes/Handler/GetlastreadHandler.php37
1 files changed, 37 insertions, 0 deletions
diff --git a/classes/Handler/GetlastreadHandler.php b/classes/Handler/GetlastreadHandler.php
new file mode 100644
index 0000000..784c6dd
--- /dev/null
+++ b/classes/Handler/GetlastreadHandler.php
@@ -0,0 +1,37 @@
+<?php
+
+namespace Handler;
+
+use ORM;
+use Request;
+use RequestHandlerInterface;
+use Response;
+
+class GetlastreadHandler implements RequestHandlerInterface
+{
+    public function handle(Request $request): Response
+    {
+        $bookid = (int) $request->getAttribute('id');
+        $owner = $_SESSION["owner"] ?? "";
+
+        if ($bookid) {
+            $book = ORM::for_table('epube_books')
+                ->where('bookid', $bookid)
+                ->where('owner', $owner)
+                ->find_one();
+
+            if ($book) {
+                return (new Response())->withBody(json_encode([
+                    "page" => (int)$book->lastread,
+                    "cfi" => $book->lastcfi,
+                    "total" => 100,
+                    "timestamp" => (int)$book->lastts
+                ]));
+            }
+        }
+
+        return (new Response())
+            ->withStatus(404)
+            ->withBody("Not found.");
+    }
+}