remove kecamatan and kelurahan table, using alamat where the lampu merah located, added the photos on /assets/image/<id> on each alamat, update server.js, add new list.html
This commit is contained in:
119
api/server.js
119
api/server.js
@ -124,23 +124,10 @@ app.get("/sensordata/:id", (req, res) => {
|
||||
air_data.bz,
|
||||
air_data.aq,
|
||||
air_data.updated_at,
|
||||
|
||||
sensor_data.alamat,
|
||||
|
||||
kelurahan.nama AS kelurahan,
|
||||
kecamatan.nama AS kecamatan
|
||||
|
||||
alamat_data.alamat AS alamat
|
||||
FROM air_data
|
||||
|
||||
JOIN sensor_data
|
||||
ON air_data.id = sensor_data.id
|
||||
|
||||
JOIN kelurahan
|
||||
ON sensor_data.kelurahan = kelurahan.id
|
||||
|
||||
JOIN kecamatan
|
||||
ON kelurahan.kecamatan_id = kecamatan.id
|
||||
|
||||
JOIN alamat_data
|
||||
ON alamat_data.id = air_data.id
|
||||
WHERE air_data.id = ?
|
||||
ORDER BY air_data.updated_at DESC
|
||||
LIMIT 1
|
||||
@ -163,7 +150,7 @@ app.get("/sensordata/:id", (req, res) => {
|
||||
});
|
||||
|
||||
app.get("/sensors", (req, res) => {
|
||||
const sql = `SELECT id FROM sensor_data ORDER BY id ASC`;
|
||||
const sql = `SELECT * FROM alamat_data where status = 1 ORDER BY id ASC`;
|
||||
|
||||
db.query(sql, (err, results) => {
|
||||
if (err) {
|
||||
@ -178,56 +165,52 @@ app.get("/sensors", (req, res) => {
|
||||
// ADD SENSOR
|
||||
// ============================
|
||||
app.post("/addSensor", (req, res) => {
|
||||
const { kelurahan, alamat } = req.body;
|
||||
const { id } = req.body;
|
||||
|
||||
// ============================
|
||||
// VALIDATION
|
||||
// ============================
|
||||
if (!kelurahan || !alamat) {
|
||||
// check if id is provided
|
||||
if (!id) {
|
||||
return res.status(400).json({
|
||||
error: "kelurahan dan alamat wajib diisi"
|
||||
error: "Sensor ID is required"
|
||||
});
|
||||
}
|
||||
|
||||
// ============================
|
||||
// VALIDATE KELURAHAN EXISTS
|
||||
// ============================
|
||||
const checkSql = `SELECT id FROM kelurahan WHERE id = ?`;
|
||||
// check if id is numeric
|
||||
if (isNaN(id)) {
|
||||
return res.status(400).json({
|
||||
error: "Sensor ID must be numeric"
|
||||
});
|
||||
}
|
||||
|
||||
db.query(checkSql, [kelurahan], (err, rows) => {
|
||||
// check if id exists in alamat_data table, if available then continue
|
||||
const checkSql = `SELECT id FROM alamat_data WHERE id = ?`;
|
||||
|
||||
db.query(checkSql, [id], (err, results) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return res.status(500).json({ error: "DB error" });
|
||||
}
|
||||
|
||||
if (rows.length === 0) {
|
||||
if (results.length === 0) {
|
||||
return res.status(400).json({
|
||||
error: "Kelurahan tidak valid"
|
||||
error: "Sensor ID not found in alamat_data table"
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// update the status field to 1 to the id in alamat_data table
|
||||
const updateSql = `UPDATE alamat_data SET status = 1 WHERE id = ?`;
|
||||
|
||||
// ============================
|
||||
// INSERT SENSOR
|
||||
// ============================
|
||||
const insertSql = `
|
||||
INSERT INTO sensor_data (kelurahan, alamat)
|
||||
VALUES (?, ?)
|
||||
`;
|
||||
|
||||
db.query(insertSql, [kelurahan, alamat], (err, result) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return res.status(500).json({
|
||||
error: "DB error saat insert"
|
||||
});
|
||||
}
|
||||
|
||||
res.json({
|
||||
message: "Sensor berhasil ditambahkan",
|
||||
sensor_id: result.insertId
|
||||
});
|
||||
db.query(updateSql, [id], (err, result) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return res.status(500).json({ error: "DB error" });
|
||||
}
|
||||
res.json({
|
||||
message: "Sensor added successfully",
|
||||
id: id
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
// ============================
|
||||
@ -252,8 +235,8 @@ app.get("/history", (req, res) => {
|
||||
|
||||
|
||||
// get kecamatan
|
||||
app.get("/kecamatan", (req, res) => {
|
||||
const sql = `SELECT id, nama FROM kecamatan ORDER BY nama ASC`;
|
||||
app.get("/alamat", (req, res) => {
|
||||
const sql = `SELECT * from alamat_data where status = 0 ORDER BY id ASC`;
|
||||
|
||||
db.query(sql, (err, results) => {
|
||||
if (err) {
|
||||
@ -266,38 +249,6 @@ app.get("/kecamatan", (req, res) => {
|
||||
});
|
||||
|
||||
|
||||
// get kelurahan by kecamatan_id
|
||||
app.get("/kelurahan", (req, res) => {
|
||||
const { kecamatan_id } = req.query;
|
||||
|
||||
let sql = `
|
||||
SELECT
|
||||
kelurahan.id,
|
||||
kelurahan.nama,
|
||||
kecamatan.nama AS kecamatan
|
||||
FROM kelurahan
|
||||
JOIN kecamatan ON kelurahan.kecamatan_id = kecamatan.id
|
||||
`;
|
||||
|
||||
let params = [];
|
||||
|
||||
if (kecamatan_id) {
|
||||
sql += ` WHERE kelurahan.kecamatan_id = ?`;
|
||||
params.push(kecamatan_id);
|
||||
}
|
||||
|
||||
sql += ` ORDER BY kelurahan.nama ASC`;
|
||||
|
||||
db.query(sql, params, (err, results) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return res.status(500).json({ error: "DB error" });
|
||||
}
|
||||
|
||||
res.json(results);
|
||||
});
|
||||
});
|
||||
|
||||
// ============================
|
||||
// START SERVER
|
||||
// ============================
|
||||
|
||||
Reference in New Issue
Block a user