Skip to content

Commit

Permalink
Merge pull request #24 from RbAvci/RB/Post_new_video_endpoint
Browse files Browse the repository at this point in the history
NW6 | Rabia Avci | Full-Stack-Project | Week-2 | Post new video endpoint
  • Loading branch information
zelihapala authored May 16, 2024
2 parents fae9b84 + 7d1678d commit bd0d054
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 25 deletions.
6 changes: 3 additions & 3 deletions data/example_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
{
"id": 1,
"title": "Never Gonna Give You Up",
"url": "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
"src": "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
},
{
"id": 2,
"title": "The Coding Train",
"url": "https://www.youtube.com/watch?v=HerCR8bw_GE"
"src": "https://www.youtube.com/watch?v=HerCR8bw_GE"
},
{
"id": 3,
"title": "Mac & Cheese | Basics with Babish",
"url": "https://www.youtube.com/watch?v=FUeyrEN14Rk"
"src": "https://www.youtube.com/watch?v=FUeyrEN14Rk"
}
]
2 changes: 2 additions & 0 deletions db/initdb.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
DROP TABLE IF EXISTS videos CASCADE;

CREATE TABLE videos (
id SERIAL PRIMARY KEY,
title VARCHAR,
src VARCHAR

);

INSERT INTO videos (title,src) VALUES ('Never Gonna Give You Up','https://www.youtube.com/watch?v=dQw4w9WgXcQ');
Expand Down
25 changes: 7 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"dependencies": {
"cors": "^2.8.5",
"jest": "^29.7.0",
"nodemon": "^3.1.0",
"serverless-http": "^3.2.0"
}
}
14 changes: 12 additions & 2 deletions server/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,18 @@ router.get("/videos", async (_, res) => {
}
});

router.get("/health", async (_, res) => {
res.sendStatus(200);
router.post("/videos", async (req, res) => {
if (!req.body.title) {
return res.status(422).json({ message: "Title field is required" });
}
if (!req.body.src) {
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`
);
const newVideoId = result.rows[0].id;
res.status(200).json({ success: true, data: { id: newVideoId } });
});

export default router;
20 changes: 19 additions & 1 deletion server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,29 @@ import { fileURLToPath } from "node:url";
const app = express();

app.use(express.json());

// api calls are all under /api and are handled in api.js
app.use("/api", apiRouter);

app.use("/health", async (_, res) => {
res.sendStatus(200);
});
// everything that is not an API call is likely the frontend react app, so make sure we route the frontend app there.
// This will allow us to access the React frontend on the same link as the backend.

// Error handler for JSON parse errors
app.use((err, req, res, next) => {
if (err instanceof SyntaxError && err.status === 400 && "body" in err) {
return res.status(400).json({ message: "Invalid JSON format" });
}
next(err);
});

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

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const staticDir = path.join(__dirname, "static");
app.use(express.static(staticDir));
Expand Down
2 changes: 1 addition & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "module",
"main": "server.js",
"scripts": {
"dev": "node server.js",
"dev": "nodemon server.js",
"test": "NODE_OPTIONS='--experimental-vm-modules' jest"
},
"keywords": [],
Expand Down

0 comments on commit bd0d054

Please sign in to comment.