File size: 1,460 Bytes
ebde876
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { prisma } from "@/lib/prisma"
import { NextResponse } from "next/server"

export async function GET(request: Request) {
    try {
        const { searchParams } = new URL(request.url)
        const cursor = searchParams.get("cursor")
        const limit = parseInt(searchParams.get("limit") || "20")

        const generations = await prisma.generation.findMany({
            where: { isPublic: true },
            orderBy: [{ createdAt: "desc" }, { id: "desc" }],
            take: limit + 1, // Fetch one extra to check for next page
            cursor: cursor ? { id: cursor } : undefined,
            skip: cursor ? 1 : 0, // Skip the cursor itself if present
            include: {
                user: {
                    select: {
                        name: true,
                        username: true,
                        image: true,
                    },
                },
            },
        })

        let nextCursor: string | null = null
        if (generations.length > limit) {
            const nextItem = generations.pop() // Remove the extra item
            nextCursor = nextItem?.id || null
        }

        return NextResponse.json({
            data: generations,
            nextCursor,
        })
    } catch (error) {
        console.error("Gallery fetch error:", error)
        return NextResponse.json(
            { error: "Failed to fetch gallery images" },
            { status: 500 }
        )
    }
}