resman/backend/controllers/ipController.js
2026-03-28 15:04:30 +01:00

59 lines
1.1 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;
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:"DB error"});
}
};