-
Notifications
You must be signed in to change notification settings - Fork 1
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 }); | ||
} | ||
} | ||
|
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" }); | ||
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};`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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};`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 😷 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 }); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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