48 lines
1.3 KiB
JavaScript
48 lines
1.3 KiB
JavaScript
const express = require("express");
|
|
const router = express.Router();
|
|
const db = require("../../../database/config");
|
|
|
|
const normalizeType = (type) => {
|
|
if (!type) {
|
|
return null;
|
|
}
|
|
|
|
const normalized = String(type).trim().toLowerCase();
|
|
if (normalized === "petang") {
|
|
return "sore";
|
|
}
|
|
|
|
return normalized;
|
|
};
|
|
|
|
router.get("/", async (req, res) => {
|
|
try {
|
|
const type = normalizeType(req.query.type);
|
|
if (type != null) {
|
|
db.all("SELECT * FROM dzikir WHERE type = ? ORDER BY rowid", [type], (err, data) => {
|
|
if (err) {
|
|
res.status(500).json({ status: 500, message: err.message });
|
|
} else if (!data) {
|
|
res.status(404).json({ status: 404, data: [] });
|
|
} else {
|
|
res.status(200).json({ status: 200, data: data });
|
|
}
|
|
});
|
|
} else {
|
|
db.all("SELECT * FROM dzikir ORDER BY rowid", (err, data) => {
|
|
if (err) {
|
|
res.status(500).json({ status: 500, message: err.message });
|
|
} else if (!data) {
|
|
res.status(404).json({ status: 404, data: [] });
|
|
} else {
|
|
res.status(200).json({ status: 200, data: data });
|
|
}
|
|
});
|
|
}
|
|
} catch (error) {
|
|
res.status(500).json({ status: 500, message: error.message });
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|