50 lines
1.3 KiB
TypeScript
50 lines
1.3 KiB
TypeScript
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())
|
|
})
|
|
});
|