| import { NextRequest, NextResponse } from "next/server"; |
| import { |
| getDistillationRequests, |
| addDistillationRequest, |
| upvoteDistillation, |
| } from "@/lib/store"; |
| import { getOrCreateUserId, userCookieOptions } from "@/lib/userIdentity"; |
|
|
| export async function GET() { |
| try { |
| const requests = await getDistillationRequests(); |
| return NextResponse.json(requests); |
| } catch (error) { |
| console.error("Error fetching distillation requests:", error); |
| return NextResponse.json({ error: "Failed to fetch requests" }, { status: 500 }); |
| } |
| } |
|
|
| export async function POST(request: NextRequest) { |
| try { |
| const { userId, shouldSetCookie } = getOrCreateUserId(request); |
| const body = await request.json(); |
| const { sourceDataset, studentModel, submitterName, additionalNotes } = body; |
|
|
| if (!sourceDataset || !studentModel) { |
| return NextResponse.json( |
| { error: "Source dataset and student model are required" }, |
| { status: 400 } |
| ); |
| } |
|
|
| const newRequest = await addDistillationRequest({ |
| sourceDataset, |
| studentModel, |
| submitterName: typeof submitterName === "string" && submitterName.trim() ? submitterName.trim() : undefined, |
| additionalNotes: additionalNotes || "", |
| ownerId: userId, |
| }); |
|
|
| const response = NextResponse.json(newRequest, { status: 201 }); |
| if (shouldSetCookie) { |
| response.cookies.set({ ...userCookieOptions(), value: userId }); |
| } |
| return response; |
| } catch (error) { |
| console.error("Error creating distillation request:", error); |
| return NextResponse.json({ error: "Failed to create request" }, { status: 500 }); |
| } |
| } |
|
|
| export async function PATCH(request: NextRequest) { |
| try { |
| const body = await request.json(); |
| const { id } = body; |
|
|
| if (!id) { |
| return NextResponse.json({ error: "Request ID is required" }, { status: 400 }); |
| } |
|
|
| const ip = request.headers.get("x-forwarded-for") || |
| request.headers.get("x-real-ip") || |
| "anonymous"; |
|
|
| const result = await upvoteDistillation(id, ip); |
|
|
| if (!result.success) { |
| return NextResponse.json( |
| { error: "Request not found", upvotes: result.upvotes }, |
| { status: 404 } |
| ); |
| } |
|
|
| return NextResponse.json({ |
| success: true, |
| upvotes: result.upvotes, |
| action: result.action, |
| }); |
| } catch (error) { |
| console.error("Error upvoting distillation request:", error); |
| return NextResponse.json({ error: "Failed to upvote" }, { status: 500 }); |
| } |
| } |
|
|