feat: add product review and rating system with schema update
This commit is contained in:
1 parent
091eeef7d8
commit
e4f7951b34
5 files changed
+230
-1
No files matched your search
@@ -0,0 +1,49 @@
|
||||
import { Elysia, t } from "elysia";
|
||||
import prisma from "../lib/prisma";
|
||||
|
||||
export const reviewsRoute = new Elysia({ prefix: "/reviews" })
|
||||
.get("/product/:productId", async ({ params: { productId } }) => {
|
||||
return await prisma.review.findMany({
|
||||
where: {
|
||||
productId: Number(productId),
|
||||
isPublished: true
|
||||
},
|
||||
orderBy: { createdAt: 'desc' },
|
||||
include: {
|
||||
customer: {
|
||||
select: {
|
||||
displayName: true,
|
||||
pictureUrl: true
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
})
|
||||
.post("/", async ({ body, set }) => {
|
||||
try {
|
||||
const data = body as any;
|
||||
const review = await prisma.review.create({
|
||||
data: {
|
||||
rating: data.rating,
|
||||
comment: data.comment,
|
||||
productId: data.productId,
|
||||
customerId: data.customerId || null,
|
||||
customerName: data.customerName || "Anonymous",
|
||||
isPublished: true
|
||||
}
|
||||
});
|
||||
return review;
|
||||
} catch (err) {
|
||||
console.error("[Reviews] Error:", err);
|
||||
set.status = 500;
|
||||
return { error: "Failed to create review" };
|
||||
}
|
||||
}, {
|
||||
body: t.Object({
|
||||
rating: t.Number(),
|
||||
comment: t.Optional(t.String()),
|
||||
productId: t.Number(),
|
||||
customerId: t.Optional(t.Nullable(t.Number())),
|
||||
customerName: t.Optional(t.String())
|
||||
})
|
||||
});
|
||||
Reference in new issue
Block a user