Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
zelihapala committed May 25, 2024
1 parent 3d7ac5b commit 676ab6e
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions server/api.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Router } from "express";
import db from "./db.js";

const router = Router();

router.get("/videos", async (_, res) => {
Expand All @@ -14,41 +15,48 @@ router.get("/videos", async (_, res) => {
});

router.post("/videos", async (req, res) => {
if (!req.body.title) {
const { title, src } = req.body;

if (!title) {
return res.status(422).json({ message: "Title field is required" });
}
if (!req.body.src) {
if (!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 } });

try {
const result = await db.query(
"INSERT INTO videos (title, src) VALUES ($1, $2) RETURNING id",
[title, src]
);

const newVideoId = result.rows[0].id;
res.status(200).json({ success: true, data: { id: newVideoId } });
} catch (error) {
res
.status(500)
.json({ success: false, error: "Failed to insert video into database" });
}
});

router.delete("/videos/:id", async (req, res) => {
const videoId = req.params.id;

try {
const checkQuery = await db.query("SELECT * FROM videos WHERE id = $1", [
`${videoId}`,
videoId,
]);
if (checkQuery.rows.length === 0) {
return res.status(404).json({
message: "Video not found",
});
return res.status(404).json({ message: "Video not found" });
}

const deleteQuery = await db.query("DELETE FROM videos WHERE id = $1", [
videoId,
]);

await db.query("DELETE FROM videos WHERE id = $1", [videoId]);
return res.status(204).end();
} catch (error) {
console.error(error);
res
.status(500)
.json({ message: "Internal server error", error: this.error });
.json({ message: "An error occurred while deleting the video" });
}
});

Expand Down

0 comments on commit 676ab6e

Please sign in to comment.