up
This commit is contained in:
16
public/api/_db.php
Executable file
16
public/api/_db.php
Executable file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
// public/api/_db.php
|
||||
$cfg = require __DIR__ . '/../../src/config.php';
|
||||
$dsn = "mysql:host={$cfg['db_host']};dbname={$cfg['db_name']};charset={$cfg['db_charset']}";
|
||||
$options = [
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||
];
|
||||
try {
|
||||
$pdo = new PDO($dsn, $cfg['db_user'], $cfg['db_pass'], $options);
|
||||
} catch (PDOException $e) {
|
||||
http_response_code(500);
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode(['error' => 'DB connection failed']);
|
||||
exit;
|
||||
}
|
||||
6
public/api/materials.php
Executable file
6
public/api/materials.php
Executable file
@@ -0,0 +1,6 @@
|
||||
<?php
|
||||
// public/api/materials.php
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
require __DIR__ . '/_db.php';
|
||||
$stmt = $pdo->query("SELECT * FROM materials WHERE is_active = 1 ORDER BY code");
|
||||
echo json_encode($stmt->fetchAll(), JSON_UNESCAPED_UNICODE);
|
||||
35
public/api/printer-materials.php
Executable file
35
public/api/printer-materials.php
Executable file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
// public/api/printer-materials.php?id={printer_id}
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
require __DIR__ . '/_db.php';
|
||||
|
||||
$printer_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
|
||||
if ($printer_id <= 0) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'printer id missing']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$printerStmt = $pdo->prepare("SELECT * FROM printers WHERE id = ?");
|
||||
$printerStmt->execute([$printer_id]);
|
||||
$printer = $printerStmt->fetch();
|
||||
if (!$printer) {
|
||||
http_response_code(404);
|
||||
echo json_encode(['error' => 'printer not found']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$sql = "SELECT m.*, pms.support_level, pms.partial_reason, pms.extra_info
|
||||
FROM materials m
|
||||
LEFT JOIN printer_material_support pms
|
||||
ON pms.material_id = m.id AND pms.printer_id = :pid
|
||||
WHERE m.is_active = 1
|
||||
ORDER BY m.code";
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([':pid' => $printer_id]);
|
||||
$materials = $stmt->fetchAll();
|
||||
|
||||
echo json_encode([
|
||||
'printer' => $printer,
|
||||
'materials' => $materials
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
6
public/api/printers.php
Executable file
6
public/api/printers.php
Executable file
@@ -0,0 +1,6 @@
|
||||
<?php
|
||||
// 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);
|
||||
Reference in New Issue
Block a user