Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added db and get/post endpoints #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@types/node": "18.15.1",
"@types/react": "18.0.28",
"@types/react-dom": "18.0.11",
"@vercel/postgres": "^0.8.0",
"discord.js": "^14.8.0",
"eslint": "8.36.0",
"eslint-config-next": "13.2.4",
Expand All @@ -24,7 +25,8 @@
"react-dom": "18.2.0",
"sass": "^1.59.2",
"totp-generator": "^0.0.14",
"typescript": "4.9.5"
"typescript": "4.9.5",
"vercel": "^34.1.7"
},
"devDependencies": {
"@types/got": "^9.6.12",
Expand Down
37 changes: 37 additions & 0 deletions pages/api/v1/sql/add-board.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { NextApiRequest, NextApiResponse } from 'next';
import NextCors from "nextjs-cors";
import { sql } from '@vercel/postgres';
import { verifyAuth } from "@/util";

export default async function handler(
req: NextApiRequest,
res: NextApiResponse,
) {
await NextCors(req, res, {
methods: ["POST"],
origin: "*",
optionsSuccessStatus: 200,
});
// try {
// await verifyAuth(req);
// } catch (err: any) {
// return res.status(400).json({ error: err });
// }

// Validate body provided
if (!req.body) return res.status(400).json({ error: "No body provided" });
const { Name, Discord } = req.body;
// Validate body values exist and are acceptable
if (!Name) return res.status(400).json({ error: "Missing required body field: Name" });
if (!Discord) return res.status(400).json({ error: "Missing required body field: Discord" });


try {
// Add to DB
await sql`INSERT INTO Board (Name, Discord) VALUES (${Name}, ${Discord});`;
return res.status(200).json({ message: `Board Member ${Name} added.` });
} catch (e: any) {
return res.status(500).json({ e });
}
}

42 changes: 42 additions & 0 deletions pages/api/v1/sql/get-board.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { NextApiRequest, NextApiResponse } from 'next';
import NextCors from "nextjs-cors";
import { sql } from '@vercel/postgres';
import { verifyAuth } from "@/util";

export default async function handler(
req: NextApiRequest,
res: NextApiResponse,
) {
await NextCors(req, res, {
methods: ["GET"],
origin: "*",
optionsSuccessStatus: 200,
});
// try {
// await verifyAuth(req);
// } catch (err: any) {
// return res.status(400).json({ error: err });
// }

// Validate body provided
if (!req.body) return res.status(400).json({ error: "No body provided" });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar with Klefki, but how is this functionality anticipated to be used? Is it something where you search by the user name or discord to fetch a single board member? GET requests don't normally have a body so curious if this is the way it has to be structured

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea this is dead code mb

const { Name, Discord } = req.body;
// Validate body values exist and are acceptable
if (!Name && !Discord) return res.status(400).json({ error: "Missing required field: One field must be specified!" });

try {
if(Name){
console.log("Name found")
const row = await sql`SELECT * FROM Board WHERE name = ${Name};`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe limit 1? Not sure how names are enforced (if we have multiple people named "Alex" or if you need to have a last name)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to rework to add a separate endpoint to get single user

return res.status(200).json({ row });
}
else{
console.log("discord found");
const row = await sql`SELECT * FROM Board WHERE discord = ${Discord};`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we explore using an ORM, or is this the way it's done in vercel postgres? I don't remember 127 but I feel like they told us not to do this bc sql injection 😷

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vercel postgres is sanitized so it shud be fine but I'll do some testing

return res.status(200).json({ row });
}
} catch (e: any) {
return res.status(500).json({ e });
}
}

Loading