Spaces:
Sleeping
Sleeping
| import { NextRequest, NextResponse } from "next/server"; | |
| import { isAuthenticated } from "@/lib/auth"; | |
| import Project from "@/models/Project"; | |
| import dbConnect from "@/lib/mongodb"; | |
| import { COLORS } from "@/lib/utils"; | |
| export async function GET() { | |
| const user = await isAuthenticated(); | |
| if (user instanceof NextResponse || !user) { | |
| return NextResponse.json({ message: "Unauthorized" }, { status: 401 }); | |
| } | |
| await dbConnect(); | |
| const projects = await Project.find({ | |
| user_id: user?.id, | |
| }) | |
| .sort({ _createdAt: -1 }) | |
| .limit(100) | |
| .lean(); | |
| if (!projects) { | |
| return NextResponse.json( | |
| { | |
| ok: false, | |
| projects: [], | |
| }, | |
| { status: 404 } | |
| ); | |
| } | |
| return NextResponse.json( | |
| { | |
| ok: true, | |
| projects, | |
| }, | |
| { status: 200 } | |
| ); | |
| } | |
| export async function POST(request: NextRequest) { | |
| const user = await isAuthenticated(); | |
| if (user instanceof NextResponse || !user) { | |
| return NextResponse.json({ message: "Unauthorized" }, { status: 401 }); | |
| } | |
| const { title, pages, prompts } = await request.json(); | |
| if (!title || !pages || pages.length === 0) { | |
| return NextResponse.json( | |
| { message: "Title and HTML content are required.", ok: false }, | |
| { status: 400 } | |
| ); | |
| } | |
| await dbConnect(); | |
| try { | |
| const newTitle = title | |
| .toLowerCase() | |
| .replace(/[^a-z0-9]+/g, "-") | |
| .split("-") | |
| .filter(Boolean) | |
| .join("-") | |
| .slice(0, 96); | |
| // Generate project metadata without HuggingFace Hub | |
| const colorFrom = COLORS[Math.floor(Math.random() * COLORS.length)]; | |
| const colorTo = COLORS[Math.floor(Math.random() * COLORS.length)]; | |
| // Store project in database with Gemini-generated content | |
| const project = await Project.create({ | |
| user_id: user.id, | |
| space_id: `${user.name}/${newTitle}`, | |
| prompts, | |
| pages, // Store pages directly in database | |
| metadata: { | |
| title: newTitle, | |
| colorFrom, | |
| colorTo, | |
| sdk: "static", | |
| tags: ["rabu-2", "gemini-ai"], | |
| } | |
| }); | |
| const path = `${user.name}/${newTitle}`; | |
| return NextResponse.json({ project, path, ok: true }, { status: 201 }); | |
| } catch (err: unknown) { | |
| const errorMessage = err instanceof Error ? err.message : 'An unknown error occurred'; | |
| return NextResponse.json( | |
| { error: errorMessage, ok: false }, | |
| { status: 500 } | |
| ); | |
| } | |
| } | |