diff --git a/public/api/_db.php b/public/api/_db.php index 943fc92..708ff5c 100755 --- a/public/api/_db.php +++ b/public/api/_db.php @@ -1,13 +1,7 @@ PDO::ERRMODE_EXCEPTION, - PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, -]; try { - $pdo = new PDO($dsn, $cfg['db_user'], $cfg['db_pass'], $options); + require_once __DIR__ . '/../../tools/db.php'; + $pdo = tools_build_pdo(); } catch (PDOException $e) { http_response_code(500); header('Content-Type: application/json'); diff --git a/public/api/printers.php b/public/api/printers.php index c560186..1adb40e 100755 --- a/public/api/printers.php +++ b/public/api/printers.php @@ -2,5 +2,6 @@ // public/api/printers.php header('Content-Type: application/json; charset=utf-8'); require __DIR__ . '/_db.php'; -$stmt = $pdo->query("SELECT * FROM printers WHERE is_active = 1 ORDER BY name"); -echo json_encode($stmt->fetchAll(), JSON_UNESCAPED_UNICODE); +require_once __DIR__ . '/../../tools/printers.php'; +$printers = tools_fetch_active_printers($pdo); +echo json_encode($printers, JSON_UNESCAPED_UNICODE); diff --git a/tools/db.php b/tools/db.php new file mode 100644 index 0000000..2a61402 --- /dev/null +++ b/tools/db.php @@ -0,0 +1,12 @@ + PDO::ERRMODE_EXCEPTION, + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, + ]; + + return new PDO($dsn, $cfg['db_user'], $cfg['db_pass'], $options); +} diff --git a/tools/printers.php b/tools/printers.php new file mode 100644 index 0000000..674261c --- /dev/null +++ b/tools/printers.php @@ -0,0 +1,6 @@ +query("SELECT * FROM printers WHERE is_active = 1 ORDER BY name"); + return $stmt->fetchAll(); +}