const express = require("express") const router = express.Router() const db = require("../db") /* alle Subdomains laden */ router.get("/", async (req,res)=>{ try{ const [rows] = await db.query(` SELECT s.id, s.subdomain, s.ip_address, d.domain_name, s.domain_id FROM subdomains s JOIN domains d ON s.domain_id=d.id `) res.json(rows) }catch(e){ console.error("SUBDOMAIN LIST error:",e) res.status(500).json({error:"DB error"}) } }) /* Subdomains einer Domain */ router.get("/domain/:id", async (req,res)=>{ try{ const [rows] = await db.query( "SELECT * FROM subdomains WHERE domain_id=?", [req.params.id] ) res.json(rows) }catch(e){ console.error("SUBDOMAIN DOMAIN error:",e) res.status(500).json({error:"DB error"}) } }) router.delete("/:id", async (req,res)=>{ await db.query("DELETE FROM subdomains WHERE id=?",[req.params.id]) res.json({success:true}) }) router.post("/", async (req,res)=>{ try{ const {domain_id, subdomain, ip_address} = req.body await db.query(` INSERT INTO subdomains (domain_id, subdomain, ip_address) VALUES (?,?,?) `,[domain_id, subdomain, ip_address]) res.json({success:true}) }catch(e){ console.error("CREATE subdomain error:",e) res.status(500).json({error:"DB error"}) } }) router.put("/:id", async (req,res)=>{ try{ const {subdomain, ip_address} = req.body await db.query(` UPDATE subdomains SET subdomain=?, ip_address=? WHERE id=? `,[subdomain, ip_address, req.params.id]) res.json({success:true}) }catch(e){ console.error("UPDATE subdomain error:",e) res.status(500).json({error:"DB error"}) } }) module.exports = router