Poi update, aber idLD muss noch gemacht werden
This commit is contained in:
@@ -68,8 +68,33 @@ const PoiUpdateModal = ({ onClose, poiData }) => {
|
|||||||
|
|
||||||
// Form submission handler
|
// Form submission handler
|
||||||
const handleSubmit = async (event) => {
|
const handleSubmit = async (event) => {
|
||||||
event.preventDefault(); // Prevent the form from submitting
|
event.preventDefault();
|
||||||
// Perform update operation here
|
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 (
|
return (
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
// pages/api/updateLocation.js
|
// pages/api/updatePoi.js
|
||||||
import mysql from "mysql";
|
import mysql from "mysql";
|
||||||
import util from "util";
|
|
||||||
|
|
||||||
const dbConfig = {
|
const dbConfig = {
|
||||||
host: process.env.DB_HOST,
|
host: process.env.DB_HOST,
|
||||||
@@ -8,32 +7,43 @@ const dbConfig = {
|
|||||||
password: process.env.DB_PASSWORD,
|
password: process.env.DB_PASSWORD,
|
||||||
database: process.env.DB_NAME,
|
database: process.env.DB_NAME,
|
||||||
port: process.env.DB_PORT,
|
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") {
|
if (req.method !== "POST") {
|
||||||
res.setHeader("Allow", ["POST"]);
|
return res.status(405).json({ error: "Nur POST Methode erlaubt" });
|
||||||
return res.status(405).end(`Method ${req.method} Not Allowed`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
console.log("Empfangene Daten:", req.body); // Loggen der empfangenen Daten zur Überprüfung
|
||||||
// Promisify the query method
|
|
||||||
const query = util.promisify(connection.query).bind(connection);
|
|
||||||
|
|
||||||
try {
|
if (!idPoi) {
|
||||||
await query("UPDATE poi SET position = POINT(?, ?) WHERE idPoi = ?", [
|
return res.status(400).json({ error: "POI ID ist erforderlich" });
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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" });
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user