Database routes split into their own file and attached to app.js
This commit is contained in:
96
src/app.js
96
src/app.js
@@ -284,100 +284,8 @@ generateClubRoutes(app);
|
||||
Favorite Team Database
|
||||
*************************/
|
||||
|
||||
// Function to add a new row to the FavoriteTeams table
|
||||
// database configuration
|
||||
|
||||
app.post("/favteam/add", async (req, res, next) => {
|
||||
try {
|
||||
const { userID, teamID, teamName, teamLogo } = req.body;
|
||||
|
||||
// Check if the user is logged in
|
||||
if (!req.session.user) {
|
||||
return res
|
||||
.status(400)
|
||||
.json({ message: "Login or register to add a favorite team." });
|
||||
}
|
||||
|
||||
// Insert the new favorite team into the database
|
||||
const query = {
|
||||
text: "INSERT INTO FavoriteTeams (UserID, TeamID, TeamName, TeamLogo) VALUES ($1, $2, $3, $4)",
|
||||
values: [userID, teamID, teamName, teamLogo],
|
||||
};
|
||||
|
||||
await db.none(query);
|
||||
console.log("New favorite team added successfully.");
|
||||
return res
|
||||
.status(200)
|
||||
.json({ message: "New favorite team added successfully." });
|
||||
} catch (error) {
|
||||
console.error("Error adding favorite team:", error);
|
||||
return res.status(500).json({ error: "Error adding favorite team" });
|
||||
}
|
||||
});
|
||||
|
||||
app.post("/favteam/remove", async (req, res) => {
|
||||
try {
|
||||
const { userID, teamID } = req.body;
|
||||
|
||||
// Check if the team exists for the user
|
||||
const existingTeam = await db.oneOrNone(
|
||||
"SELECT * FROM FavoriteTeams WHERE UserID = $1 AND TeamID = $2",
|
||||
[userID, teamID]
|
||||
);
|
||||
|
||||
// If the team does not exist for the user, return a 404 error
|
||||
if (!existingTeam) {
|
||||
return res
|
||||
.status(404)
|
||||
.json({ message: "This team is not in your favorites." });
|
||||
}
|
||||
|
||||
// Remove the favorite team from the database
|
||||
await db.none(
|
||||
"DELETE FROM FavoriteTeams WHERE UserID = $1 AND TeamID = $2",
|
||||
[userID, teamID]
|
||||
);
|
||||
|
||||
console.log("Favorite team removed successfully.");
|
||||
return res
|
||||
.status(200)
|
||||
.json({ message: "Favorite team removed successfully." });
|
||||
} catch (error) {
|
||||
console.error("Error removing favorite team:", error);
|
||||
|
||||
// If the error is a database error, return a 500 error
|
||||
if (error instanceof QueryResultError) {
|
||||
return res.status(500).json({
|
||||
error: "Database error occurred while removing favorite team",
|
||||
});
|
||||
}
|
||||
|
||||
// If the error is a generic error, return a 400 error
|
||||
return res
|
||||
.status(400)
|
||||
.json({ error: "Error occurred while removing favorite team" });
|
||||
}
|
||||
});
|
||||
|
||||
async function getFavoriteTeamsForUser(userId) {
|
||||
try {
|
||||
// Execute the SQL query
|
||||
const favoriteTeams = await db.any(
|
||||
`
|
||||
SELECT * FROM FavoriteTeams
|
||||
WHERE UserID = $1;
|
||||
`,
|
||||
userId
|
||||
);
|
||||
`a`;
|
||||
|
||||
// Return the result
|
||||
return favoriteTeams;
|
||||
} catch (error) {
|
||||
console.error("Error fetching favorite teams:", error);
|
||||
throw error; // Rethrow the error for handling at a higher level
|
||||
}
|
||||
}
|
||||
const databaseRoutes = require("./routes/database/database-routes");
|
||||
app.use("/", databaseRoutes);
|
||||
|
||||
// *****************************************************
|
||||
// <!-- Section 6 : Export the App Module to Index.js -->
|
||||
|
||||
98
src/routes/database/database-routes.js
Normal file
98
src/routes/database/database-routes.js
Normal file
@@ -0,0 +1,98 @@
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
|
||||
// Function to add a new row to the FavoriteTeams table
|
||||
// database configuration
|
||||
router.post("/favteam/add", async (req, res, next) => {
|
||||
try {
|
||||
const { userID, teamID, teamName, teamLogo } = req.body;
|
||||
|
||||
// Check if the user is logged in
|
||||
if (!req.session.user) {
|
||||
return res
|
||||
.status(400)
|
||||
.json({ message: "Login or register to add a favorite team." });
|
||||
}
|
||||
|
||||
// Insert the new favorite team into the database
|
||||
const query = {
|
||||
text: "INSERT INTO FavoriteTeams (UserID, TeamID, TeamName, TeamLogo) VALUES ($1, $2, $3, $4)",
|
||||
values: [userID, teamID, teamName, teamLogo],
|
||||
};
|
||||
|
||||
await db.none(query);
|
||||
console.log("New favorite team added successfully.");
|
||||
return res
|
||||
.status(200)
|
||||
.json({ message: "New favorite team added successfully." });
|
||||
} catch (error) {
|
||||
console.error("Error adding favorite team:", error);
|
||||
return res.status(500).json({ error: "Error adding favorite team" });
|
||||
}
|
||||
});
|
||||
|
||||
router.post("/favteam/remove", async (req, res) => {
|
||||
try {
|
||||
const { userID, teamID } = req.body;
|
||||
|
||||
// Check if the team exists for the user
|
||||
const existingTeam = await db.oneOrNone(
|
||||
"SELECT * FROM FavoriteTeams WHERE UserID = $1 AND TeamID = $2",
|
||||
[userID, teamID]
|
||||
);
|
||||
|
||||
// If the team does not exist for the user, return a 404 error
|
||||
if (!existingTeam) {
|
||||
return res
|
||||
.status(404)
|
||||
.json({ message: "This team is not in your favorites." });
|
||||
}
|
||||
|
||||
// Remove the favorite team from the database
|
||||
await db.none(
|
||||
"DELETE FROM FavoriteTeams WHERE UserID = $1 AND TeamID = $2",
|
||||
[userID, teamID]
|
||||
);
|
||||
|
||||
console.log("Favorite team removed successfully.");
|
||||
return res
|
||||
.status(200)
|
||||
.json({ message: "Favorite team removed successfully." });
|
||||
} catch (error) {
|
||||
console.error("Error removing favorite team:", error);
|
||||
|
||||
// If the error is a database error, return a 500 error
|
||||
if (error instanceof QueryResultError) {
|
||||
return res.status(500).json({
|
||||
error: "Database error occurred while removing favorite team",
|
||||
});
|
||||
}
|
||||
|
||||
// If the error is a generic error, return a 400 error
|
||||
return res
|
||||
.status(400)
|
||||
.json({ error: "Error occurred while removing favorite team" });
|
||||
}
|
||||
});
|
||||
|
||||
async function getFavoriteTeamsForUser(userId) {
|
||||
try {
|
||||
// Execute the SQL query
|
||||
const favoriteTeams = await db.any(
|
||||
`
|
||||
SELECT * FROM FavoriteTeams
|
||||
WHERE UserID = $1;
|
||||
`,
|
||||
userId
|
||||
);
|
||||
`a`;
|
||||
|
||||
// Return the result
|
||||
return favoriteTeams;
|
||||
} catch (error) {
|
||||
console.error("Error fetching favorite teams:", error);
|
||||
throw error; // Rethrow the error for handling at a higher level
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user