mvp
This commit is contained in:
41
src/app/api/search/route.ts
Normal file
41
src/app/api/search/route.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const { searchParams } = new URL(req.url)
|
||||
const q = searchParams.get('q') || ''
|
||||
const type = searchParams.get('type')
|
||||
const tag = searchParams.get('tag')
|
||||
|
||||
const where: Record<string, unknown> = {}
|
||||
|
||||
if (q) {
|
||||
where.OR = [
|
||||
{ title: { contains: q } },
|
||||
{ content: { contains: q } },
|
||||
]
|
||||
}
|
||||
|
||||
if (type) {
|
||||
where.type = type
|
||||
}
|
||||
|
||||
if (tag) {
|
||||
where.tags = {
|
||||
some: {
|
||||
tag: { name: tag },
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
const notes = await prisma.note.findMany({
|
||||
where,
|
||||
include: { tags: { include: { tag: true } } },
|
||||
orderBy: [
|
||||
{ isPinned: 'desc' },
|
||||
{ updatedAt: 'desc' },
|
||||
],
|
||||
})
|
||||
|
||||
return NextResponse.json(notes)
|
||||
}
|
||||
Reference in New Issue
Block a user