Skip to content

Commit bc1e5ab

Browse files
Merge pull request #236 from ReDI-School/Feature/clean-comment-integration
Feature/clean comment integration
2 parents 7006c75 + 49767c7 commit bc1e5ab

File tree

13 files changed

+768
-424
lines changed

13 files changed

+768
-424
lines changed

backend/prisma/schema.prisma

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,7 @@ model Comment {
8686
content String
8787
pin Pin @relation(fields: [pinId], references: [id])
8888
user User @relation(fields: [userId], references: [id])
89-
90-
@@unique([userId, pinId])
91-
}
89+
}
9290

9391
model SavedPin {
9492
id Int @id @default(autoincrement())

backend/scripts/clean-categories.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { PrismaClient } from "@prisma/client";
33
const prisma = new PrismaClient();
44

55
async function main() {
6-
console.log("Cleaning up categories...");
6+
// console.log("Cleaning up categories...");
77

88
// First, remove category references from pins
99
await prisma.pin.updateMany({
@@ -15,7 +15,7 @@ async function main() {
1515
// Then delete all categories
1616
await prisma.category.deleteMany({});
1717

18-
console.log("Categories cleaned up successfully!");
18+
// console.log("Categories cleaned up successfully!");
1919
}
2020

2121
main()

backend/scripts/seed-categories.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const categories = [
6868
];
6969

7070
async function main() {
71-
console.log("Starting to seed categories...");
71+
// console.log("Starting to seed categories...");
7272

7373
for (const category of categories) {
7474
// Check if category already exists to avoid duplicates
@@ -85,13 +85,13 @@ async function main() {
8585
const created = await prisma.category.create({
8686
data: category
8787
});
88-
console.log(`Created category: ${created.title}`);
88+
console.info(`Created category: ${created.title}`);
8989
} else {
90-
console.log(`Category "${category.title}" already exists, skipping`);
90+
console.info(`Category "${category.title}" already exists, skipping`);
9191
}
9292
}
9393

94-
console.log("Category seeding completed!");
94+
// console.log("Category seeding completed!");
9595
}
9696

9797
main()

backend/src/controllers/commentsController.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ export const getComments = async (req, res) => {
1616
const comments = await prisma.comment.findMany({
1717
where: {
1818
pinId: Number(pinId)
19+
},
20+
include: {
21+
user: {
22+
// Include the related User model
23+
select: {
24+
email: true,
25+
id: true,
26+
name: true
27+
}
28+
}
1929
}
2030
});
2131

@@ -28,14 +38,23 @@ export const getComments = async (req, res) => {
2838
};
2939

3040
export const addComment = async (req, res) => {
31-
const { pinId, userId, content } = req.body;
41+
const { pinId, content } = req.body;
42+
const userId = req.user?.id;
43+
44+
// Debug information
45+
// console.log("Received comment request:", { pinId, content, userId });
3246

3347
// Validate required fields
34-
if (!pinId || !userId) {
48+
if (!pinId) {
49+
return res.status(BAD_REQUEST).json({ error: "pinId is required." });
50+
}
51+
52+
if (!userId) {
3553
return res
3654
.status(BAD_REQUEST)
37-
.json({ error: "pinId and userId are required." });
55+
.json({ error: "You must be logged in to add a comment." });
3856
}
57+
3958
if (!content) {
4059
return res.status(BAD_REQUEST).json({ error: "Comment is empty." });
4160
}
@@ -62,6 +81,7 @@ export const addComment = async (req, res) => {
6281

6382
export const deleteComment = async (req, res) => {
6483
const { id } = req.body;
84+
// const userId = req.user?.id;
6585
try {
6686
await prisma.comment.delete({
6787
where: {

backend/src/routes/commentsRoute.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import {
55
addComment,
66
deleteComment
77
} from "../controllers/commentsController.js";
8+
import { protect } from "../middlewares/authMiddleware.js";
89

910
router.get("/", getComments);
10-
router.post("/", addComment);
11-
router.delete("/", deleteComment);
11+
router.post("/", protect, addComment);
12+
router.delete("/", protect, deleteComment);
1213

1314
export default router;

frontend/src/components/ExplorePageSection1/CardDetailPage.jsx

Lines changed: 69 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import { useState, useEffect } from "react";
1+
import { useEffect, useState } from "react";
22
import { useParams, Link } from "react-router-dom";
33
import Breadcrumb from "./Breadcrumb";
44
import styles from "./CardDetailPage.module.css";
55
import { fetchPinById } from "../../services/pinService";
6+
import ShopItem from "./ShopItem";
67

78
const CardDetailPage = () => {
89
const { id } = useParams();
910
const [pin, setPin] = useState(null);
1011
const [loading, setLoading] = useState(true);
1112
const [relatedPins, setRelatedPins] = useState([]);
12-
const [error, setError] = useState(null);
1313

1414
useEffect(() => {
1515
const loadPin = async () => {
@@ -18,106 +18,103 @@ const CardDetailPage = () => {
1818
const fetchedPin = await fetchPinById(id);
1919
setPin(fetchedPin);
2020

21-
// Load related pins
22-
try {
23-
const res = await fetch(
24-
`http://localhost:4000/api/pins/${id}/related`
25-
);
26-
if (res.ok) {
27-
const related = await res.json();
28-
setRelatedPins(related);
29-
} else {
30-
console.error("Could not load related pins");
31-
setRelatedPins([]);
32-
}
33-
} catch (error) {
34-
console.error("Error loading related pins:", error);
21+
// Load related pins (optional)
22+
23+
const res = await fetch(`/api/pins/${id}/related`);
24+
if (res.ok) {
25+
const related = await res.json();
26+
setRelatedPins(related);
27+
} else {
28+
console.error("Could not load related pins");
3529
setRelatedPins([]);
3630
}
3731
} catch (error) {
38-
console.error("Error loading pin:", error);
39-
setError("Failed to load pin details");
32+
console.error("Error loading pin or related pins:", error);
4033
} finally {
4134
setLoading(false);
4235
}
4336
};
4437
loadPin();
4538
}, [id]);
4639

40+
const categories = [
41+
{ name: "Explore", link: "/explore" },
42+
{ name: "Electronics", link: "/electronics" },
43+
{ name: "Cell Phones And Accessories", link: "/phones-accessories" },
44+
{ name: "Phone Accessories", link: "/phone-accessories" }
45+
];
46+
4747
if (loading) {
4848
return <div className={styles.cardDetailPage}>Loading...</div>;
4949
}
5050

51-
if (error) {
52-
return <div className={styles.cardDetailPage}>{error}</div>;
53-
}
54-
5551
if (!pin) {
5652
return <div className={styles.cardDetailPage}>Pin not found!</div>;
5753
}
5854

59-
const breadcrumbItems = [
60-
{ name: "Explore", link: "/explore" },
61-
{
62-
name: pin.category?.title || "Category",
63-
link: `/category/${pin.category?.id}`
64-
}
65-
];
66-
6755
return (
68-
<div className={styles.cardDetailPage}>
69-
<Breadcrumb categories={breadcrumbItems} />
56+
<>
57+
<div className={styles.cardDetailPage}>
58+
<Breadcrumb categories={categories} />
59+
{/* Show the ShopItem component here */}
60+
61+
<ShopItem imageSrc={pin.imageUrl} />
62+
63+
{/*
7064
<div className={styles.cardImageContainer}>
7165
<img src={pin.imageUrl} alt={pin.altText || pin.title} />
7266
<div className={styles.cardOverlay}>
7367
<h1>{pin.title}</h1>
7468
<p>{pin.description}</p>
7569
</div>
7670
</div>
77-
<h3>{pin.tags?.map(tag => tag.name).join(", ")}</h3>
71+
*/}
7872

79-
<div className={styles.galleryContainer}>
80-
<div className={styles.gallery}>
81-
{relatedPins.length > 0 ? (
82-
relatedPins.map(pin => (
83-
<div key={pin.id} className={styles.imageContainer}>
84-
<div className={styles.imageWrapper}>
85-
<img
86-
src={pin.imageUrl}
87-
alt={pin.altText || pin.title}
88-
className={styles.image}
89-
/>
90-
<Link to={`/pin/${pin.id}`} className={styles.overlay}>
91-
<span className={styles.overlayText}>Open</span>
92-
<div className={styles.overlayButtons}>
93-
<img
94-
src="/images/share-icon.svg"
95-
alt="Share"
96-
className={styles.shareIcon}
97-
/>
98-
<img
99-
src="/image/more-icon.svg"
100-
alt="More"
101-
className={styles.moreIcon}
102-
/>
103-
</div>
104-
</Link>
105-
</div>
106-
<div className={styles.tags}>
107-
{pin.tags?.map((tag, tagIndex) => (
108-
<div key={tagIndex} className={styles.tag}>
109-
{tag.name}
110-
</div>
111-
))}
73+
<h3>{pin.tags?.map(tag => tag.name).join(", ")}</h3>
74+
75+
<div className={styles.galleryContainer}>
76+
<div className={styles.gallery}>
77+
{relatedPins.length > 0 ? (
78+
relatedPins.map(pin => (
79+
<div key={pin.id} className={styles.imageContainer}>
80+
<div className={styles.imageWrapper}>
81+
<img
82+
src={pin.imageUrl}
83+
alt={pin.altText || pin.title}
84+
className={styles.image}
85+
/>
86+
<Link to={`/pin/${pin.id}`} className={styles.overlay}>
87+
<span className={styles.overlayText}>Open</span>
88+
<div className={styles.overlayButtons}>
89+
<img
90+
src="/images/share-icon.svg"
91+
alt="Share"
92+
className={styles.shareIcon}
93+
/>
94+
<img
95+
src="/image/more-icon.svg"
96+
alt="More"
97+
className={styles.moreIcon}
98+
/>
99+
</div>
100+
</Link>
101+
</div>
102+
<div className={styles.tags}>
103+
{pin.tags?.map((tag, tagIndex) => (
104+
<div key={tagIndex} className={styles.tag}>
105+
{tag.name}
106+
</div>
107+
))}
108+
</div>
112109
</div>
113-
</div>
114-
))
115-
) : (
116-
<p>No related pins found</p>
117-
)}
110+
))
111+
) : (
112+
<p>No related pins found</p>
113+
)}
114+
</div>
118115
</div>
119116
</div>
120-
</div>
117+
</>
121118
);
122119
};
123120

0 commit comments

Comments
 (0)