Spaces:
Configuration error
Configuration error
| import { NextRequest, NextResponse } from 'next/server' | |
| import prisma from '@/lib/prisma' | |
| // GET /api/comparisons - Get saved comparisons | |
| export async function GET(request: NextRequest) { | |
| const { searchParams } = new URL(request.url) | |
| const userId = searchParams.get('userId') | |
| const type = searchParams.get('type') // "thunderdome" or "performance" | |
| try { | |
| const where: any = {} | |
| if (userId) { | |
| where.userId = userId | |
| } | |
| if (type) { | |
| where.type = type | |
| } | |
| const comparisons = await prisma.savedComparison.findMany({ | |
| where, | |
| orderBy: { createdAt: 'desc' }, | |
| take: 50, | |
| }) | |
| return NextResponse.json({ comparisons }) | |
| } catch (error) { | |
| console.error('Error fetching comparisons:', error) | |
| return NextResponse.json({ error: 'Internal server error' }, { status: 500 }) | |
| } | |
| } | |
| // POST /api/comparisons - Save a comparison | |
| export async function POST(request: NextRequest) { | |
| try { | |
| const body = await request.json() | |
| const { userId, name, type, prompt, results, winner } = body | |
| if (!type || !prompt || !results) { | |
| return NextResponse.json( | |
| { error: 'type, prompt, and results are required' }, | |
| { status: 400 } | |
| ) | |
| } | |
| const comparison = await prisma.savedComparison.create({ | |
| data: { | |
| type, | |
| userId: userId || null, | |
| name: name || null, | |
| prompt, | |
| results, | |
| winner: winner || null, | |
| } | |
| }) | |
| return NextResponse.json(comparison, { status: 201 }) | |
| } catch (error) { | |
| console.error('Error saving comparison:', error) | |
| return NextResponse.json({ error: 'Internal server error' }, { status: 500 }) | |
| } | |
| } | |