diff --git a/components/PoiUpdateModal.js b/components/PoiUpdateModal.js index bed6e3814..2b48214db 100644 --- a/components/PoiUpdateModal.js +++ b/components/PoiUpdateModal.js @@ -68,8 +68,33 @@ const PoiUpdateModal = ({ onClose, poiData }) => { // Form submission handler const handleSubmit = async (event) => { - event.preventDefault(); // Prevent the form from submitting - // Perform update operation here + event.preventDefault(); + try { + const response = await fetch("/api/updatePoi", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + idPoi: poiId, + description: name, + idPoiTyp: poiTypeId, + }), + }); + + if (response.ok) { + alert("POI wurde erfolgreich aktualisiert."); + onClose(); // Schließen des Modals und Aktualisieren der Ansicht + } else { + const errorResponse = await response.json(); + throw new Error( + errorResponse.error || "Fehler beim Aktualisieren des POI." + ); + } + } catch (error) { + console.error("Fehler beim Aktualisieren des POI:", error); + alert("Fehler beim Aktualisieren des POI."); + } }; return ( diff --git a/pages/api/updatePoi.js b/pages/api/updatePoi.js index a79bbaf01..a94fce1c9 100644 --- a/pages/api/updatePoi.js +++ b/pages/api/updatePoi.js @@ -1,6 +1,5 @@ -// pages/api/updateLocation.js +// pages/api/updatePoi.js import mysql from "mysql"; -import util from "util"; const dbConfig = { host: process.env.DB_HOST, @@ -8,32 +7,43 @@ const dbConfig = { password: process.env.DB_PASSWORD, database: process.env.DB_NAME, port: process.env.DB_PORT, - charset: "utf8mb4", }; -export default async function handler(req, res) { +const connection = mysql.createConnection(dbConfig); +connection.connect((err) => { + if (err) { + console.error("Fehler beim Verbinden:", err.stack); + return; + } + console.log("Verbunden als ID", connection.threadId); +}); + +export default function handler(req, res) { if (req.method !== "POST") { - res.setHeader("Allow", ["POST"]); - return res.status(405).end(`Method ${req.method} Not Allowed`); + return res.status(405).json({ error: "Nur POST Methode erlaubt" }); } - const { id, latitude, longitude } = req.body; + const { idPoi, description, idPoiTyp } = req.body; // Stellen Sie sicher, dass die Felder korrekt benannt sind - const connection = mysql.createConnection(dbConfig); - // Promisify the query method - const query = util.promisify(connection.query).bind(connection); + console.log("Empfangene Daten:", req.body); // Loggen der empfangenen Daten zur Überprüfung - try { - await query("UPDATE poi SET position = POINT(?, ?) WHERE idPoi = ?", [ - longitude, - latitude, - id, - ]); - res.status(200).json({ success: true }); - } catch (error) { - console.error(error); - res.status(500).json({ error: "Ein Fehler ist aufgetreten" }); - } finally { - connection.end(); + if (!idPoi) { + return res.status(400).json({ error: "POI ID ist erforderlich" }); } + + const query = + "UPDATE talas_v5.poi SET description = ?, idPoiTyp = ? WHERE idPoi = ?"; + connection.query(query, [description, idPoiTyp, idPoi], (error, results) => { + if (error) { + console.error("Fehler beim Aktualisieren des POI:", error); + return res + .status(500) + .json({ error: "Fehler beim Aktualisieren des POI" }); + } + if (results.affectedRows > 0) { + res.json({ message: "POI erfolgreich aktualisiert" }); + } else { + res.status(404).json({ error: "POI nicht gefunden" }); + } + }); }