working resman version
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
const express = require('express')
|
||||
const router = express.Router()
|
||||
const db = require('../db')
|
||||
|
||||
router.get('/', async (req, res) => {
|
||||
|
||||
try {
|
||||
|
||||
const [rows] = await db.query(`
|
||||
SELECT
|
||||
d.id AS domain_id,
|
||||
d.domain_name,
|
||||
d.ip_address,
|
||||
r.id AS resource_id,
|
||||
r.name AS server_name
|
||||
FROM domains d
|
||||
LEFT JOIN resource_ips ip ON d.ip_address = ip.ip
|
||||
LEFT JOIN resources r ON ip.resource_id = r.id
|
||||
ORDER BY d.domain_name
|
||||
`)
|
||||
|
||||
res.json(rows)
|
||||
|
||||
} catch(err){
|
||||
|
||||
console.error(err)
|
||||
res.status(500).json({error:"DB error"})
|
||||
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
@@ -0,0 +1,120 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const pool = require('../db');
|
||||
|
||||
|
||||
// GET ALL DOMAINS
|
||||
router.get('/', async (req, res) => {
|
||||
try {
|
||||
const [rows] = await pool.query(`
|
||||
SELECT
|
||||
d.id,
|
||||
d.domain_name,
|
||||
d.provider,
|
||||
d.ip_address,
|
||||
d.yearly_cost,
|
||||
d.notes,
|
||||
r.name AS resource_name
|
||||
FROM domains d
|
||||
LEFT JOIN resource_ips ip ON d.ip_address = ip.ip
|
||||
LEFT JOIN resources r ON ip.resource_id = r.id
|
||||
ORDER BY d.domain_name
|
||||
`);
|
||||
|
||||
res.json(rows);
|
||||
|
||||
} catch (err) {
|
||||
console.error("GET domains error:", err);
|
||||
res.status(500).json({ error: "DB error" });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// GET SINGLE DOMAIN
|
||||
router.get('/:id', async (req, res) => {
|
||||
try {
|
||||
const [rows] = await pool.query(
|
||||
`SELECT * FROM domains WHERE id = ?`,
|
||||
[req.params.id]
|
||||
);
|
||||
|
||||
if (rows.length === 0) {
|
||||
return res.status(404).json({ error: "Domain not found" });
|
||||
}
|
||||
|
||||
res.json(rows[0]);
|
||||
|
||||
} catch (err) {
|
||||
console.error("GET domain error:", err);
|
||||
res.status(500).json({ error: "DB error" });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// CREATE DOMAIN
|
||||
router.post('/', async (req, res) => {
|
||||
try {
|
||||
const { domain_name, provider, ip_address, yearly_cost, notes } = req.body;
|
||||
|
||||
const [result] = await pool.query(
|
||||
`INSERT INTO domains
|
||||
(domain_name, provider, ip_address, yearly_cost, notes)
|
||||
VALUES (?, ?, ?, ?, ?)`,
|
||||
[domain_name, provider, ip_address, yearly_cost, notes]
|
||||
);
|
||||
|
||||
res.json({
|
||||
message: "Domain created",
|
||||
id: result.insertId
|
||||
});
|
||||
|
||||
} catch (err) {
|
||||
console.error("CREATE domain error:", err);
|
||||
res.status(500).json({ error: "DB error" });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// UPDATE DOMAIN
|
||||
router.put('/:id', async (req, res) => {
|
||||
try {
|
||||
const { domain_name, provider, ip_address, yearly_cost, notes } = req.body;
|
||||
|
||||
await pool.query(
|
||||
`UPDATE domains SET
|
||||
domain_name = ?,
|
||||
provider = ?,
|
||||
ip_address = ?,
|
||||
yearly_cost = ?,
|
||||
notes = ?
|
||||
WHERE id = ?`,
|
||||
[domain_name, provider, ip_address, yearly_cost, notes, req.params.id]
|
||||
);
|
||||
|
||||
res.json({ message: "Domain updated" });
|
||||
|
||||
} catch (err) {
|
||||
console.error("UPDATE domain error:", err);
|
||||
res.status(500).json({ error: "DB error" });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// DELETE DOMAIN
|
||||
router.delete('/:id', async (req, res) => {
|
||||
try {
|
||||
await pool.query(
|
||||
`DELETE FROM domains WHERE id = ?`,
|
||||
[req.params.id]
|
||||
);
|
||||
|
||||
res.json({ message: "Domain deleted" });
|
||||
|
||||
} catch (err) {
|
||||
console.error("DELETE domain error:", err);
|
||||
res.status(500).json({ error: "DB error" });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user