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:ranklist): add endpoint `/api/problems/ranklist/[…
Browse files Browse the repository at this point in the history
…problemNum]`

  ## what
  - add endpoint `/api/problems/ranklist/[problemNum]`
    - get ranklist 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 ranklist
      - add extra properties
        - verdict object
          - 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/problems/ranklist/[problemNum]/route.ts

  ## usage
  • Loading branch information
Clumsy-Coder committed Jan 12, 2024
1 parent fe1b462 commit 1a35cec
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/app/api/problems/ranklist/[problemNum]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { NextResponse } from "next/server";
import { z } from "zod";

import { problemNumRanklistSchema as schema } from "@/schema";
import { Language, Problem, ProblemVerdictMap, Submission } from "@/types";
import { uhuntProblemNumUrl, uhuntProblemRankUrl } from "@/utils/constants";

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 problem ranklist
const ranklistUrl = uhuntProblemRankUrl(problemData.pid, 1, 10);
const ranklistResponse = await fetch(ranklistUrl);
const ranklistData: Submission["msg"][] = await ranklistResponse.json();

// add properties to the ranklist array
const converted = ranklistData.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 1a35cec

Please sign in to comment.