feat: API-Endpunkte für Bereichs-Updates implementiert

- Neuer Endpunkt `updateBereich.js` für die Aktualisierung von Bereichskoordinaten per PUT-Request.
- Utility-Funktion `updateBereichUtil.js` hinzugefügt, um API-Aufrufe für Bereichs-Updates im Frontend zu kapseln.
- Fehler bei der URL in `updateBereichUtil.js` behoben, um sicherzustellen, dass die Koordinaten korrekt aktualisiert werden.
- Verbesserte Fehlerbehandlung und Logging zur Debug-Unterstützung.
This commit is contained in:
ISA
2024-12-21 05:49:43 +01:00
parent a82a92e89a
commit 35a6d4120b
2 changed files with 19 additions and 4 deletions

View File

@@ -1,13 +1,17 @@
// /pages/api/talas_v5_DB/bereich/updateBereich.js // /pages/api/talas_v5_DB/bereich/updateBereich.js
import getPool from "../../../utils/mysqlPool"; import getPool from "../../../../utils/mysqlPool";
export default async function handler(req, res) { export default async function handler(req, res) {
console.log("Request erhalten:", req.method, req.body); // Debugging
const pool = getPool(); const pool = getPool();
// Prüfe, ob die Methode PUT ist
if (req.method !== "PUT") { if (req.method !== "PUT") {
return res.status(405).json({ error: "Nur PUT Methode erlaubt" }); return res.status(405).json({ error: "Nur PUT Methode erlaubt" });
} }
// Daten aus dem Request-Body extrahieren
const { idLocation, idMap, x, y } = req.body; const { idLocation, idMap, x, y } = req.body;
if (!idLocation || !idMap || x === undefined || y === undefined) { if (!idLocation || !idMap || x === undefined || y === undefined) {
@@ -17,10 +21,12 @@ export default async function handler(req, res) {
let connection; let connection;
try { try {
// Verbindung zur Datenbank herstellen
connection = await pool.getConnection(); connection = await pool.getConnection();
const query = "UPDATE location_coordinates SET x = ?, y = ? WHERE idLocation = ? AND idMaps = ?"; const query = "UPDATE location_coordinates SET x = ?, y = ? WHERE idLocation = ? AND idMaps = ?";
const [result] = await connection.query(query, [x, y, idLocation, idMap]); const [result] = await connection.query(query, [x, y, idLocation, idMap]);
// Erfolgreiche Aktualisierung prüfen
if (result.affectedRows > 0) { if (result.affectedRows > 0) {
return res.status(200).json({ success: true, message: "Koordinaten erfolgreich aktualisiert" }); return res.status(200).json({ success: true, message: "Koordinaten erfolgreich aktualisiert" });
} else { } else {
@@ -31,7 +37,16 @@ export default async function handler(req, res) {
return res.status(500).json({ error: "Interner Serverfehler beim Aktualisieren der Koordinaten" }); return res.status(500).json({ error: "Interner Serverfehler beim Aktualisieren der Koordinaten" });
} finally { } finally {
if (connection) { if (connection) {
connection.release(); connection.release();
}
// Sicherheitshalber eine Standardantwort senden, falls keine vorherige Antwort existiert
if (!res.headersSent) {
res.status(500).json({ error: "Keine Antwort vom Server" });
} }
}
} }
}

View File

@@ -1,6 +1,6 @@
export const updateLocation = async (idLocation, idMap, newCoords) => { export const updateLocation = async (idLocation, idMap, newCoords) => {
try { try {
const response = await fetch("/api/updateBereich", { const response = await fetch("/api/talas_v5_DB/bereich/updateBereich", {
method: "PUT", method: "PUT",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",