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): add endpoint
/api/problems
## what - add endpoint `/api/problems` ## how ## why - this will fetch all problems from upstream ## where - ./src/app/api/problems/route.ts ## usage
- Loading branch information
1 parent
14bb0a8
commit e65ce18
Showing
1 changed file
with
73 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,73 @@ | ||
import { uhuntAllProblemsUrl } from "@/utils/constants"; | ||
|
||
/** | ||
* an array of keys that will be used to convert an array of arrays into array of objects | ||
* | ||
* check `https://uhunt.onlinejudge.org/api/p` to view raw data. (each problem stats are presented as an array element) | ||
*/ | ||
const arrKey = [ | ||
"pid", | ||
"num", | ||
"title", | ||
"dacu", | ||
"mrun", | ||
"mmem", | ||
"nover", | ||
"sube", | ||
"noj", | ||
"inq", | ||
"ce", | ||
"rf", | ||
"re", | ||
"ole", | ||
"tle", | ||
"mle", | ||
"wa", | ||
"pe", | ||
"ac", | ||
"rtl", | ||
"status", | ||
"rej", | ||
]; | ||
|
||
export const GET = async (_request: Request) => { | ||
const url = uhuntAllProblemsUrl(); | ||
|
||
const response = await fetch(url); | ||
// data returned is an array of array | ||
// ex: | ||
// [ | ||
// [ | ||
// 36, | ||
// 100, | ||
// "The 3n + 1 problem", | ||
// ... | ||
// ], | ||
// ] | ||
const data = await response.json(); | ||
|
||
// convert an array of array into array of problems | ||
// ex: | ||
// convert | ||
// [ | ||
// [1,2,3,...], | ||
// [1,2,3,...], | ||
// ] | ||
// | ||
// into | ||
// [ | ||
// {pid: 1, num: 2, title: 3, ...}, | ||
// {pid: 1, num: 2, title: 3, ...}, | ||
// ] | ||
const converted = data.map((problem: number[]) => { | ||
const initialValue = {}; | ||
return problem.reduce((obj, item, index: number) => { | ||
return { | ||
...obj, | ||
[arrKey[index]]: item, | ||
}; | ||
}, initialValue); | ||
}); | ||
|
||
return Response.json(converted); | ||
}; |