resman/backend/controllers/ipController.js
2026-04-05 18:15:53 +02:00

63 lines
1.2 KiB
JavaScript

const pool = require("../db");
const db = require("../db");
/* LIST IPs for resource */
exports.getByResource = async (req, res) => {
const [rows] = await pool.query(
"SELECT * FROM resource_ips WHERE resource_id = ?",
[req.params.id]
);
res.json(rows);
};
/* ADD IP */
exports.create = async (req, res) => {
const { ip, type, comment } = req.body;
await pool.query(
"INSERT INTO resource_ips (resource_id, ip, type, comment) VALUES (?, ?, ?, ?)",
[req.params.id, ip, type, comment]
);
res.json({ message: "IP added" });
};
/* DELETE IP */
exports.remove = async (req, res) => {
await pool.query(
"DELETE FROM resource_ips WHERE id = ?",
[req.params.id]
);
res.json({ message: "IP deleted" });
};
exports.update = async (req, res) => {
try {
const { ip, type, comment } = req.body
if (!ip) {
return res.status(400).json({ error: "IP darf nicht leer sein" })
}
await db.query(
"UPDATE resource_ips SET ip=?, type=?, comment=? WHERE id=?",
[ip, type, comment, req.params.id]
)
res.json({ success: true })
} catch (e) {
console.error("UPDATE IP error:", e)
res.status(500).json({
error: "IP konnte nicht gespeichert werden"
})
}
}