From 9baa59004170a4048647d198c846405f5f5a8986 Mon Sep 17 00:00:00 2001 From: LarsLindain Date: Mon, 27 Jan 2025 22:20:32 -0800 Subject: [PATCH 1/5] test --- navigation/logicgatesgame/lgates.md | 69 ++++++++++++++++++++++------- 1 file changed, 54 insertions(+), 15 deletions(-) diff --git a/navigation/logicgatesgame/lgates.md b/navigation/logicgatesgame/lgates.md index 07af687..09887ec 100644 --- a/navigation/logicgatesgame/lgates.md +++ b/navigation/logicgatesgame/lgates.md @@ -114,11 +114,12 @@ permalink: /logicgame Name - Score + Score1 + Action - + @@ -159,8 +160,13 @@ async function create_User() { async function fetch_Data() { try { + // Reference the table body + const tableBody = document.querySelector("#dataTable tbody"); + tableBody.innerHTML = ""; // Clear any existing rows + + // Fetch data from the backend const response = await fetch("http://127.0.0.1:8887/api/lgate", { - method: "GET", // Assuming the backend supports GET for retrieving all records + method: "GET", headers: { "Content-Type": "application/json", }, @@ -168,33 +174,66 @@ async function fetch_Data() { if (response.ok) { const data = await response.json(); - const tableBody = document.querySelector("#dataTable tbody"); - - // Clear existing table data - tableBody.innerHTML = ""; - // Populate table with fetched data + // Loop through the data to populate rows data.forEach((item) => { const row = document.createElement("tr"); - const nameCell = document.createElement("td"); - const scoreCell = document.createElement("td"); + // Add Name cell + const nameCell = document.createElement("td"); nameCell.textContent = item.name; - scoreCell.textContent = item.score; - row.appendChild(nameCell); + + // Add Score cell + const scoreCell = document.createElement("td"); + scoreCell.textContent = item.score; row.appendChild(scoreCell); + + // Add Action cell with delete button + const actionCell = document.createElement("td"); + const deleteButton = document.createElement("button"); + deleteButton.textContent = "Delete"; + deleteButton.onclick = () => delete_User(item.id); // Pass the ID to the delete function + actionCell.appendChild(deleteButton); + row.appendChild(actionCell); + + // Append the row to the table body tableBody.appendChild(row); }); + } else { + console.error("Failed to fetch data:", await response.text()); + } + } catch (error) { + console.error("Error fetching data:", error); + } +} + +async function delete_User(userId) { + if (!confirm("Are you sure you want to delete this user?")) { + return; // Cancel the deletion if the user doesn't confirm + } - alert("Data retrieved successfully!"); + try { + const response = await fetch("http://127.0.0.1:8887/api/lgate", { + method: "DELETE", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ id: userId }), // Send the ID to delete + }); + + const result = await response.json(); + + if (response.ok) { + alert("User deleted successfully!"); + fetch_Data(); // Refresh the table after deletion } else { - const result = await response.json(); alert(`Error: ${result.error}`); } } catch (error) { console.error("Error:", error); - alert("Failed to fetch data from the server."); + alert("Failed to delete the user."); } } + \ No newline at end of file From 612e0ce7e7f6d5f73574c0bd6640b9d7406427c8 Mon Sep 17 00:00:00 2001 From: LarsLindain Date: Mon, 27 Jan 2025 22:33:36 -0800 Subject: [PATCH 2/5] test2 --- navigation/logicgatesgame/lgates.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/navigation/logicgatesgame/lgates.md b/navigation/logicgatesgame/lgates.md index 09887ec..225f1da 100644 --- a/navigation/logicgatesgame/lgates.md +++ b/navigation/logicgatesgame/lgates.md @@ -114,7 +114,7 @@ permalink: /logicgame Name - Score1 + Score Action From b490e4f004a1a9c128289b1cd116c444cf834e9c Mon Sep 17 00:00:00 2001 From: LarsLindain Date: Mon, 27 Jan 2025 23:00:50 -0800 Subject: [PATCH 3/5] failed test --- navigation/logicgatesgame/lgates.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/navigation/logicgatesgame/lgates.md b/navigation/logicgatesgame/lgates.md index 225f1da..1e58b2c 100644 --- a/navigation/logicgatesgame/lgates.md +++ b/navigation/logicgatesgame/lgates.md @@ -88,7 +88,7 @@ permalink: /logicgame -

Welcome to the Logic Gates Lesson!

+

Welcome to the Logic Gates 123 Lesson!

Input your name and score to save them!

From 4f889e2eaa3f00b72ff2c220ecf22b11664bc011 Mon Sep 17 00:00:00 2001 From: LarsLindain Date: Tue, 28 Jan 2025 11:23:09 -0800 Subject: [PATCH 4/5] delete has been added --- navigation/logicgatesgame/lgates.md | 34 +++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/navigation/logicgatesgame/lgates.md b/navigation/logicgatesgame/lgates.md index 1e58b2c..53476fe 100644 --- a/navigation/logicgatesgame/lgates.md +++ b/navigation/logicgatesgame/lgates.md @@ -236,4 +236,38 @@ async function delete_User(userId) { } } + +async function update_User(userId) { + const newName = prompt("Enter the new name:"); + const newScore = prompt("Enter the new score:"); + + if (!newName || !newScore) { + alert("Both fields are required!"); + return; + } + + const data = { id: userId, name: newName, score: newScore }; + + try { + const response = await fetch("http://127.0.0.1:8887/api/lgate", { + method: "PUT", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(data), + }); + + if (response.ok) { + alert("User updated successfully!"); + fetch_Data(); // Refresh the table to show updated data + } else { + const result = await response.json(); + alert(`Error updating user: ${result.error}`); + } + } catch (error) { + console.error("Error:", error); + alert("Failed to update the user."); + } +} + \ No newline at end of file From bcd17d512b9e287b268849d372a0b01fcd988562 Mon Sep 17 00:00:00 2001 From: LarsLindain Date: Tue, 28 Jan 2025 15:08:47 -0800 Subject: [PATCH 5/5] oops typo --- navigation/logicgatesgame/lgates.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/navigation/logicgatesgame/lgates.md b/navigation/logicgatesgame/lgates.md index 53476fe..a5b603a 100644 --- a/navigation/logicgatesgame/lgates.md +++ b/navigation/logicgatesgame/lgates.md @@ -88,7 +88,7 @@ permalink: /logicgame -

Welcome to the Logic Gates 123 Lesson!

+

Welcome to the Logic Gates Lesson!

Input your name and score to save them!