1- import * as problemService from "./service" ;
2- import { IdSchema } from "@/app/api/types" ;
3- import { addProblemSchema } from "@/lib/validations" ;
4-
5- // /app/api/orgs/[orgId]/contests/[contestId]/problems/route.ts
61import { NextRequest , NextResponse } from "next/server" ;
7- import { getOrgIdFromNameId , getContestIdFromNameId } from "@/app/api/service" ;
8- import { NameIdSchema } from "@/app/api/types" ;
92import { z } from "zod" ;
3+ import * as problemService from "./service" ;
4+ import { getOrgIdFromNameId , getContestIdFromNameId } from "@/app/api/service" ;
5+ import { addProblemSchema , NameIdSchema } from "@/lib/validations" ;
106
117export async function POST (
128 request : NextRequest ,
13- { params } : { params : { contestId : string ; orgId : string } } ,
9+ { params } : { params : { orgId : string ; contestId : string } } ,
1410) {
1511 try {
16- const contestId = IdSchema . parse ( params . contestId ) ;
12+ // Validate nameIds first
13+ const orgNameId = NameIdSchema . parse ( params . orgId ) ;
14+ const contestNameId = NameIdSchema . parse ( params . contestId ) ;
15+
16+ // Then get numeric IDs
17+ const orgId = await getOrgIdFromNameId ( orgNameId ) ;
18+ const contestId = await getContestIdFromNameId ( orgId , contestNameId ) ;
1719 const data = addProblemSchema . parse ( await request . json ( ) ) ;
1820
1921 const problem = await problemService . addProblemToContest (
22+ orgId ,
2023 contestId ,
21- data . problemId ,
24+ data . problemCode ,
2225 data . order ?? 0 ,
2326 ) ;
2427
25- return Response . json ( problem , { status : 201 } ) ;
28+ return NextResponse . json ( problem , { status : 201 } ) ;
2629 } catch ( error ) {
2730 if ( error instanceof z . ZodError ) {
28- return Response . json ( { error : error . errors } , { status : 400 } ) ;
31+ return NextResponse . json ( { error : error . errors } , { status : 400 } ) ;
2932 }
30- return Response . json (
33+ if ( error instanceof Error ) {
34+ if (
35+ error . message === "Organization not found" ||
36+ error . message === "Contest not found"
37+ ) {
38+ return NextResponse . json ( { error : error . message } , { status : 404 } ) ;
39+ }
40+ if ( error . message === "Problem not found" ) {
41+ return NextResponse . json ( { error : error . message } , { status : 404 } ) ;
42+ }
43+ if ( error . message === "Problem already added to contest" ) {
44+ return NextResponse . json ( { error : error . message } , { status : 409 } ) ;
45+ }
46+ }
47+ return NextResponse . json (
3148 { error : "Failed to add problem to contest" } ,
3249 { status : 500 } ,
3350 ) ;
@@ -47,21 +64,64 @@ export async function GET(
4764 const orgId = await getOrgIdFromNameId ( orgNameId ) ;
4865 const contestId = await getContestIdFromNameId ( orgId , contestNameId ) ;
4966
50- const problems = await problemService . getContestProblems ( contestId ) ;
67+ const problems = await problemService . getContestProblems ( orgId , contestId ) ;
5168 return NextResponse . json ( problems ) ;
5269 } catch ( error ) {
5370 if ( error instanceof z . ZodError ) {
54- return NextResponse . json ( { errors : error . errors } , { status : 400 } ) ;
71+ return NextResponse . json ( { error : error . errors } , { status : 400 } ) ;
72+ }
73+ if ( error instanceof Error ) {
74+ if (
75+ error . message === "Organization not found" ||
76+ error . message === "Contest not found"
77+ ) {
78+ return NextResponse . json ( { error : error . message } , { status : 404 } ) ;
79+ }
80+ }
81+ return NextResponse . json (
82+ { error : "Failed to fetch contest problems" } ,
83+ { status : 500 } ,
84+ ) ;
85+ }
86+ }
87+
88+ export async function DELETE (
89+ request : NextRequest ,
90+ {
91+ params,
92+ } : { params : { orgId : string ; contestId : string ; problemId : string } } ,
93+ ) {
94+ try {
95+ // Validate nameIds first
96+ const orgNameId = NameIdSchema . parse ( params . orgId ) ;
97+ const contestNameId = NameIdSchema . parse ( params . contestId ) ;
98+ const problemCode = NameIdSchema . parse ( params . problemId ) ;
99+
100+ // Then get numeric IDs
101+ const orgId = await getOrgIdFromNameId ( orgNameId ) ;
102+ const contestId = await getContestIdFromNameId ( orgId , contestNameId ) ;
103+
104+ await problemService . removeProblemFromContest (
105+ orgId ,
106+ contestId ,
107+ problemCode ,
108+ ) ;
109+ return new Response ( null , { status : 204 } ) ;
110+ } catch ( error ) {
111+ if ( error instanceof z . ZodError ) {
112+ return NextResponse . json ( { error : error . errors } , { status : 400 } ) ;
55113 }
56- if (
57- error instanceof Error &&
58- ( error . message === "Organization not found" ||
59- error . message === "Contest not found" )
60- ) {
61- return NextResponse . json ( { message : error . message } , { status : 404 } ) ;
114+ if ( error instanceof Error ) {
115+ if (
116+ error . message === "Organization not found" ||
117+ error . message === "Contest not found" ||
118+ error . message === "Problem not found"
119+ ) {
120+ return NextResponse . json ( { error : error . message } , { status : 404 } ) ;
121+ }
62122 }
63123 return NextResponse . json (
64- { message : "Failed to fetch contest problems " } ,
124+ { error : "Failed to remove problem from contest " } ,
65125 { status : 500 } ,
66126 ) ;
67127 }
0 commit comments