From 1a35cec38173c12ce1078dda8795cc807ebacbd3 Mon Sep 17 00:00:00 2001 From: Clumsy-Coder <19594044+Clumsy-Coder@users.noreply.github.com> Date: Tue, 9 Jan 2024 18:30:10 -0700 Subject: [PATCH] feat(api:problemNum:ranklist): add endpoint `/api/problems/ranklist/[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 --- .../problems/ranklist/[problemNum]/route.ts | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/app/api/problems/ranklist/[problemNum]/route.ts diff --git a/src/app/api/problems/ranklist/[problemNum]/route.ts b/src/app/api/problems/ranklist/[problemNum]/route.ts new file mode 100644 index 0000000..d84611d --- /dev/null +++ b/src/app/api/problems/ranklist/[problemNum]/route.ts @@ -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; +}; + +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); +};