39 lines
1.2 KiB
JavaScript
39 lines
1.2 KiB
JavaScript
// API in /api/talas_v5_DB/locationDevice/getDeviceId.js
|
|
import getPool from "../../../../utils/mysqlPool"; // Singleton-Pool importieren
|
|
|
|
export default async function handler(req, res) {
|
|
const pool = getPool(); // Singleton-Pool verwenden
|
|
|
|
if (req.method !== "GET") {
|
|
return res.status(405).json({ error: "Nur GET Methode erlaubt" });
|
|
}
|
|
|
|
const { deviceName } = req.query;
|
|
|
|
if (!deviceName) {
|
|
return res.status(400).json({ error: "deviceName ist erforderlich" });
|
|
}
|
|
|
|
const query = "SELECT idLD FROM location_device WHERE name = ?";
|
|
|
|
let connection;
|
|
|
|
try {
|
|
connection = await pool.getConnection(); // Hole eine Verbindung aus dem Pool
|
|
|
|
// Ausführen der Abfrage
|
|
const [results] = await connection.query(query, [deviceName]);
|
|
|
|
if (results.length > 0) {
|
|
res.status(200).json({ idLD: results[0].idLD });
|
|
} else {
|
|
res.status(404).json({ error: "Gerät nicht gefunden" });
|
|
}
|
|
} catch (error) {
|
|
console.error("Fehler beim Abrufen der Geräte-ID:", error);
|
|
res.status(500).json({ error: "Fehler beim Abrufen der Geräte-ID" });
|
|
} finally {
|
|
if (connection) connection.release(); // Gib die Verbindung in den Pool zurück
|
|
}
|
|
}
|