From 38cb8632dc5b0f95659b32ace700557115ba9799 Mon Sep 17 00:00:00 2001 From: Dang Nguyen Nguyen <93059380+NwinNwin@users.noreply.github.com> Date: Tue, 14 May 2024 13:28:51 -0700 Subject: [PATCH] removed middleware --- packages/functions/src/routes/items.js | 132 +++++++++---------- packages/functions/src/routes/leaderboard.js | 49 +++---- packages/functions/src/server.js | 12 +- 3 files changed, 92 insertions(+), 101 deletions(-) diff --git a/packages/functions/src/routes/items.js b/packages/functions/src/routes/items.js index 2e16b02..968231d 100644 --- a/packages/functions/src/routes/items.js +++ b/packages/functions/src/routes/items.js @@ -202,75 +202,75 @@ itemsRouter.get("/:id", async (req, res) => { } }); -// // Get email associated with an item id (only if user is logged in) -// itemsRouter.get("/:id/email", middleware.decodeToken, async (req, res) => { -// try { -// const { id } = req.params; - -// const item = await client.query( -// `SELECT email FROM ${itemsTable} WHERE id=$1`, -// [id] -// ); - -// res.json(item.rows[0]); -// } catch (error) { -// console.error(error); -// } -// }); +// Get email associated with an item id (only if user is logged in) +itemsRouter.get("/:id/email", async (req, res) => { + try { + const { id } = req.params; -// // Retrieve Items by Category -// itemsRouter.get("/category/:category", async (req, res) => { -// try { -// const { category } = req.params; + const item = await client.query( + `SELECT email FROM ${itemsTable} WHERE id=$1`, + [id] + ); -// const items = await client.query( -// `SELECT * FROM ${itemsTable} WHERE type=$1`, -// [category] -// ); + res.json(item.rows[0]); + } catch (error) { + console.error(error); + } +}); -// res.json(items.rows); -// } catch (error) { -// console.error(error); -// } -// }); +// Retrieve Items by Category +itemsRouter.get("/category/:category", async (req, res) => { + try { + const { category } = req.params; -// //Update a item resolve and helpfulness -// itemsRouter.put("/:id", middleware.decodeToken, async (req, res) => { -// try { -// const { id } = req.params; -// const { ishelped } = req.body; - -// const item = await client.query( -// `UPDATE ${itemsTable} SET isresolved=$1, ishelped=$2 WHERE id=$3 RETURNING *`, -// [true, ishelped, id] -// ); - -// res.json(item.rows[0]); -// } catch (error) { -// console.error(error); -// } -// }); + const items = await client.query( + `SELECT * FROM ${itemsTable} WHERE type=$1`, + [category] + ); -// // Mark an item as deleted instead of removing it from the database -// itemsRouter.delete("/:id", middleware.decodeToken, async (req, res) => { -// try { -// const { id } = req.params; - -// const markAsDeleted = await client.query( -// `UPDATE ${itemsTable} SET is_deleted = true WHERE id = $1 RETURNING *`, -// [id] -// ); - -// // If no rows are returned, it means that there was no item with the given ID. -// if (markAsDeleted.rowCount === 0) { -// return res.status(404).json({ message: "Item not found" }); -// } - -// res.json(markAsDeleted.rows[0]); -// } catch (error) { -// console.error(error.message); -// res.status(500).send("Server error"); -// } -// }); + res.json(items.rows); + } catch (error) { + console.error(error); + } +}); + +//Update a item resolve and helpfulness +itemsRouter.put("/:id", async (req, res) => { + try { + const { id } = req.params; + const { ishelped } = req.body; + + const item = await client.query( + `UPDATE ${itemsTable} SET isresolved=$1, ishelped=$2 WHERE id=$3 RETURNING *`, + [true, ishelped, id] + ); + + res.json(item.rows[0]); + } catch (error) { + console.error(error); + } +}); + +// Mark an item as deleted instead of removing it from the database +itemsRouter.delete("/:id", async (req, res) => { + try { + const { id } = req.params; + + const markAsDeleted = await client.query( + `UPDATE ${itemsTable} SET is_deleted = true WHERE id = $1 RETURNING *`, + [id] + ); + + // If no rows are returned, it means that there was no item with the given ID. + if (markAsDeleted.rowCount === 0) { + return res.status(404).json({ message: "Item not found" }); + } + + res.json(markAsDeleted.rows[0]); + } catch (error) { + console.error(error.message); + res.status(500).send("Server error"); + } +}); -// module.exports = itemsRouter; +module.exports = itemsRouter; diff --git a/packages/functions/src/routes/leaderboard.js b/packages/functions/src/routes/leaderboard.js index 28bb53c..f9a4f7c 100644 --- a/packages/functions/src/routes/leaderboard.js +++ b/packages/functions/src/routes/leaderboard.js @@ -1,12 +1,12 @@ const express = require("express"); const leaderboardRouter = express.Router(); -const middleware = require("../middleware/index.js"); +// const middleware = require("../middleware/index.js"); const { leaderboardTable } = require("../config/db-config.js"); const client = require("../server/db"); // add a user to leaderboard -leaderboardRouter.post("/", middleware.decodeToken, async (req, res) => { +leaderboardRouter.post("/", async (req, res) => { try { const { email, points } = req.body; // Get email and points from request body @@ -29,7 +29,6 @@ leaderboardRouter.post("/", middleware.decodeToken, async (req, res) => { // get all users on leaderboard (descending) leaderboardRouter.get("/", async (req, res) => { try { - const lbData = await client.query( `SELECT * FROM ${leaderboardTable} ORDER BY points DESC LIMIT 3` ); @@ -43,7 +42,6 @@ leaderboardRouter.get("/", async (req, res) => { // get count of users in leaderboard leaderboardRouter.get("/count", async (req, res) => { try { - const lbCount = await client.query( `SELECT COUNT(*) as count FROM ${leaderboardTable}` ); @@ -59,31 +57,27 @@ leaderboardRouter.get("/count", async (req, res) => { } }); -leaderboardRouter.patch( - "/changeSubscription", - middleware.decodeToken, - async (req, res) => { - try { - const { subscription, email } = req.body; - if (subscription === undefined) { - return res.status(400).send("Unsubscribe action is unknown"); - } - - await client.query( - `UPDATE ${leaderboardTable} SET subscription=$1 WHERE email=$2`, - [subscription, email] - ); - - res.send("Subscription updated successfully!"); - } catch (err) { - console.error(err); - res.status(500).send("Internal server error"); +leaderboardRouter.patch("/changeSubscription", async (req, res) => { + try { + const { subscription, email } = req.body; + if (subscription === undefined) { + return res.status(400).send("Unsubscribe action is unknown"); } + + await client.query( + `UPDATE ${leaderboardTable} SET subscription=$1 WHERE email=$2`, + [subscription, email] + ); + + res.send("Subscription updated successfully!"); + } catch (err) { + console.error(err); + res.status(500).send("Internal server error"); } -); +}); // update user's points -leaderboardRouter.put("/", middleware.decodeToken, async (req, res) => { +leaderboardRouter.put("/", async (req, res) => { const { email, pointsToAdd } = req.body; // Assume you're sending email and pointsToAdd in the request body if (!email || typeof pointsToAdd !== "number") { @@ -91,14 +85,12 @@ leaderboardRouter.put("/", middleware.decodeToken, async (req, res) => { } try { - // First, fetch the current points of the user const currentPointsResult = await client.query( `SELECT points FROM ${leaderboardTable} WHERE email=$1`, [email] ); - if (currentPointsResult.rows.length === 0) { return res.status(404).send("User not found"); } @@ -106,7 +98,6 @@ leaderboardRouter.put("/", middleware.decodeToken, async (req, res) => { const currentPoints = currentPointsResult.rows[0].points; const newPoints = currentPoints + pointsToAdd; - // Now, update the user's points in the leaderboard await client.query( `UPDATE ${leaderboardTable} SET points=$1 WHERE email=$2`, @@ -121,7 +112,7 @@ leaderboardRouter.put("/", middleware.decodeToken, async (req, res) => { }); // delete user from leaderboard -leaderboardRouter.delete("/:id", middleware.decodeToken, async (req, res) => { +leaderboardRouter.delete("/:id", async (req, res) => { try { const { id } = req.params; // Extract id from request body if (!id) { diff --git a/packages/functions/src/server.js b/packages/functions/src/server.js index e3bd2c2..33210e1 100644 --- a/packages/functions/src/server.js +++ b/packages/functions/src/server.js @@ -7,9 +7,9 @@ const app = express(); // const port = 8080; const serverless = require("serverless-http"); // ROUTES -// const items = require("./routes/items"); -// const nodemailer = require("./routes/nodeMailer"); -// const leaderboard = require("./routes/leaderboard"); +const items = require("./routes/items"); +const nodemailer = require("./routes/nodeMailer"); +const leaderboard = require("./routes/leaderboard"); app.use(cors()); app.use(express.json({ limit: "25mb" })); @@ -26,8 +26,8 @@ app.get("/", async (req, res) => { console.error(error); } }); -// app.use("/items", items); -// app.use("/leaderboard", leaderboard); -// app.use("/nodemailer", nodemailer); +app.use("/items", items); +app.use("/leaderboard", leaderboard); +app.use("/nodemailer", nodemailer); module.exports = serverless(app);