Iot / src /app /api /gallery /route.ts
veltrixcode's picture
Upload 74 files
64fa001 verified
Raw History Blame Contribute Delete
1.46 kB
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 }
)
}
}