Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Commit

Permalink
feat(api:submission:language): add endpoint `/api/submissions/languag…
Browse files Browse the repository at this point in the history
…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
Clumsy-Coder committed Jan 12, 2024
1 parent 445dec0 commit fd4c856
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions src/app/api/submissions/language/[problemNum]/route.tsx
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);
};

0 comments on commit fd4c856

Please sign in to comment.