61 lines
1.8 KiB
JavaScript
61 lines
1.8 KiB
JavaScript
|
|
const express = require("express");
|
||
|
|
const router = express.Router();
|
||
|
|
const db = require("../models");
|
||
|
|
const { Op } = require("sequelize");
|
||
|
|
|
||
|
|
router.get("/", async (req, res) => {
|
||
|
|
try {
|
||
|
|
const rules = await db.rules.findAll();
|
||
|
|
res.json({ success: true, data: rules });
|
||
|
|
} catch (err) {
|
||
|
|
res.status(500).json({ success: false, error: err.message });
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
router.get("/:id", async (req, res) => {
|
||
|
|
try {
|
||
|
|
const rule = await db.rules.findByPk(req.params.id);
|
||
|
|
if (!rule)
|
||
|
|
return res.status(404).json({ success: false, error: "Rule not found" });
|
||
|
|
res.json({ success: true, data: rule });
|
||
|
|
} catch (err) {
|
||
|
|
res.status(500).json({ success: false, error: err.message });
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
router.post("/:id", async (req, res) => {
|
||
|
|
try {
|
||
|
|
const rule = await db.rules.create(req.body);
|
||
|
|
res.status(201).json({ success: true, data: rule });
|
||
|
|
} catch (err) {
|
||
|
|
res.status(400).json({ success: false, error: err.message });
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
router.put("/:id", async (req, res) => {
|
||
|
|
try {
|
||
|
|
const [updated] = await db.rules.update(req.body, {
|
||
|
|
where: { id: req.params.id },
|
||
|
|
});
|
||
|
|
if (!updated)
|
||
|
|
return res.status(404).json({ success: false, error: "Rule not found" });
|
||
|
|
const rule = await db.rules.findByPk(req.params.id);
|
||
|
|
res.json({ success: true, data: rule });
|
||
|
|
} catch (err) {
|
||
|
|
res.status(400).json({ success: false, error: err.message });
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
router.delete("/:id", async (req, res) => {
|
||
|
|
try {
|
||
|
|
const deleted = await db.rules.destroy({ where: { id: req.params.id } });
|
||
|
|
if (!deleted)
|
||
|
|
return res.status(404).json({ success: false, error: "Rule not found" });
|
||
|
|
res.json({ success: true, data: { message: "Rule deleted" } });
|
||
|
|
} catch (err) {
|
||
|
|
res.status(500).json({ success: false, error: err.message });
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
module.exports = router;
|