This commit is contained in:
RochesterX
2025-11-26 21:10:22 -05:00
parent d781a90e87
commit 344d46d89e
8 changed files with 307 additions and 37 deletions

View File

@@ -32,7 +32,7 @@ dbConfig = {
user: "server",
password: "TorusField1*",
server: "mssql.rochesterx.dev",
database: "ProjectTest",
database: "Project",
options: { trustServerCertificate: true }
};
@@ -105,22 +105,91 @@ app.post("/login", async (req, res) => {
}
});
app.post("/getWatchlist", authenticate, async (req, res) => {
const { id } = req.body;
const watchlist = await pool.request()
.input("userID", sql.Int, req.user.Id)
.input("id", sql.Int, id)
.query(`SELECT Player.PlayerName, Player.Team, Player.Position, Player.PlayerID FROM Watch JOIN Player ON Watch.PlayerID = Player.PlayerID WHERE UserID = @userID`);
res.status(200).json({ watchlist: watchlist.recordset });
});
app.post("/isWatched", authenticate, async (req, res) => {
const { id } = req.body;
const watchlist = await pool.request()
.input("userID", sql.Int, req.user.Id)
.input("id", sql.Int, id)
.query(`SELECT PlayerID FROM Watch WHERE UserID = @userID`);
const isWatched = watchlist.recordset.some(row => row.PlayerID === parseInt(id));
res.status(200).json({ isWatched: isWatched });
});
app.post("/toggleWatched", authenticate, async (req, res) => {
const { id } = req.body;
const watchlist = await pool.request()
.input("userID", sql.Int, req.user.Id)
.input("id", sql.Int, id)
.query(`SELECT PlayerID FROM Watch WHERE UserID = @userID`);
console.log(watchlist.recordset);
const isWatched = watchlist.recordset.some(row => row.PlayerID === parseInt(id));
console.log(isWatched);
if (isWatched) {
const result = await pool.request()
.input("userID", sql.Int, req.user.Id)
.input("id", sql.Int, id)
.query(`DELETE FROM Watch WHERE UserID = @userID AND PlayerID = @id`);
res.status(200).json({ message: "No longer watching player" });
return;
}
// Otherwise, watch the player
const result = await pool.request()
.input("userID", sql.Int, req.user.Id)
.input("id", sql.Int, id)
.query(`INSERT INTO Watch (UserID, PlayerID) VALUES (@userID, @id)`);
res.status(200).json({ message: "Watching Player" });
});
app.post("/getPlayers", authenticate, async (req, res) => {
const { player } = req.body;
const result = await pool.request()
.input("query", sql.VarChar, player)
.query(`SELECT * FROM Player WHERE player_name LIKE '%' + @query + '%'`);
.query(`SELECT p.PlayerID, p.PlayerName, c.TotalValue, p.Team, p.Position FROM Player AS p JOIN Contract AS c ON p.PlayerID = c.PlayerID WHERE p.PlayerName LIKE '%' + @query + '%'`);
res.status(200).json({ query: player, matches: result.recordset });
});
app.post("/getHighest", authenticate, async (req, res) => {
const { amount } = req.body;
const result = await pool.request()
.input("amount", sql.Int, amount)
.query(`
SELECT TOP (@amount) p.PlayerID, p.PlayerName, p.[Position], p.Team, TotalValue
FROM Player AS p JOIN Contract AS c ON p.PlayerID = c.PlayerID
ORDER BY TotalValue DESC;
`);
res.status(200).json({ matches: result.recordset });
});
app.post("/getPlayer", authenticate, async (req, res) => {
const { id } = req.body;
const result = await pool.request()
.input("query", sql.VarChar, id)
.query(`SELECT * FROM Player WHERE player_id = @query`);
.query(`SELECT p.PlayerName, p.PlayerID, c.TotalValue, p.Team, p.Position FROM Player AS p JOIN Contract AS c ON p.PlayerID = c.PlayerID WHERE p.PlayerID = @query`);
if (result.recordset.length !== 1) {
res.status(400).json({ success: false })