File size: 2,772 Bytes
101ebaa 6593c28 101ebaa f15ff24 101ebaa 6593c28 101ebaa f15ff24 101ebaa 6593c28 101ebaa |
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
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 });
}
}
|