Skip to content

Commit

Permalink
fixing error of update tenant information section
Browse files Browse the repository at this point in the history
  • Loading branch information
Norseolee committed Dec 22, 2023
1 parent dade2bb commit 13ba65d
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 50 deletions.
52 changes: 14 additions & 38 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ app.use(express.static("public")); // this to serve our public folder
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

// mySQL;
const pool = mysql.createPool({
host: "localhost",
user: "root",
password: "",
database: "Am9Commercial",
connectionLimit: 20,
});
//mySQL;
// const pool = mysql.createPool({
// host: "b18rvltkeprxyrwdxj35-mysql.services.clever-cloud.com",
// user: "u1llarmjzoo3t683",
// password: "HKDuDLkfJO1cZQJX58M7",
// database: "b18rvltkeprxyrwdxj35",
// host: "localhost",
// user: "root",
// password: "",
// database: "Am9Commercial",
// connectionLimit: 20,
// });
const pool = mysql.createPool({
host: "b18rvltkeprxyrwdxj35-mysql.services.clever-cloud.com",
user: "u1llarmjzoo3t683",
password: "HKDuDLkfJO1cZQJX58M7",
database: "b18rvltkeprxyrwdxj35",
connectionLimit: 20,
});

// get all the tenant_data
app.get("/tenant", (req, res) => {
Expand Down Expand Up @@ -263,11 +263,6 @@ app.put("/tenant/update-tenant/:tenant_id", (req, res) => {
building: req.body["building"],
};

const meralcoParams = {
date_of_reading: req.body["date_of_reading"],
current_reading: req.body["current_reading"],
};

// Update tenant record
connection.query(
"UPDATE tenants SET name = ?, building = ? WHERE tenant_id = ?",
Expand All @@ -279,27 +274,8 @@ app.put("/tenant/update-tenant/:tenant_id", (req, res) => {
return res.status(500).send("Error updating tenant");
}

// Update Meralco record for the same tenant
connection.query(
"UPDATE Meralco SET date_of_reading = ?, current_reading = ? WHERE tenant_id = ?",
[
meralcoParams.date_of_reading,
meralcoParams.current_reading,
tenantId,
],
(meralcoErr, meralcoResult) => {
if (meralcoErr) {
return connection.rollback(() => {
console.error("Error updating Meralco:", meralcoErr);
connection.release();
res.status(500).send("Error updating Meralco");
});
}

connection.release();
res.send(`Tenant with ID ${tenantId} has been updated`);
}
);
connection.release();
res.send(`Tenant with ID ${tenantId} has been updated`);
}
);
});
Expand Down
26 changes: 16 additions & 10 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ <h1 style="text-align: center">ADD NEW TENANT</h1>
<div class="update-content-tenant">
<h1 style="text-align: center">UPDATE READING</h1>
<div class="body-history">
<p>Name: <span id="updateNameTenant"></span></p>
<p>Building: <span id="updateBuildingTenant"></span></p>
<p>Name: <span id="meralcoNameTenant"></span></p>
<p>Building: <span id="meralcoBuildingTenant"></span></p>
</div>
<form action="/meralco/add-meralco" method="post" id="addMeralcoForm">
<div class="update-Due-kwh">
Expand Down Expand Up @@ -216,16 +216,22 @@ <h1 style="text-align: center">UPDATE READING</h1>

<div class="update-section">
<div class="update-content">
<h1 style="text-align: center">UPDATE READING</h1>
<h1 style="text-align: center">UPDATE TENANT INFORMATION</h1>
<form id="updateTenantForm">
<div class="update-Information">
<label for="updateName">Name: </label>
<input type="text" name="updateName" id="updateNameTenant" />

<div class="update-Information">
<label for="updateName">Name: </label>
<input type="text" name="updateName" id="updateNameTenant" />
<label for="updateBuilding">Building and Stall: </label>
<input
type="text"
name="updateBuilding"
id="updateBuildingTenant" />

<label for="updateBuilding">Building and Stall: </label>
<input type="text" name="updateBuilding" id="updateBuildingTenant" />
</div>
<button type="button" id="updateBtn" class="button">Update</button>
<input type="hidden" id="updateTenantId" />
</div>
<button type="submit" id="updateBtn" class="button">Update</button>
</form>
</div>
</div>

Expand Down
44 changes: 42 additions & 2 deletions public/javascript/index-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,16 @@ document.addEventListener("DOMContentLoaded", function () {
document.querySelector(".History-Builing").innerHTML =
selectedObject.building;

document.getElementById("updateNameTenant").innerHTML =
// show in meralco Form
document.getElementById("meralcoNameTenant").innerHTML =
selectedObject.name;
document.getElementById("updateBuildingTenant").innerHTML =
document.getElementById("meralcoBuildingTenant").innerHTML =
selectedObject.building;
document.getElementById("updateTenantId").value =
selectedObject.tenant_id;
// show in Update Tenant Form
document.getElementById("updateNameTenant").value = selectedObject.name;
document.getElementById("updateBuildingTenant").value =
selectedObject.building;
document.getElementById("updateTenantId").value =
selectedObject.tenant_id;
Expand Down Expand Up @@ -200,6 +207,8 @@ document.addEventListener("DOMContentLoaded", function () {
window.deleteMeralco = async function deleteMeralco() {
meralco_id;

console.log(meralco_id);

if (!meralco_id) {
alert("Please select a date to delete.");
return;
Expand Down Expand Up @@ -227,4 +236,35 @@ document.addEventListener("DOMContentLoaded", function () {
}
}
};

//
const updateTenantForm = document.getElementById("updateTenantForm");
updateTenantForm.addEventListener("submit", async (event) => {
let tenant_Id = document.getElementById("updateTenantId").value;
event.preventDefault();

const tenantId = tenant_Id;
const name = document.getElementById("updateNameTenant").value;
const building = document.getElementById("updateBuildingTenant").value;

try {
const response = await fetch(`/tenant/update-tenant/${tenantId}`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ name, building }),
});

if (!response.ok) {
throw new Error(`Error updating tenant: ${response.statusText}`);
}

const result = await response.text();
alert(result);
} catch (error) {
console.error("Error:", error);
// Handle error (e.g., show an error message to the user)
}
});
});

0 comments on commit 13ba65d

Please sign in to comment.