Skip to content

Commit 712752e

Browse files
Merge pull request #190 from ReDI-School/feature/comments-endpoint
endpoint Comments
2 parents a642652 + 58baf46 commit 712752e

File tree

7 files changed

+135
-9
lines changed

7 files changed

+135
-9
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-- CreateTable
2+
CREATE TABLE "Comment" (
3+
"id" SERIAL NOT NULL,
4+
"userId" TEXT NOT NULL,
5+
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
6+
"pinId" INTEGER NOT NULL,
7+
8+
CONSTRAINT "Comment_pkey" PRIMARY KEY ("id")
9+
);
10+
11+
-- CreateIndex
12+
CREATE UNIQUE INDEX "Comment_userId_pinId_key" ON "Comment"("userId", "pinId");
13+
14+
-- AddForeignKey
15+
ALTER TABLE "Comment" ADD CONSTRAINT "Comment_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
16+
17+
-- AddForeignKey
18+
ALTER TABLE "Comment" ADD CONSTRAINT "Comment_pinId_fkey" FOREIGN KEY ("pinId") REFERENCES "Pin"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*
2+
Warnings:
3+
4+
- Added the required column `msg` to the `Comment` table without a default value. This is not possible if the table is not empty.
5+
6+
*/
7+
-- AlterTable
8+
ALTER TABLE "Comment" ADD COLUMN "msg" TEXT NOT NULL;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
Warnings:
3+
4+
- You are about to drop the column `msg` on the `Comment` table. All the data in the column will be lost.
5+
- Added the required column `content` to the `Comment` table without a default value. This is not possible if the table is not empty.
6+
7+
*/
8+
-- AlterTable
9+
ALTER TABLE "Comment" DROP COLUMN "msg",
10+
ADD COLUMN "content" TEXT NOT NULL;

backend/prisma/schema.prisma

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,20 @@ model Pin {
4040
imageUrl String
4141
description String?
4242
createdAt DateTime @default(now())
43-
4443
author User @relation(fields: [authorId], references: [id])
4544
authorId String
46-
4745
reactions Reaction[]
4846
comments Comment[]
4947
}
5048

5149
model Comment {
52-
id Int @id @default(autoincrement())
53-
content String
50+
id Int @id @default(autoincrement())
51+
user User @relation(fields: [userId], references: [id])
52+
userId String
5453
createdAt DateTime @default(now())
54+
pin Pin @relation(fields: [pinId], references: [id])
55+
pinId Int
56+
content String
5557
56-
user User @relation(fields: [userId], references: [id])
57-
userId String
58-
59-
pin Pin @relation(fields: [pinId], references: [id])
60-
pinId Int
58+
@@unique([userId, pinId]) // prevent duplicate Comments
6159
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import prisma from "../prisma/client.js";
2+
import {
3+
BAD_REQUEST,
4+
CREATED,
5+
INTERNAL_SERVER_ERROR,
6+
OK
7+
} from "../constants/http.js";
8+
9+
export const getComments = async (req, res) => {
10+
const { pinId } = req.query;
11+
try {
12+
if (!pinId) {
13+
return res.status(BAD_REQUEST).json({ error: "pinId is required." });
14+
}
15+
16+
const comments = await prisma.comment.findMany({
17+
where: {
18+
pinId: Number(pinId)
19+
}
20+
});
21+
22+
res.json({ comments });
23+
} catch (err) {
24+
res
25+
.status(INTERNAL_SERVER_ERROR)
26+
.json({ error: "Something went wrong in loading comments." });
27+
}
28+
};
29+
30+
export const addComment = async (req, res) => {
31+
const { pinId, userId, content } = req.body;
32+
33+
// Validate required fields
34+
if (!pinId || !userId) {
35+
return res
36+
.status(BAD_REQUEST)
37+
.json({ error: "pinId and userId are required." });
38+
}
39+
if (!content) {
40+
return res.status(BAD_REQUEST).json({ error: "Comment is empty." });
41+
}
42+
43+
try {
44+
// Create a comment
45+
const comment = await prisma.comment.create({
46+
data: {
47+
userId: String(userId),
48+
pinId: Number(pinId),
49+
content: String(content)
50+
}
51+
});
52+
53+
res.status(CREATED).json({
54+
message: "Comment added.",
55+
comment
56+
});
57+
} catch (err) {
58+
console.error("Add Comment Error:", err);
59+
res.status(INTERNAL_SERVER_ERROR).json({ error: "Failed to add comment." });
60+
}
61+
};
62+
63+
export const deleteComment = async (req, res) => {
64+
const { id } = req.body;
65+
try {
66+
await prisma.comment.delete({
67+
where: {
68+
id: Number(id)
69+
}
70+
});
71+
res.status(OK).json({ message: "Comment removed." });
72+
} catch (err) {
73+
res
74+
.status(INTERNAL_SERVER_ERROR)
75+
.json({ error: "Failed to delete comment." });
76+
}
77+
};

backend/src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import express from "express";
55
import cors from "cors";
66
import reactionsRoutes from "./routes/reactionsRoute.js";
77
import userRoutes from "./routes/userRoute.js";
8+
import commentsRoutes from "./routes/commentsRoute.js";
89
import { errorHandler } from "./middlewares/errorMiddleware.js";
910

1011
const app = express();
@@ -17,6 +18,7 @@ app.use(cors());
1718
// routes
1819
app.use("/api/reactions", reactionsRoutes);
1920
app.use("/api/user", userRoutes);
21+
app.use("/api/comments", commentsRoutes);
2022

2123
// get
2224
app.get("/", (req, res) => {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import express from "express";
2+
const router = express.Router();
3+
import {
4+
getComments,
5+
addComment,
6+
deleteComment
7+
} from "../controllers/commentsController.js";
8+
9+
router.get("/", getComments);
10+
router.post("/", addComment);
11+
router.delete("/", deleteComment);
12+
13+
export default router;

0 commit comments

Comments
 (0)