Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { NextResponse } from 'next/server'
import { Prisma } from '@prisma/client'
import { prisma } from '@/lib/prisma'
// GET /api/products - Get all products with optional filters
export async function GET(request: Request) {
try {
const { searchParams } = new URL(request.url)
const category = searchParams.get('category')
const subcategory = searchParams.get('subcategory')
const q = searchParams.get('q')
const isNew = searchParams.get('isNew')
const isUsed = searchParams.get('isUsed')
const where: Prisma.ProductWhereInput = {}
Iif (category) where.category = category
Iif (subcategory) where.subcategory = subcategory
Iif (isNew === 'true') where.isNew = true
Iif (isUsed === 'true') where.isUsed = true
Iif (q) {
where.OR = [
{ name: { contains: q, mode: 'insensitive' } },
{ description: { contains: q, mode: 'insensitive' } },
{ shortDescription: { contains: q, mode: 'insensitive' } },
{ brand: { contains: q, mode: 'insensitive' } },
]
}
const products = await prisma.product.findMany({
where,
include: {
reviews: {
where: { status: 'approved' },
},
},
orderBy: { createdAt: 'desc' },
})
return NextResponse.json(products)
} catch (error) {
console.error('Error fetching products:', error)
return NextResponse.json({ error: 'Failed to fetch products' }, { status: 500 })
}
}
|