refactor: split routes and controllers

This commit is contained in:
root
2026-03-05 17:38:35 +01:00
parent 264e7e3ba9
commit 6c974d01e8
3 changed files with 61 additions and 92 deletions
+36
View File
@@ -0,0 +1,36 @@
const pool = require("../db");
exports.getActive = async (req, res) => {
const [rows] = await pool.query(
"SELECT * FROM resources WHERE status != 'gekündigt'"
);
res.json(rows);
};
exports.getCancelled = async (req, res) => {
const [rows] = await pool.query(
"SELECT * FROM resources WHERE status = 'gekündigt'"
);
res.json(rows);
};
exports.create = async (req, res) => {
await pool.query("INSERT INTO resources SET ?", req.body);
res.json({ message: "Inserted" });
};
exports.update = async (req, res) => {
await pool.query(
"UPDATE resources SET ? WHERE id = ?",
[req.body, req.params.id]
);
res.json({ message: "Updated" });
};
exports.remove = async (req, res) => {
await pool.query(
"DELETE FROM resources WHERE id = ?",
[req.params.id]
);
res.json({ message: "Deleted" });
};
+12
View File
@@ -0,0 +1,12 @@
const express = require("express");
const router = express.Router();
const controller = require("../controllers/resourceController");
router.get("/active", controller.getActive);
router.get("/cancelled", controller.getCancelled);
router.post("/", controller.create);
router.put("/:id", controller.update);
router.delete("/:id", controller.remove);
module.exports = router;
+13 -92
View File
@@ -1,99 +1,20 @@
const express = require("express"); const express = require("express");
const pool = require("./db");
const cors = require("cors"); const cors = require("cors");
const app = express();
app.use(express.json());
app.use(cors());
const BASE_PATH = "/resman/api";
const path = require("path"); const path = require("path");
/* Static Files */ const resourceRoutes = require("./routes/resources");
const app = express();
app.use(cors());
app.use(express.json());
/* static frontend */
app.use("/resman", express.static(path.join(__dirname, "public"))); app.use("/resman", express.static(path.join(__dirname, "public")));
/* Resourcen laden */ /* API routes */
app.get("/resman/api/resources", async (req, res) => { app.use("/resman/api/resources", resourceRoutes);
const [rows] = await pool.query(
"SELECT * FROM resources WHERE status != 'gekündigt'" app.listen(3000, () => {
); console.log("ResMan running on port 3000");
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"));