about summary refs log tree commit diff stats
path: root/classes/Handler/GetinfoHandler.php
blob: e5996714c54336c796a6bb84ab570b7d71b0cdcd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php

namespace Handler;

use Config;
use Request;
use RequestHandlerInterface;
use Response;
use PDO;

class GetinfoHandler implements RequestHandlerInterface
{
    public function handle(Request $request): Response
    {
        $id = (int) $request->getAttribute('id');
        $caldb = new PDO('sqlite:' . Config::get(Config::CALIBRE_DB));

        $sth = $caldb->prepare("SELECT books.*, s.name AS series_name,
            (SELECT text FROM comments WHERE book = books.id) AS comment,
            (SELECT id FROM data WHERE book = books.id AND format = 'EPUB' LIMIT 1) AS epub_id FROM books
            LEFT JOIN books_series_link AS bsl ON (bsl.book = books.id)
            LEFT JOIN series AS s ON (bsl.series = s.id)
            WHERE books.id = ?");
        $sth->execute([$id]);

        if ($row = $sth->fetch()) {
            return (new Response())->withBody(json_encode($row));
        }

        return (new Response())
            ->withStatus(404)
            ->withBody("Not found.");
    }
}