38 lines
865 B
TypeScript
38 lines
865 B
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { prisma } from '@/lib/prisma'
|
|
|
|
export async function GET(request: NextRequest, props: { params: Promise<{ slug: string }> }) {
|
|
const params = await props.params;
|
|
try {
|
|
const product = await prisma.product.findUnique({
|
|
where: {
|
|
slug: params.slug,
|
|
isActive: true
|
|
},
|
|
include: {
|
|
category: {
|
|
select: {
|
|
id: true,
|
|
name: true
|
|
}
|
|
}
|
|
},
|
|
})
|
|
|
|
if (!product) {
|
|
return NextResponse.json(
|
|
{ error: 'Product not found' },
|
|
{ status: 404 }
|
|
)
|
|
}
|
|
|
|
return NextResponse.json(product)
|
|
} catch (error) {
|
|
console.error('Error fetching product by slug:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch product' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|