import { NextRequest, NextResponse } from 'next/server'; import { prisma } from '@/lib/prisma'; export async function GET(request: NextRequest) { try { const { searchParams } = new URL(request.url); const slug = searchParams.get('slug'); const search = searchParams.get('search'); const category = searchParams.get('category'); if (slug) { const project = await prisma.project.findFirst({ where: { published: true, slug, }, orderBy: { createdAt: 'desc' }, }); return NextResponse.json({ projects: project ? [project] : [] }); } if (search) { // General search const projects = await prisma.project.findMany({ where: { published: true, OR: [ { title: { contains: search, mode: 'insensitive' } }, { description: { contains: search, mode: 'insensitive' } }, { tags: { hasSome: [search] } }, { content: { contains: search, mode: 'insensitive' } } ] }, orderBy: { createdAt: 'desc' } }); return NextResponse.json({ projects }); } if (category && category !== 'All') { // Filter by category const projects = await prisma.project.findMany({ where: { published: true, category: category }, orderBy: { createdAt: 'desc' } }); return NextResponse.json({ projects }); } // Return all published projects if no specific search const projects = await prisma.project.findMany({ where: { published: true }, orderBy: { createdAt: 'desc' } }); return NextResponse.json({ projects }); } catch (error) { console.error('Error searching projects:', error); return NextResponse.json( { error: 'Failed to search projects' }, { status: 500 } ); } }