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

NW6 | Rabia Avci | Full-Stack-Project | Week-2 | Post new video endpoint #24

Merged
merged 3 commits into from
May 16, 2024

Conversation

RbAvci
Copy link
Owner

@RbAvci RbAvci commented May 16, 2024

Completed these tasks for this ticket:

  • There is a POST /api/videos backend endpoint to facilitate adding videos to the database.
  • The endpoint generates and attaches a unique ID to each newly added video.
  • The endpoint accepts video data in JSON format (example provided).
  • The endpoint responds with a JSON response that includes the generated id (example provided)

@RbAvci RbAvci requested a review from zelihapala May 16, 2024 00:21
Copy link

netlify bot commented May 16, 2024

Deploy Preview for watch-next-cyf ready!

Name Link
🔨 Latest commit 7d1678d
🔍 Latest deploy log https://app.netlify.com/sites/watch-next-cyf/deploys/66455181ef27880008177170
😎 Deploy Preview https://deploy-preview-24--watch-next-cyf.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Collaborator

@zelihapala zelihapala left a comment

Choose a reason for hiding this comment

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

Well done!

@zelihapala zelihapala merged commit bd0d054 into main May 16, 2024
6 checks passed
Copy link
Collaborator

@Ara225 Ara225 left a comment

Choose a reason for hiding this comment

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

Overall, this works just fine, but there are some important issues that are worth reviewing. You'll find that as your applications get more complex, there are more of these issues. Embrace them all learn the why, they're intresting and will help you grow your knowledge

@@ -3,8 +3,10 @@
DROP TABLE IF EXISTS videos CASCADE;

CREATE TABLE videos (
id SERIAL PRIMARY KEY,
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

return res.status(422).json({ message: "Title field is required" });
}
if (!req.body.src) {
return res.status(422).json({ message: "src field is required" });
Copy link
Collaborator

Choose a reason for hiding this comment

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

It is good to see you using HTTP status codes other than 200 and thinking carefully about the right one to use. However, it's worth being aware that there are conventions - 400 bad request is usually used for this situation. It makes it very clear that the problem is on the users side.

The message is a nice touch and would be much appreciated when debugging!

return res.status(422).json({ message: "src field is required" });
}
const result = await db.query(
`INSERT INTO videos (title,src) VALUES ('${req.body.title}','${req.body.src}') RETURNING id`
Copy link
Collaborator

Choose a reason for hiding this comment

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

This line works, but sadly it's horribly insecure thanks to SQL injection. Anyone could delete your videos table just by sending a src with a value like this:
Fakesrc');DROP TABLE videos;

I don't know if Kieth mentioned this, and even if he did, it's easy not to make the connection so there’s no shame in having made the mistake. However, you need to get into the habit of doing the right thing have a look at https://stackoverflow.com/questions/58174695/prevent-sql-injection-with-nodejs-and-postgres

`INSERT INTO videos (title,src) VALUES ('${req.body.title}','${req.body.src}') RETURNING id`
);
const newVideoId = result.rows[0].id;
res.status(200).json({ success: true, data: { id: newVideoId } });
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's good that you're sending back the video ID that's extremely useful 👍

app.use((err, req, res, next) => {
res.status(500).json({ message: "An unexpected error occurred" });
next(err);
});
Copy link
Collaborator

Choose a reason for hiding this comment

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

So good to see error handling and the health endpoint but there's a major issue with doing it just in this file. None of the stuff in this file ever runs in Netifly (the starting point for Netlifly is functions/app.mjs), so this code only works on your local machine. If this is intentional, that's fine, but please add a comment to clarify :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants