diff options
| author | Andrew Dolgov <fox@fakecake.org> | 2026-05-26 10:51:49 +0300 |
|---|---|---|
| committer | Andrew Dolgov <fox@fakecake.org> | 2026-05-26 10:51:49 +0300 |
| commit | ab7702f0e3dd63bdeaea3cb6db138878774c3b47 (patch) | |
| tree | 2f26bffd847b8e095e0446daee3879c849c9adc8 /classes/Handler | |
| parent | 7585cd2047345487e062a86ea7b3fee1acf52d68 (diff) | |
| download | the-epube-ab7702f0e3dd63bdeaea3cb6db138878774c3b47.tar.gz the-epube-ab7702f0e3dd63bdeaea3cb6db138878774c3b47.tar.xz | |
unify pagination-related handlers
Diffstat (limited to 'classes/Handler')
| -rw-r--r-- | classes/Handler/DeletepaginationHandler.php | 32 | ||||
| -rw-r--r-- | classes/Handler/GetpaginationHandler.php | 31 | ||||
| -rw-r--r-- | classes/Handler/PaginationHandler.php | 80 | ||||
| -rw-r--r-- | classes/Handler/StorepaginationHandler.php | 35 |
4 files changed, 80 insertions, 98 deletions
diff --git a/classes/Handler/DeletepaginationHandler.php b/classes/Handler/DeletepaginationHandler.php deleted file mode 100644 index 9ebfa57..0000000 --- a/classes/Handler/DeletepaginationHandler.php +++ /dev/null @@ -1,32 +0,0 @@ -<?php - -namespace Handler; - -use AuthRequired; -use ORM; -use Request; -use RequestHandlerInterface; -use Response; - -class DeletepaginationHandler implements RequestHandlerInterface, AuthRequired -{ - public function handle(Request $request): Response - { - $bookid = (int) $request->getAttribute('id'); - - if ($bookid) { - $pag = ORM::for_table('epube_pagination') - ->where('bookid', $bookid) - ->find_one(); - - if ($pag) { - $pag->delete(); - return new Response(); - } - } - - return (new Response()) - ->withStatus(404) - ->withBody("Not found."); - } -} diff --git a/classes/Handler/GetpaginationHandler.php b/classes/Handler/GetpaginationHandler.php deleted file mode 100644 index 282c724..0000000 --- a/classes/Handler/GetpaginationHandler.php +++ /dev/null @@ -1,31 +0,0 @@ -<?php - -namespace Handler; - -use AuthRequired; -use ORM; -use Request; -use RequestHandlerInterface; -use Response; - -class GetpaginationHandler implements RequestHandlerInterface, AuthRequired -{ - public function handle(Request $request): Response - { - $bookid = (int) $request->getAttribute('id'); - - if ($bookid) { - $pag = ORM::for_table('epube_pagination') - ->where('bookid', $bookid) - ->find_one(); - - if ($pag) { - return (new Response())->withBody($pag->pagination); - } - } - - return (new Response()) - ->withStatus(404) - ->withBody("File not found."); - } -} diff --git a/classes/Handler/PaginationHandler.php b/classes/Handler/PaginationHandler.php new file mode 100644 index 0000000..c126762 --- /dev/null +++ b/classes/Handler/PaginationHandler.php @@ -0,0 +1,80 @@ +<?php + +namespace Handler; + +use AuthRequired; +use ORM; +use Request; +use RequestHandlerInterface; +use Response; + +class PaginationHandler implements RequestHandlerInterface, AuthRequired +{ + private function handleGet(int $bookid): Response + { + if ($bookid) { + $pag = ORM::for_table('epube_pagination') + ->where('bookid', $bookid) + ->find_one(); + + if ($pag) { + return (new Response())->withBody($pag->pagination); + } + } + + return (new Response()) + ->withStatus(404) + ->withBody("File not found."); + } + + private function handleStore(int $bookid, ?string $payload): Response + { + if ($bookid && $payload) { + $pag = ORM::for_table('epube_pagination') + ->where('bookid', $bookid) + ->find_one(); + + if (!$pag) { + $pag = ORM::for_table('epube_pagination')->create(); + $pag->bookid = $bookid; + } + + $pag->pagination = $payload; + $pag->total_pages = 100; + $pag->save(); + } + + return new Response(); + } + + private function handleDelete(int $bookid): Response + { + if ($bookid) { + $pag = ORM::for_table('epube_pagination') + ->where('bookid', $bookid) + ->find_one(); + + if ($pag) { + $pag->delete(); + return new Response(); + } + } + + return (new Response()) + ->withStatus(404) + ->withBody("Not found."); + } + + public function handle(Request $request): Response + { + $subOp = $request->getAttribute('op', ''); + $bookid = (int) $request->getAttribute('id'); + + return match ($subOp) { + 'getpagination' => $this->handleGet($bookid), + 'storepagination' => $this->handleStore($bookid, $request->getAttribute('payload')), + 'deletepagination' => $this->handleDelete($bookid), + default => (new Response())->withStatus(404)->withBody("Not found."), + }; + } +} diff --git a/classes/Handler/StorepaginationHandler.php b/classes/Handler/StorepaginationHandler.php deleted file mode 100644 index 8d272af..0000000 --- a/classes/Handler/StorepaginationHandler.php +++ /dev/null @@ -1,35 +0,0 @@ -<?php - -namespace Handler; - -use AuthRequired; -use ORM; -use Request; -use RequestHandlerInterface; -use Response; - -class StorepaginationHandler implements RequestHandlerInterface, AuthRequired -{ - public function handle(Request $request): Response - { - $bookid = (int) $request->getAttribute('id'); - $payload = $request->getAttribute('payload'); - - if ($bookid && $payload) { - $pag = ORM::for_table('epube_pagination') - ->where('bookid', $bookid) - ->find_one(); - - if (!$pag) { - $pag = ORM::for_table('epube_pagination')->create(); - $pag->bookid = $bookid; - } - - $pag->pagination = $payload; - $pag->total_pages = 100; - $pag->save(); - } - - return new Response(); - } -} |
