@@ -3,41 +3,98 @@ import { z } from "zod";
33import { NameIdSchema } from "@/lib/validations" ;
44import { getOrgIdFromNameId , getContestIdFromNameId } from "@/app/api/service" ;
55import * as problemService from "./service" ;
6+ import { db } from "@/db/drizzle" ;
7+ import { problems , contestProblems , contests , testCases } from "@/db/schema" ;
8+ import { eq , and } from "drizzle-orm" ;
9+ import { IdSchema } from "@/app/api/types" ;
610
711export async function GET (
8- _req : NextRequest ,
12+ request : NextRequest ,
913 {
1014 params,
1115 } : { params : { orgId : string ; contestId : string ; problemId : string } } ,
1216) {
1317 try {
14- const orgId = await getOrgIdFromNameId ( NameIdSchema . parse ( params . orgId ) ) ;
15- const contestId = await getContestIdFromNameId (
16- orgId ,
17- NameIdSchema . parse ( params . contestId ) ,
18- ) ;
19- const problemCode = NameIdSchema . parse ( params . problemId ) ;
18+ // Parse and validate parameters
19+ const orgNameId = NameIdSchema . parse ( params . orgId ) ;
20+ const contestNameId = NameIdSchema . parse ( params . contestId ) ;
21+ const problemId = params . problemId ;
22+
23+ // Get numeric orgId
24+ const orgId = await getOrgIdFromNameId ( orgNameId ) ;
25+
26+ // Find the contest by nameId
27+ const contestResult = await db
28+ . select ( { id : contests . id } )
29+ . from ( contests )
30+ . where (
31+ and (
32+ eq ( contests . nameId , contestNameId ) ,
33+ eq ( contests . organizerId , orgId ) ,
34+ eq ( contests . organizerKind , "org" ) ,
35+ ) ,
36+ )
37+ . limit ( 1 ) ;
38+
39+ if ( contestResult . length === 0 ) {
40+ return NextResponse . json (
41+ { message : "Contest not found" } ,
42+ { status : 404 } ,
43+ ) ;
44+ }
45+
46+ const contestId = contestResult [ 0 ] . id ;
47+
48+ // Find the problem and its associated contest problem
49+ const problemResult = await db
50+ . select ( {
51+ problem : problems ,
52+ contestProblem : contestProblems ,
53+ } )
54+ . from ( problems )
55+ . innerJoin (
56+ contestProblems ,
57+ and (
58+ eq ( contestProblems . problemId , problems . id ) ,
59+ eq ( contestProblems . contestId , contestId ) ,
60+ ) ,
61+ )
62+ . where ( eq ( problems . code , problemId ) )
63+ . limit ( 1 ) ;
64+
65+ if ( problemResult . length === 0 ) {
66+ return NextResponse . json (
67+ { message : "Problem not found in this contest" } ,
68+ { status : 404 } ,
69+ ) ;
70+ }
2071
21- const problem = await problemService . getContestProblem (
72+ // Get test cases for the problem
73+ const testCasesResult = await db
74+ . select ( )
75+ . from ( testCases )
76+ . where ( eq ( testCases . problemId , problemResult [ 0 ] . problem . id ) ) ;
77+
78+ // Combine the data
79+ const problem = {
80+ ...problemResult [ 0 ] . problem ,
81+ contestProblemId : problemResult [ 0 ] . contestProblem . id ,
82+ testCases : testCasesResult . map ( ( tc ) => ( {
83+ input : tc . input ,
84+ output : tc . output ,
85+ kind : tc . kind ,
86+ } ) ) ,
87+ orgId,
2288 contestId,
23- problemCode ,
24- ) ;
89+ } ;
90+
2591 return NextResponse . json ( problem ) ;
2692 } catch ( error ) {
2793 if ( error instanceof z . ZodError ) {
28- return NextResponse . json ( { error : error . errors } , { status : 400 } ) ;
29- }
30- if ( error instanceof Error ) {
31- if (
32- error . message === "Organization not found" ||
33- error . message === "Contest not found" ||
34- error . message === "Problem not found"
35- ) {
36- return NextResponse . json ( { error : error . message } , { status : 404 } ) ;
37- }
94+ return NextResponse . json ( { errors : error . errors } , { status : 400 } ) ;
3895 }
3996 return NextResponse . json (
40- { error : "Failed to fetch contest problem" } ,
97+ { message : "Failed to fetch problem" } ,
4198 { status : 500 } ,
4299 ) ;
43100 }
0 commit comments