Player search

This commit is contained in:
RochesterX
2025-11-17 18:56:31 -05:00
parent 9fd3a76546
commit 310bbc9679
10 changed files with 137 additions and 51 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@@ -1,8 +1,6 @@
export async function postData(url, data, token = null) {
const elements = url.split("/");
console.log(elements);
const end = elements[elements.length - 1];
console.log(end);
const res = await fetch(end, token == null ? {
method: "POST",

9
public/delete.js Normal file
View File

@@ -0,0 +1,9 @@
import { postData } from "./client.js";
document.getElementById("deleteButton").onclick = async e => {
e.preventDefault();
const resultObject = await postData("/delete", { username: true, actor: true }, window.localStorage.getItem("token"));
window.localStorage.removeItem("token");
alert(resultObject.message);
window.location.href = "/login.html"
};

View File

@@ -7,20 +7,16 @@
<body>
<div class="center">
<h2>Home</h2>
<a href="search.html">Search Users</a>
<a href="search.html">Search Players</a>
<br>
<a href="info.html">Personal Information</a>
<br>
<a class="visibilityRestriction" data-role="professor" href="sections.html">Manage My Sections</a>
<br>
<button id="logoutButton">Log Out</button>
<br>
<button id="deleteButton">Delete Account</button>
<button id="deleteButton" style="display: none;">Delete Account</button>
<script type="module" src="logout.js"></script>
<script type="module" src="delete.js"></script>
<script type="module" src="visibility.js"></script>
</div>
</body>
</html>

35
public/info.html Normal file
View File

@@ -0,0 +1,35 @@
<!DOCTYPE html>
<html>
<head>
<title>Personal Information</title>
<link rel="stylesheet" href="../assets/css/style.css">
</head>
<body>
<div class="center">
<h2>Search Users</h2>
<form id="infoForm">
<div class="inputGroup">
<p>Username:</p>
<p id="usernameField">[username]</p>
</div>
<div class="inputGroup">
<p>First Name:</p>
<input type="text" id="firstNameField" placeholder="">
</div>
<div class="inputGroup">
<p>Last Name:</p>
<input type="text" id="lastNameField" placeholder="">
</div>
<div class="inputGroup">
<p>Date of Birth:</p>
<input type="date" id="dobField" placeholder="">
</div>
<button type="submit">Update Information</button>
</form>
<a href="home.html">Return Home</a>
</div>
<script type="module" src="info.js"></script>
</body>
</html>

36
public/info.js Normal file
View File

@@ -0,0 +1,36 @@
import { postData, verifyLogin } from "./client.js";
verifyLogin();
const token = window.localStorage.getItem("token");
const infoForm = document.getElementById("infoForm");
async function fetchPersonalInformation() {
const userData = await postData("https://134.209.36.64:3000/getInfo", {}, token);
console.log(userData);
document.getElementById("usernameField").textContent = userData.Username;
document.getElementById("firstNameField").value = userData.FirstName;
document.getElementById("lastNameField").value = userData.LastName;
document.getElementById("dobField").value = userData.DOB.split("T")[0];
return userData
}
const userData = fetchPersonalInformation();
async function updatePersonalInformation(e) {
console.log(verifyLogin());
e.preventDefault();
const userData = {
firstName: document.getElementById("firstNameField").value,
lastName: document.getElementById("lastNameField").value,
dob: document.getElementById("dobField").value
}
console.log(userData.dob);
const resultObject = await postData("https://134.209.36.64:3000/setInfo", userData, token);
alert(resultObject.message);
}
infoForm.onsubmit = updatePersonalInformation;

View File

@@ -11,10 +11,8 @@
<input type="text" id="regUser" placeholder="Username" required>
<input type="password" id="regPass" placeholder="Password" required>
<select id="regRole" name="Role" required>
<option value="student">Student</option>
<option value="professor">Professor</option>
<option value="staff">Staff</option>
<option value="member">Member</option>
<option value="user">User</option>
<option value="player">Player</option>
</select>
<button type="submit">Register</button>
</form>

View File

@@ -1,15 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<title>Search Users</title>
<title>Search Players</title>
<link rel="stylesheet" href="../assets/css/style.css">
<link rel="stylesheet" href="../public/assets/css/style.css">
</head>
<body>
<div class="center">
<h2>Search Users</h2>
<h2>Search Players</h2>
<form id="searchForm">
<select id="category" required>
<select id="category" style="display: none;">
<option value="Id">ID</option>
<option value="Username">Username</option>
<option value="Last Name">Last Name</option>
@@ -19,25 +19,18 @@
<option value="LastLogin">Last Login</option>
<option value="CreatedAt">Date Created</option>
</select>
<p>Search players by name:</p>
<input type="text" id="query" placeholder="Contains" required>
<button type="submit">Search</button>
</form>
<table id="table">
<thead>
<tr>
<th>ID</th>
<th>Username</th>
<th>Name</th>
<th>DOB</th>
<th>Role</th>
<th>Last Login</th>
<th>Date Created</th>
</tr>
</thead>
<thead id="header"></thead>
<tbody id="results"></tbody>
</table>
<p id="nomatch" style="display: none;">No matches found.</p>
<br>
<a href="home.html">Return Home</a>

View File

@@ -4,8 +4,10 @@ const searchForm = document.getElementById("searchForm");
const token = window.localStorage.getItem("token");
const tableHeader = document.getElementById("header");
const tableBody = document.getElementById("results");
const table = document.getElementById("table");
const nomatch = document.getElementById("nomatch");
updateTableVisibility();
@@ -13,23 +15,37 @@ if (searchForm)searchForm.onsubmit = async e => {
e.preventDefault();
if (!verifyLogin()) return;
let resultObject = await postData("https://134.209.36.64:3000/getUsers", {
query: document.getElementById("query").value,
category: document.getElementById("category").value
let resultObject = await postData("https://134.209.36.64:3000/getPlayers", {
player: document.getElementById("query").value
}, token);
tableBody.innerHTML = "";
resultObject.matches.forEach(user => {
const row = document.createElement("tr");
if (resultObject.matches.length === 0) {
nomatch.style.display = "";
table.style.display = "none";
return;
}
nomatch.style.display = "none"
table.style.display = "";
tableHeader.innerHTML = "";
const headerRow = document.createElement("tr")
Object.keys(resultObject.matches[0]).forEach(attribute => {
headerRow.innerHTML += `<td>${attribute}</td>`;
})
tableHeader.appendChild(headerRow);
tableBody.innerHTML = "";
resultObject.matches.forEach(player => {
const row = document.createElement("tr");
//for (attribute in player) {
// row.innerHTML += `<td>${player}</td>l`;
//}
row.innerHTML = `
<td>${user.Id}</td>
<td>${formatText(user.Username)}</td>
<td>${formatText(user.FirstName)} ${formatText(user.LastName)}</td>
<td>${formatDate(user.DOB)}</td>
<td>${formatText(user.Role)}</td>
<td>${formatDateTime(user.LastLogin)}</td>
<td>${formatDate(user.CreatedAt)}</td>
<td>${player.player_id}</td>
<td>${player.player_name}</td>
<td>${formatSalary(player.salary)}</td>
<td>${player.team_name}</td>
<td>${player.position}</td>
`;
tableBody.appendChild(row);
});
@@ -59,6 +75,17 @@ function formatText(text) {
return "Unknown format";
}
}
function formatSalary(text) {
if (text == null) return "—";
try {
var millions = (parseInt(text, 10) / 1000000).toFixed(2);
return `$${millions} million`;
} catch (e) {
return "Unknown format"
}
}
function formatDate(date) {
if (date == null) return "—";

View File

@@ -98,20 +98,14 @@ app.post("/login", async (req, res) => {
}
});
app.post("/getUsers", authenticate, async (req, res) => {
const { query, category } = req.body;
const allowed = ["Id", "Username", "FirstName", "LastName", "DOB", "Role", "LastLogin", "CreatedAt"];
if (!allowed.includes(category)) {
res.status(500).json({ message: `Illegal category "${category}"`});
}
app.post("/getPlayers", authenticate, async (req, res) => {
const { player } = req.body;
const result = await pool.request()
.input("query", sql.VarChar, query)
.query(`SELECT * FROM Users WHERE ${category} LIKE '%' + @query + '%'`);
.input("query", sql.VarChar, player)
.query(`SELECT * FROM [Player, IMPORTANT] WHERE player_name LIKE '%' + @query + '%'`);
res.status(200).json({ query: query, matches: result.recordset });
res.status(200).json({ query: player, matches: result.recordset });
});
app.post("/getInfo", authenticate, async (req, res) => {