first commit
This commit is contained in:
37
app/api/products/[slug]/route.ts
Normal file
37
app/api/products/[slug]/route.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
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 }
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user