This repository has been archived by the owner on Jan 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api:submission:language): add endpoint `/api/submissions/languag…
…e/[problemNum]` ## what - add endpoint `/api/submissions/language/[problemNum]` - get submissions language count of a problem using problem number - if invalid `problem number` is given, a response of 400 will be returned - if the problem doesn't exist, a response of 404 will be returned - fetch submissions of problem number - count submission language ## how ## why ## where - ./src/app/api/submissions/language/[problemNum]/route.tsx ## usage
- Loading branch information
1 parent
445dec0
commit fd4c856
Showing
1 changed file
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { NextResponse } from "next/server"; | ||
import { z } from "zod"; | ||
|
||
import { submissionLangSchema as schema } from "@/schema"; | ||
import { | ||
uhuntProblemNumUrl, | ||
uhuntProblemSubmissionListUrl, | ||
} from "@/utils/constants"; | ||
import { Language, Problem, Submission } from "@/types"; | ||
|
||
type getParamsType = { | ||
params: z.infer<typeof schema>; | ||
}; | ||
|
||
export type getResponseType = Record<string, number>; | ||
|
||
export const GET = async (_request: Request, { params }: getParamsType) => { | ||
// validate params | ||
const schemaResponse = await schema.safeParseAsync(params); | ||
if (!schemaResponse.success) { | ||
const message = { | ||
message: schemaResponse.error.issues[0].message, | ||
}; | ||
|
||
return NextResponse.json(message, { | ||
status: 400, | ||
}); | ||
} | ||
|
||
//----------------------------------------------------------------------------------------------// | ||
|
||
// fetch problem stats | ||
const { problemNum } = params; | ||
|
||
const problemUrl = uhuntProblemNumUrl(problemNum); | ||
const problemResponse = await fetch(problemUrl); | ||
const problemData: Problem = await problemResponse.json(); | ||
|
||
// return 404 if problem doesn't exist | ||
if (Object.entries(problemData).length === 0) { | ||
const message = { | ||
message: `Problem number ${problemNum} not found`, | ||
}; | ||
return NextResponse.json(message, { | ||
status: 404, | ||
}); | ||
} | ||
|
||
//----------------------------------------------------------------------------------------------// | ||
|
||
// fetch submissions of the problem | ||
const submissionsUrl = uhuntProblemSubmissionListUrl(problemData.pid); | ||
const submissionResponse = await fetch(submissionsUrl, { cache: "no-cache" }); | ||
const submissionData: Submission["msg"][] = await submissionResponse.json(); | ||
|
||
// map language id as key and 0 as value. (the value will be count of a submission language) | ||
const languageObj = Object.keys(Language).reduce( | ||
(acc: Record<string, number>, cur: string) => { | ||
acc[cur] = 0; | ||
|
||
return acc; | ||
}, | ||
{}, | ||
); | ||
|
||
// increment count of key-value for their respective language ID | ||
const responseData: getResponseType = submissionData.reduce((acc, cur) => { | ||
const languageId = cur.lan; | ||
acc[languageId] = acc[languageId] + 1; | ||
|
||
return acc; | ||
}, languageObj); | ||
delete responseData["undefined"]; | ||
|
||
return Response.json(responseData); | ||
}; |