File size: 1,882 Bytes
bcce530
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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 })
    }
}