File size: 2,816 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
84
85
import { NextRequest, NextResponse } from "next/server";
import {
    getDatasetRequests,
    addDatasetRequest,
    upvoteDataset,
} from "@/lib/store";
import { getOrCreateUserId, userCookieOptions } from "@/lib/userIdentity";

export async function GET() {
    try {
        const requests = await getDatasetRequests();
        return NextResponse.json(requests);
    } catch (error) {
        console.error("Error fetching dataset 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 { sourceModel, submitterName, datasetSize, reasoningDepth, topics, additionalNotes } = body;

        if (!sourceModel) {
            return NextResponse.json(
                { error: "Source model is required" },
                { status: 400 }
            );
        }

        const newRequest = await addDatasetRequest({
            sourceModel,
            submitterName: typeof submitterName === "string" && submitterName.trim() ? submitterName.trim() : undefined,
            datasetSize: datasetSize || "250x",
            reasoningDepth: reasoningDepth || "high",
            topics: topics || [],
            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 dataset 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 upvoteDataset(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 dataset request:", error);
        return NextResponse.json({ error: "Failed to upvote" }, { status: 500 });
    }
}