const express = require("express"); const pool = require("./db"); const cors = require("cors"); const app = express(); app.use(express.json()); app.use(cors()); const BASE_PATH = "/resman/api"; const path = require("path"); /* Static Files */ app.use("/resman", express.static(path.join(__dirname, "public"))); /* Resourcen laden */ app.get("/resman/api/resources", async (req, res) => { const [rows] = await pool.query( "SELECT * FROM resources WHERE status != 'gekündigt'" ); res.json(rows); }); /* LIST Aktive resourcen*/ app.get("/resman/api/resources/active", async (req, res) => { const [rows] = await pool.query( "SELECT * FROM resources WHERE status != 'gekündigt'" ); res.json(rows); }); /* LIST Gekündigte resourcen*/ app.get("/resman/api/resources/cancelled", async (req, res) => { const [rows] = await pool.query( "SELECT * FROM resources WHERE status = 'gekündigt'" ); console.log("Cancelled resources:", rows); res.json(rows); }); /* INSERT */ app.post(`${BASE_PATH}/resources`, async (req, res) => { const data = req.body; await pool.query("INSERT INTO resources SET ?", data); res.json({ message: "Inserted" }); }); /* UPDATE */ app.put(`${BASE_PATH}/resources/:id`, async (req, res) => { await pool.query("UPDATE resources SET ? WHERE id = ?", [ req.body, req.params.id ]); res.json({ message: "Updated" }); }); /* DELETE */ app.delete(`${BASE_PATH}/resources/:id`, async (req, res) => { await pool.query("DELETE FROM resources WHERE id = ?", [ req.params.id ]); res.json({ message: "Deleted" }); }); /* COPY */ app.post(`${BASE_PATH}/resources/:id/copy`, async (req, res) => { await pool.query(` INSERT INTO resources ( name, produkt, provider, art, kosten_monat, kosten_jahr, laufzeit_monate, bemerkung, cpu, ram, disk, os, ipv4_adresse, vlan_ip1, vlan_ip2, ipv6_net, ipv6_adresse, providername, bestelldatum, kuendbar_ab ) SELECT CONCAT(name, ' (Copy)'), produkt, provider, art, kosten_monat, kosten_jahr, laufzeit_monate, bemerkung, cpu, ram, disk, os, ipv4_adresse, vlan_ip1, vlan_ip2, ipv6_net, ipv6_adresse, providername, bestelldatum, kuendbar_ab FROM resources WHERE id = ? `, [req.params.id]); res.json({ message: "Copied" }); }); app.listen(3000, () => console.log("ResMan running on port 3000"));