diff --git a/pages/api/locationDeviceNameById.js b/pages/api/locationDeviceNameById.js new file mode 100644 index 000000000..5956ea60b --- /dev/null +++ b/pages/api/locationDeviceNameById.js @@ -0,0 +1,39 @@ +// API in /api/locationDeviceNameById.js +import mysql from "mysql"; + + +const dbConfig = { + host: process.env.DB_HOST, + user: process.env.DB_USER, + password: process.env.DB_PASSWORD, + database: process.env.DB_NAME, + port: process.env.DB_PORT, +}; + +const connection = mysql.createConnection(dbConfig); +connection.connect((err) => { + if (err) { + console.error("Fehler beim Verbinden:", err.stack); + return; + } +}); + +export default function handler(req, res) { + if (req.method !== "GET") { + return res.status(405).json({ error: "Nur GET Methode erlaubt" }); + } + const { idLD } = req.query; + + const query = "SELECT name FROM location_device WHERE idLD = ?"; + connection.query(query, [idLD], (error, results) => { + if (error) { + console.error("Fehler beim Abrufen des Gerätenamens:", error); + return res.status(500).json({ error: "Fehler beim Abrufen des Gerätenamens" }); + } + if (results.length > 0) { + res.json({ name: results[0].name }); + } else { + res.status(404).json({ error: "Gerät nicht gefunden" }); + } + }); +}