Skip to content

Commit

Permalink
Merge pull request #6 from icssc/dang-test-be-firebase
Browse files Browse the repository at this point in the history
removed middleware
  • Loading branch information
NwinNwin authored May 14, 2024
2 parents 155d428 + 38cb863 commit d879dd7
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 101 deletions.
132 changes: 66 additions & 66 deletions packages/functions/src/routes/items.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
49 changes: 20 additions & 29 deletions packages/functions/src/routes/leaderboard.js
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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`
);
Expand All @@ -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}`
);
Expand All @@ -59,54 +57,47 @@ 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") {
return res.status(400).send("Invalid request parameters");
}

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");
}

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`,
Expand All @@ -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) {
Expand Down
12 changes: 6 additions & 6 deletions packages/functions/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" }));
Expand All @@ -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);

0 comments on commit d879dd7

Please sign in to comment.