-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathroute.ts
More file actions
69 lines (63 loc) · 2.19 KB
/
route.ts
File metadata and controls
69 lines (63 loc) · 2.19 KB
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
import { createProblemSchema, createTestCaseSchema } from "@/lib/validations";
import * as problemService from "./service";
import { getOrgIdFromNameId } from "@/app/api/service";
import { NextRequest, NextResponse } from "next/server";
import { NameIdSchema } from "@/app/api/types";
import { problemSchema } from "@/lib/validations";
import { z } from "zod";
import { invalidateCacheKey } from "@/lib/cache/utils";
export async function GET(
_req: NextRequest,
{ params }: { params: { orgId: string } },
) {
try {
const orgId = await getOrgIdFromNameId(NameIdSchema.parse(params.orgId));
const problems = await problemService.getOrgProblems(orgId);
const validatedProblems = z.array(problemSchema).parse(problems);
return NextResponse.json(validatedProblems);
} catch (error) {
if (error instanceof z.ZodError) {
return NextResponse.json({ error: error.errors }, { status: 400 });
}
if (error instanceof Error && error.message === "Organization not found") {
return NextResponse.json({ error: error.message }, { status: 404 });
}
return NextResponse.json(
{ error: "Failed to fetch problems" },
{ status: 500 },
);
}
}
export async function POST(
request: NextRequest,
{ params }: { params: { orgId: string } },
) {
try {
const orgId = await getOrgIdFromNameId(NameIdSchema.parse(params.orgId));
const body = await request.json();
console.log("body", request.body);
const { testCases, ...problemData } = body;
console.log("testCases", testCases);
const validatedProblem = createProblemSchema.parse(body);
const validatedTestCases = z
.array(createTestCaseSchema)
.min(1)
.parse(testCases);
const problem = await problemService.createProblem(
orgId,
validatedProblem,
validatedTestCases,
);
await invalidateCacheKey(`org:problems:${orgId}`);
return NextResponse.json(problem, { status: 201 });
} catch (error) {
if (error instanceof z.ZodError) {
console.log(error.errors);
return NextResponse.json({ error: error.errors }, { status: 400 });
}
return NextResponse.json(
{ error: "Failed to create problem" },
{ status: 500 },
);
}
}