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

Commit

Permalink
feat(api:problemNum:submission): add endpoint `/api/submissions/[prob…
Browse files Browse the repository at this point in the history
…lemNum]`

  ## what
  - add endpoint `/api/submissions/[problemNum]`
    - get submissions 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 problem submissions
      - add extra properties
        - verdict
          - fgColor
          - bgColor
          - title
          - fgHex
          - bgHex
        - language: convert language ID into a string
        - pnum: problem number
        - pTitle: name of the problem

  ## how

  ## why

  ## where
  - ./src/app/api/submissions/[problemNum]/route.ts

  ## usage
  • Loading branch information
Clumsy-Coder committed Jan 16, 2024
1 parent fc62c79 commit 7b71529
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions src/app/api/submissions/[problemNum]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { z } from "zod";

import { problemNumSubmissionSchema as schema } from "@/schema";
import { NextResponse } from "next/server";
import {
uhuntProblemNumUrl,
uhuntProblemRankUrl,
uhuntProblemSubmissionListUrl,
} from "@/utils/constants";
import { Language, Problem, ProblemVerdictMap, Submission } from "@/types";
import moment from "moment";

type getParamsType = {
params: z.infer<typeof schema>;
};

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,
moment().subtract(1, "years").unix(),
moment().unix(),
20
);
const submissionResponse = await fetch(submissionsUrl, { cache: "no-cache" });
const submissionData: Submission["msg"][] = await submissionResponse.json();

const converted = submissionData.map((rank: Submission["msg"]) => {
rank.verdict = ProblemVerdictMap[rank.ver] || {
fgColor: "text-primary-foreground dark:text-secondary-foreground",
bgColor: "bg-gray-500",
title: "- In Queue -",
fgHex: "",
bgHex: "6b7280",
};
rank.lan = Language[rank.lan] || "--";
rank.pnum = problemData.num;
rank.pTitle = problemData.title;

return rank;
});

return Response.json(converted);
};

0 comments on commit 7b71529

Please sign in to comment.