refactor: Move API-Endpoint-Functions to /api/talas_v5_DB/ folder for better organization
This commit is contained in:
38
pages/api/talas_v5_DB/pois/addLocation.js
Normal file
38
pages/api/talas_v5_DB/pois/addLocation.js
Normal file
@@ -0,0 +1,38 @@
|
||||
// pages/api/talas_v5_DB/pois/addLocation.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,
|
||||
};
|
||||
|
||||
export default function handler(req, res) {
|
||||
if (req.method === "POST") {
|
||||
const { name, poiTypeId, latitude, longitude, idLD } = req.body;
|
||||
console.log("Received data:", req.body); // Überprüfen der empfangenen Daten
|
||||
const connection = mysql.createConnection(dbConfig);
|
||||
|
||||
const query =
|
||||
"INSERT INTO poi (description, idPoiTyp, position, idLD) VALUES (?, ?, ST_GeomFromText(?),?)";
|
||||
const point = `POINT(${longitude} ${latitude})`;
|
||||
const values = [name, poiTypeId, point, idLD]; // Stellen Sie sicher, dass poiTypeId korrekt ist
|
||||
|
||||
connection.query(query, values, (error, results) => {
|
||||
connection.end();
|
||||
if (error) {
|
||||
console.error("Fehler beim Einfügen des Standorts:", error);
|
||||
return res.status(500).json({ error: "Ein Fehler ist aufgetreten" });
|
||||
}
|
||||
res.status(200).json({
|
||||
id: results.insertId,
|
||||
message: "Standort erfolgreich hinzugefügt",
|
||||
});
|
||||
});
|
||||
} else {
|
||||
res.setHeader("Allow", ["POST"]);
|
||||
res.status(405).end(`Method ${req.method} Not Allowed`);
|
||||
}
|
||||
}
|
||||
45
pages/api/talas_v5_DB/pois/deletePoi.js
Normal file
45
pages/api/talas_v5_DB/pois/deletePoi.js
Normal file
@@ -0,0 +1,45 @@
|
||||
// pages/api/talas_v5_DB/pois/deletePoi.js
|
||||
import mysql from "mysql";
|
||||
|
||||
// Datenbankkonfiguration
|
||||
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;
|
||||
}
|
||||
console.log("Verbunden als ID", connection.threadId);
|
||||
});
|
||||
|
||||
export default function handler(req, res) {
|
||||
if (req.method !== "DELETE") {
|
||||
return res.status(405).json({ error: "Nur DELETE Methode erlaubt" });
|
||||
}
|
||||
|
||||
const { id } = req.query; // ID aus der Anfrage holen
|
||||
|
||||
if (!id) {
|
||||
return res.status(400).json({ error: "POI ID ist erforderlich" });
|
||||
}
|
||||
|
||||
const query = "DELETE FROM poi WHERE idPoi = ?";
|
||||
connection.query(query, [id], (error, results) => {
|
||||
if (error) {
|
||||
console.error("Fehler beim Löschen des POI:", error);
|
||||
return res.status(500).json({ error: "Fehler beim Löschen des POI" });
|
||||
}
|
||||
if (results.affectedRows > 0) {
|
||||
res.json({ message: "POI erfolgreich gelöscht" });
|
||||
} else {
|
||||
res.status(404).json({ error: "POI nicht gefunden" });
|
||||
}
|
||||
});
|
||||
}
|
||||
42
pages/api/talas_v5_DB/pois/getPoiById.js
Normal file
42
pages/api/talas_v5_DB/pois/getPoiById.js
Normal file
@@ -0,0 +1,42 @@
|
||||
// pages/api/talas_v5_DB/pois/getPoiById.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,
|
||||
};
|
||||
|
||||
export default function handler(req, res) {
|
||||
if (req.method === "GET") {
|
||||
const { idPoi } = req.query;
|
||||
const connection = mysql.createConnection(dbConfig);
|
||||
|
||||
connection.connect((err) => {
|
||||
if (err) {
|
||||
console.error("Fehler beim Verbinden:", err.stack);
|
||||
return res
|
||||
.status(500)
|
||||
.json({ error: "Verbindungsfehler zur Datenbank" });
|
||||
}
|
||||
|
||||
const query = "SELECT description FROM poi WHERE idPoi = ?";
|
||||
connection.query(query, [idPoi], (error, results) => {
|
||||
connection.end();
|
||||
if (error) {
|
||||
console.error("Fehler bei der Abfrage:", error);
|
||||
return res.status(500).json({ error: "Fehler bei der Abfrage" });
|
||||
}
|
||||
if (results.length === 0) {
|
||||
return res.status(404).json({ error: "POI nicht gefunden" });
|
||||
}
|
||||
res.status(200).json(results[0]);
|
||||
});
|
||||
});
|
||||
} else {
|
||||
res.setHeader("Allow", ["GET"]);
|
||||
res.status(405).end(`Method ${req.method} Not Allowed`);
|
||||
}
|
||||
}
|
||||
36
pages/api/talas_v5_DB/pois/poi-icons.js
Normal file
36
pages/api/talas_v5_DB/pois/poi-icons.js
Normal file
@@ -0,0 +1,36 @@
|
||||
// pages/api/talas_v5_DB/pois/poi-icons.js
|
||||
import mysql from "mysql";
|
||||
|
||||
const dbConfig = {
|
||||
//host: "10.10.0.13",
|
||||
//host: "localhost",
|
||||
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 query = `SELECT p.idPoi, i.path
|
||||
FROM poi p
|
||||
JOIN poiTyp pt ON p.idPoiTyp = pt.idPoiTyp
|
||||
JOIN poiicons i ON pt.icon = i.idpoiicons;`;
|
||||
connection.query(query, (error, results) => {
|
||||
if (error) {
|
||||
console.error("Fehler beim Abrufen der icons:", error);
|
||||
return res.status(500).json({ error: "Fehler beim Abrufen der icons" });
|
||||
}
|
||||
res.json(results);
|
||||
});
|
||||
}
|
||||
45
pages/api/talas_v5_DB/pois/readLocations.js
Normal file
45
pages/api/talas_v5_DB/pois/readLocations.js
Normal file
@@ -0,0 +1,45 @@
|
||||
// pages/api/talas_v5_DB/pois/readLocations.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,
|
||||
};
|
||||
console.log("my dbconfig: ", dbConfig);
|
||||
export default function handler(req, res) {
|
||||
const connection = mysql.createConnection(dbConfig);
|
||||
|
||||
connection.connect((err) => {
|
||||
if (err) {
|
||||
console.error("Fehler beim Verbinden:", err.stack);
|
||||
res.status(500).json({ error: "Verbindungsfehler zur Datenbank" });
|
||||
return;
|
||||
}
|
||||
|
||||
console.log("Verbunden als ID", connection.threadId);
|
||||
|
||||
connection.query(
|
||||
"SELECT idPoi, description, idPoiTyp, idLD, ST_AsText(position) AS position FROM poi",
|
||||
(error, results) => {
|
||||
if (error) {
|
||||
console.error("Fehler beim Abrufen der API", error);
|
||||
res.status(500).json({ error: "Fehler bei der Abfrage" });
|
||||
return;
|
||||
}
|
||||
|
||||
// Wichtig: Senden Sie die Antwort zurück
|
||||
res.status(200).json(results);
|
||||
console.log(
|
||||
"--------------- location.js ---------------",
|
||||
"results in location.js : ",
|
||||
results,
|
||||
"---------------------- location.js end ---------------------------"
|
||||
);
|
||||
connection.end();
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
39
pages/api/talas_v5_DB/pois/updateLocation.js
Normal file
39
pages/api/talas_v5_DB/pois/updateLocation.js
Normal file
@@ -0,0 +1,39 @@
|
||||
// pages/api/talas_v5_DB/pois/updateLocation.js
|
||||
import mysql from "mysql";
|
||||
import util from "util";
|
||||
|
||||
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,
|
||||
charset: "utf8mb4",
|
||||
};
|
||||
|
||||
export default async function handler(req, res) {
|
||||
if (req.method !== "POST") {
|
||||
res.setHeader("Allow", ["POST"]);
|
||||
return res.status(405).end(`Method ${req.method} Not Allowed`);
|
||||
}
|
||||
|
||||
const { id, latitude, longitude } = req.body;
|
||||
|
||||
const connection = mysql.createConnection(dbConfig);
|
||||
// Promisify the query method
|
||||
const query = util.promisify(connection.query).bind(connection);
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
53
pages/api/talas_v5_DB/pois/updatePoi.js
Normal file
53
pages/api/talas_v5_DB/pois/updatePoi.js
Normal file
@@ -0,0 +1,53 @@
|
||||
// pages/api/talas_v5_DB/pois/updatePoi.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;
|
||||
}
|
||||
console.log("Verbunden als ID", connection.threadId);
|
||||
});
|
||||
|
||||
export default function handler(req, res) {
|
||||
if (req.method !== "POST") {
|
||||
return res.status(405).json({ error: "Nur POST Methode erlaubt" });
|
||||
}
|
||||
|
||||
const { idPoi, description, idPoiTyp, idLD } = req.body; // Stellen Sie sicher, dass die Felder korrekt benannt sind
|
||||
|
||||
console.log("Empfangene Daten:", req.body); // Loggen der empfangenen Daten zur Überprüfung
|
||||
|
||||
if (!idPoi) {
|
||||
return res.status(400).json({ error: "POI ID ist erforderlich" });
|
||||
}
|
||||
|
||||
const query =
|
||||
"UPDATE talas_v5.poi SET description = ?, idPoiTyp = ?, idLD = ? WHERE idPoi = ?";
|
||||
connection.query(
|
||||
query,
|
||||
[description, idPoiTyp, idLD, 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