tambah fitur add tempat ibadah
This commit is contained in:
@@ -1,6 +1,59 @@
|
||||
const express = require("express");
|
||||
const db = require("../db");
|
||||
|
||||
const TEMPAT_IBADAH_TYPES = ["Gereja Katolik", "GPIB", "GKI", "HKBP", "Pentakosta"];
|
||||
|
||||
const createTempatIbadah = `
|
||||
CREATE TABLE IF NOT EXISTS tempat_ibadah (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
nama VARCHAR(150) NOT NULL,
|
||||
jenis ENUM('Gereja Katolik','GPIB','GKI','HKBP','Pentakosta') NOT NULL,
|
||||
alamat TEXT NOT NULL,
|
||||
kelurahan VARCHAR(100) NOT NULL,
|
||||
lat DOUBLE NOT NULL,
|
||||
lng DOUBLE NOT NULL,
|
||||
aktif TINYINT(1) NOT NULL DEFAULT 1,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
`;
|
||||
|
||||
async function initTempatIbadahTable() {
|
||||
await db.execute(createTempatIbadah);
|
||||
const columns = await tableColumns("tempat_ibadah");
|
||||
if (!columns.includes("aktif")) {
|
||||
await db.execute("ALTER TABLE tempat_ibadah ADD COLUMN aktif TINYINT(1) NOT NULL DEFAULT 1");
|
||||
}
|
||||
if (!columns.includes("created_at")) {
|
||||
await db.execute("ALTER TABLE tempat_ibadah ADD COLUMN created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP");
|
||||
}
|
||||
await db.execute(`
|
||||
UPDATE tempat_ibadah
|
||||
SET jenis = 'Gereja Katolik'
|
||||
WHERE jenis IS NULL
|
||||
OR jenis = ''
|
||||
OR jenis NOT IN ('Gereja Katolik','GPIB','GKI','HKBP','Pentakosta')
|
||||
`);
|
||||
await db.execute(`
|
||||
ALTER TABLE tempat_ibadah
|
||||
MODIFY jenis ENUM('Gereja Katolik','GPIB','GKI','HKBP','Pentakosta') NOT NULL
|
||||
`);
|
||||
await db.execute(`
|
||||
UPDATE tempat_ibadah
|
||||
SET jenis = 'Gereja Katolik'
|
||||
WHERE jenis IS NULL
|
||||
OR jenis = ''
|
||||
OR jenis NOT IN ('Gereja Katolik','GPIB','GKI','HKBP','Pentakosta')
|
||||
`);
|
||||
}
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
await initTempatIbadahTable();
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
})();
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
let tablesReady = false;
|
||||
@@ -128,6 +181,8 @@ function buildFeature(item) {
|
||||
async function ensureTables() {
|
||||
if (tablesReady) return;
|
||||
|
||||
await initTempatIbadahTable();
|
||||
|
||||
await db.execute(`
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id_user INT AUTO_INCREMENT PRIMARY KEY,
|
||||
@@ -492,6 +547,22 @@ function validateFamily(body) {
|
||||
return null;
|
||||
}
|
||||
|
||||
function validateIbadah(body) {
|
||||
const required = ["nama", "jenis", "alamat", "kelurahan", "lat", "lng"];
|
||||
for (const key of required) {
|
||||
if (body[key] === undefined || body[key] === null || body[key] === "") {
|
||||
return `${key} wajib diisi.`;
|
||||
}
|
||||
}
|
||||
if (!TEMPAT_IBADAH_TYPES.includes(body.jenis)) {
|
||||
return "Jenis tempat ibadah wajib valid.";
|
||||
}
|
||||
if (!Number.isFinite(Number(body.lat)) || !Number.isFinite(Number(body.lng))) {
|
||||
return "Latitude dan longitude wajib valid.";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
router.use(async (req, res, next) => {
|
||||
try {
|
||||
await ensureTables();
|
||||
@@ -904,4 +975,64 @@ router.delete("/riwayat-bantuan/:id", async (req, res) => {
|
||||
res.json({ success: true, message: "Riwayat bantuan berhasil dihapus." });
|
||||
});
|
||||
|
||||
router.get("/ibadah", async (req, res) => {
|
||||
try {
|
||||
const [rows] = await db.execute("SELECT * FROM tempat_ibadah WHERE aktif = 1 ORDER BY nama ASC");
|
||||
res.json({ success: true, data: rows });
|
||||
} catch (error) {
|
||||
res.status(500).json({ success: false, message: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
router.get("/ibadah/:id", async (req, res) => {
|
||||
try {
|
||||
const [rows] = await db.execute("SELECT * FROM tempat_ibadah WHERE id = ? AND aktif = 1", [req.params.id]);
|
||||
if (!rows.length) return res.status(404).json({ success: false, message: "Tempat ibadah tidak ditemukan." });
|
||||
res.json({ success: true, data: rows[0] });
|
||||
} catch (error) {
|
||||
res.status(500).json({ success: false, message: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
router.post("/ibadah", async (req, res) => {
|
||||
const error = validateIbadah(req.body);
|
||||
if (error) return res.status(400).json({ success: false, message: error });
|
||||
const body = req.body;
|
||||
try {
|
||||
const [result] = await db.execute(
|
||||
"INSERT INTO tempat_ibadah (nama, jenis, alamat, kelurahan, lat, lng) VALUES (?, ?, ?, ?, ?, ?)",
|
||||
[body.nama, body.jenis, body.alamat, body.kelurahan, Number(body.lat), Number(body.lng)],
|
||||
);
|
||||
res.status(201).json({ success: true, id: result.insertId, message: "Tempat ibadah berhasil ditambahkan." });
|
||||
} catch (err) {
|
||||
res.status(500).json({ success: false, message: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
router.put("/ibadah/:id", async (req, res) => {
|
||||
const error = validateIbadah(req.body);
|
||||
if (error) return res.status(400).json({ success: false, message: error });
|
||||
const body = req.body;
|
||||
try {
|
||||
const [result] = await db.execute(
|
||||
"UPDATE tempat_ibadah SET nama=?, jenis=?, alamat=?, kelurahan=?, lat=?, lng=? WHERE id=? AND aktif = 1",
|
||||
[body.nama, body.jenis, body.alamat, body.kelurahan, Number(body.lat), Number(body.lng), req.params.id],
|
||||
);
|
||||
if (!result.affectedRows) return res.status(404).json({ success: false, message: "Tempat ibadah tidak ditemukan." });
|
||||
res.json({ success: true, message: "Tempat ibadah berhasil diperbarui." });
|
||||
} catch (err) {
|
||||
res.status(500).json({ success: false, message: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
router.delete("/ibadah/:id", async (req, res) => {
|
||||
try {
|
||||
const [result] = await db.execute("UPDATE tempat_ibadah SET aktif = 0 WHERE id = ?", [req.params.id]);
|
||||
if (!result.affectedRows) return res.status(404).json({ success: false, message: "Tempat ibadah tidak ditemukan." });
|
||||
res.json({ success: true, message: "Tempat ibadah berhasil dihapus." });
|
||||
} catch (error) {
|
||||
res.status(500).json({ success: false, message: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user