feat: add getAllUsers function and integrate user fetching in dashboard
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import mysql from "mysql2";
|
import mysql from "mysql2";
|
||||||
import dotenv from "dotenv";
|
import dotenv from "dotenv";
|
||||||
|
import { error } from "console";
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
|
||||||
// Create a MySQL connection pool using environment variables for configuration
|
// Create a MySQL connection pool using environment variables for configuration
|
||||||
@@ -130,3 +131,12 @@ export async function deleteUser(
|
|||||||
}
|
}
|
||||||
} catch (err) {}
|
} catch (err) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getAllUsers() {
|
||||||
|
try {
|
||||||
|
const [data] = await pool.query("SELECT * FROM users;");
|
||||||
|
return { result: data, success: true };
|
||||||
|
} catch (err) {
|
||||||
|
return { result: err, success: false };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -4,7 +4,13 @@ const app = express();
|
|||||||
const port = 4000;
|
const port = 4000;
|
||||||
|
|
||||||
// Importing database functions for user operations
|
// Importing database functions for user operations
|
||||||
import { loginUser, createUser, updateUser, deleteUser } from "./database.js";
|
import {
|
||||||
|
loginUser,
|
||||||
|
createUser,
|
||||||
|
updateUser,
|
||||||
|
deleteUser,
|
||||||
|
getAllUsers,
|
||||||
|
} from "./database.js";
|
||||||
|
|
||||||
// Middleware to parse URL-encoded bodies (form submissions)
|
// Middleware to parse URL-encoded bodies (form submissions)
|
||||||
app.use(express.urlencoded({ extended: true }));
|
app.use(express.urlencoded({ extended: true }));
|
||||||
@@ -39,9 +45,23 @@ app.get("/", (req, res) => {
|
|||||||
|
|
||||||
// Variable to keep track of the latest logged-in user
|
// Variable to keep track of the latest logged-in user
|
||||||
let latestUser;
|
let latestUser;
|
||||||
|
let response;
|
||||||
|
|
||||||
|
// static function to get all users
|
||||||
|
function allUsers() {
|
||||||
|
getAllUsers().then((resultFromFunc) => {
|
||||||
|
if (resultFromFunc.success) {
|
||||||
|
response = resultFromFunc.result;
|
||||||
|
} else {
|
||||||
|
response = resultFromFunc.result;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Route to handle user login
|
// Route to handle user login
|
||||||
app.post("/login", (req, res) => {
|
app.post("/login", (req, res) => {
|
||||||
|
allUsers();
|
||||||
|
|
||||||
// Attempt to log in the user with provided credentials
|
// Attempt to log in the user with provided credentials
|
||||||
loginUser(req.body.username, req.body.password).then((result) => {
|
loginUser(req.body.username, req.body.password).then((result) => {
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
@@ -51,6 +71,7 @@ app.post("/login", (req, res) => {
|
|||||||
newLink: `/dashboard/${result.user.id}`,
|
newLink: `/dashboard/${result.user.id}`,
|
||||||
alert: null,
|
alert: null,
|
||||||
success: null,
|
success: null,
|
||||||
|
users: response,
|
||||||
});
|
});
|
||||||
latestUser = result;
|
latestUser = result;
|
||||||
} else {
|
} else {
|
||||||
@@ -77,12 +98,22 @@ app.post(["/createUser", "/updateUser", "/deleteUser"], (req, res) => {
|
|||||||
if (latestUser && req.body.username !== latestUser.user.username) {
|
if (latestUser && req.body.username !== latestUser.user.username) {
|
||||||
funcName = deleteUser;
|
funcName = deleteUser;
|
||||||
} else {
|
} else {
|
||||||
|
let response;
|
||||||
|
|
||||||
|
getAllUsers().then((resultFromFunc) => {
|
||||||
|
if (resultFromFunc.success) {
|
||||||
|
response = resultFromFunc.result;
|
||||||
|
} else {
|
||||||
|
response = resultFromFunc.result;
|
||||||
|
}
|
||||||
|
});
|
||||||
// Render dashboard with alert if trying to delete logged-in user
|
// Render dashboard with alert if trying to delete logged-in user
|
||||||
res.status(400).render("dashboard.ejs", {
|
res.status(400).render("dashboard.ejs", {
|
||||||
sqlResult: latestUser,
|
sqlResult: latestUser,
|
||||||
newLink: latestUser ? `/dashboard/${latestUser.id}` : "#",
|
newLink: latestUser ? `/dashboard/${latestUser.id}` : "#",
|
||||||
alert: "Cannot delete the currently logged-in user!",
|
alert: "Cannot delete the currently logged-in user!",
|
||||||
success: null,
|
success: null,
|
||||||
|
users: response,
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -100,20 +131,41 @@ app.post(["/createUser", "/updateUser", "/deleteUser"], (req, res) => {
|
|||||||
req.body.email
|
req.body.email
|
||||||
).then((result) => {
|
).then((result) => {
|
||||||
if (result.success === true) {
|
if (result.success === true) {
|
||||||
|
let response;
|
||||||
|
|
||||||
|
getAllUsers().then((resultFromFunc) => {
|
||||||
|
if (resultFromFunc.success) {
|
||||||
|
response = resultFromFunc.result;
|
||||||
|
} else {
|
||||||
|
response = resultFromFunc.result;
|
||||||
|
}
|
||||||
|
});
|
||||||
// On success, render dashboard with success message
|
// On success, render dashboard with success message
|
||||||
res.status(201).render("dashboard.ejs", {
|
res.status(201).render("dashboard.ejs", {
|
||||||
sqlResult: latestUser,
|
sqlResult: latestUser,
|
||||||
newLink: `/dashboard/${latestUser.id}`,
|
newLink: `/dashboard/${latestUser.id}`,
|
||||||
alert: null,
|
alert: null,
|
||||||
success: "User action successful!",
|
success: "User action successful!",
|
||||||
|
users: response,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
let response;
|
||||||
|
|
||||||
|
getAllUsers().then((resultFromFunc) => {
|
||||||
|
if (resultFromFunc.success) {
|
||||||
|
response = resultFromFunc.result;
|
||||||
|
} else {
|
||||||
|
response = resultFromFunc.result;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// On failure, render dashboard with alert
|
// On failure, render dashboard with alert
|
||||||
res.status(400).render("dashboard.ejs", {
|
res.status(400).render("dashboard.ejs", {
|
||||||
sqlResult: latestUser,
|
sqlResult: latestUser,
|
||||||
newLink: `/dashboard/${latestUser.id}`,
|
newLink: `/dashboard/${latestUser.id}`,
|
||||||
alert: "User action failed!",
|
alert: "User action failed!",
|
||||||
success: null,
|
success: null,
|
||||||
|
users: response,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@@ -227,6 +227,16 @@
|
|||||||
Delete User
|
Delete User
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="col-sm">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onclick="callFunc('fetchAllUsers')"
|
||||||
|
class="btn btn-info w-100"
|
||||||
|
>
|
||||||
|
Fetch all Users
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
@@ -239,6 +249,37 @@
|
|||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="user-table">
|
||||||
|
<h4 class="mt-5 mb-3">All Users</h4>
|
||||||
|
<!-- Table to display all users -->
|
||||||
|
<table class="table">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th scope="col">id</th>
|
||||||
|
<th scope="col">Username</th>
|
||||||
|
<th scope="col">First name</th>
|
||||||
|
<th scope="col">Last name</th>
|
||||||
|
<th scope="col">E-Mail</th>
|
||||||
|
<th scope="col">Password</th>
|
||||||
|
<th scope="col">Created</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody class="table-group-divider">
|
||||||
|
<% users.forEach(user => { %>
|
||||||
|
<tr>
|
||||||
|
<td><%= user.id %></td>
|
||||||
|
<td><%= user.username %></td>
|
||||||
|
<td><%= user.first_name %></td>
|
||||||
|
<td><%= user.last_name %></td>
|
||||||
|
<td><%= user.email %></td>
|
||||||
|
<td><%= user.password %></td>
|
||||||
|
<td><%= user.created.toISOString() %></td>
|
||||||
|
</tr>
|
||||||
|
<% }) %>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Bootstrap JS Bundle -->
|
<!-- Bootstrap JS Bundle -->
|
||||||
|
Reference in New Issue
Block a user