project-standalo-sonic-cloud/app/api/search/route.ts

130 lines
2.6 KiB
TypeScript

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 query = searchParams.get('q')
const limit = parseInt(searchParams.get('limit') || '20')
if (!query || query.trim().length === 0) {
return NextResponse.json({ error: 'Search query is required' }, { status: 400 })
}
const lowerQuery = query.toLowerCase()
// Search songs - SQLite uses LIKE for case-insensitive matching with LOWER()
const songs = await prisma.song.findMany({
where: {
isPublic: true,
OR: [
{
title: {
contains: lowerQuery,
},
},
{
description: {
contains: lowerQuery,
},
},
],
},
include: {
artist: {
select: {
id: true,
name: true,
slug: true,
verified: true,
},
},
album: {
select: {
id: true,
title: true,
slug: true,
coverUrl: true,
},
},
genres: {
include: {
genre: true,
},
},
},
take: limit,
})
// Search artists
const artists = await prisma.artist.findMany({
where: {
OR: [
{
name: {
contains: lowerQuery,
},
},
{
bio: {
contains: lowerQuery,
},
},
],
},
include: {
_count: {
select: {
songs: true,
albums: true,
},
},
},
take: limit,
})
// Search albums
const albums = await prisma.album.findMany({
where: {
OR: [
{
title: {
contains: lowerQuery,
},
},
{
description: {
contains: lowerQuery,
},
},
],
},
include: {
artist: {
select: {
id: true,
name: true,
slug: true,
verified: true,
},
},
_count: {
select: {
songs: true,
},
},
},
take: limit,
})
return NextResponse.json({
songs,
artists,
albums,
})
} catch (error) {
console.error('Error searching:', error)
return NextResponse.json({ error: 'Failed to perform search' }, { status: 500 })
}
}