|
1 | 1 | import { Question, StatusBody, QuestionFullBody } from "./structs";
|
2 | 2 |
|
3 | 3 | const questions: { [key: string]: Question } = {
|
4 |
| - "0" : { |
5 |
| - "id": 0, |
6 |
| - "difficulty": 2, |
7 |
| - "title": "Two Sum", |
8 |
| - "description": "Given an array of integers, return indices of the two numbers such that they add up to a specific target.", |
9 |
| - "test_cases": { |
10 |
| - "[2, 7, 11, 15], 9" : "[0, 1]", |
11 |
| - "[3, 2, 4], 6" : "[1, 2]", |
12 |
| - "[3, 3], 6" : "[0, 1]" |
13 |
| - } |
| 4 | + "0": { |
| 5 | + id: 0, |
| 6 | + difficulty: 2, |
| 7 | + title: "Two Sum", |
| 8 | + description: |
| 9 | + "Given an array of integers, return indices of the two numbers such that they add up to a specific target.", |
| 10 | + categories: ["Hash Table", "Array"], |
| 11 | + test_cases: { |
| 12 | + "[2, 7, 11, 15], 9": "[0, 1]", |
| 13 | + "[3, 2, 4], 6": "[1, 2]", |
| 14 | + "[3, 3], 6": "[0, 1]", |
| 15 | + }, |
| 16 | + }, |
| 17 | + "1": { |
| 18 | + id: 1, |
| 19 | + difficulty: 1, |
| 20 | + title: "Reverse Integer", |
| 21 | + description: "Given a 32-bit signed integer, reverse digits of an integer.", |
| 22 | + categories: ["Math"], |
| 23 | + test_cases: { |
| 24 | + "123": "321", |
| 25 | + "1": "1", |
| 26 | + "22": "22", |
| 27 | + }, |
14 | 28 | },
|
15 |
| - "1" : { |
16 |
| - "id": 1, |
17 |
| - "difficulty": 1, |
18 |
| - "title": "Reverse Integer", |
19 |
| - "description": "Given a 32-bit signed integer, reverse digits of an integer.", |
20 |
| - "test_cases": { |
21 |
| - "123" : "321", |
22 |
| - "1" : "1", |
23 |
| - "22" : "22" |
24 |
| - } |
25 |
| - } |
26 | 29 | };
|
27 | 30 |
|
28 |
| -export async function fetchQuestion(questionId: string): Promise<Question|StatusBody> { |
| 31 | +export async function fetchQuestion( |
| 32 | + questionId: string |
| 33 | +): Promise<Question | StatusBody> { |
29 | 34 | // remove this when services are up
|
30 | 35 | if (process.env.DEV_ENV === "dev") {
|
31 | 36 | return questions[questionId] === undefined
|
32 |
| - ? {error: "Question not found", status: 404} |
33 |
| - : questions[questionId]; |
| 37 | + ? { error: "Question not found", status: 404 } |
| 38 | + : questions[questionId]; |
34 | 39 | }
|
35 | 40 | try {
|
36 |
| - const response = await fetch(`${process.env.NEXT_PUBLIC_QUESTION_SERVICE}/questions/solve/${questionId}`); |
| 41 | + const response = await fetch( |
| 42 | + `${process.env.NEXT_PUBLIC_QUESTION_SERVICE}/questions/solve/${questionId}` |
| 43 | + ); |
37 | 44 | if (!response.ok) {
|
38 | 45 | return {
|
39 |
| - ...(await response.json()), |
40 |
| - status: response.status |
| 46 | + error: await response.text(), |
| 47 | + status: response.status, |
41 | 48 | };
|
42 | 49 | }
|
43 |
| - return await response.json() as Question; |
| 50 | + return (await response.json()) as Question; |
44 | 51 | } catch (err: any) {
|
45 |
| - return { error: err.message, status: 0}; |
| 52 | + return { error: err.message, status: 400 }; |
46 | 53 | }
|
47 | 54 | }
|
48 | 55 |
|
49 | 56 | export async function addQuestion(body: QuestionFullBody): Promise<StatusBody> {
|
50 | 57 | try {
|
51 | 58 | const response = await fetch(
|
52 |
| - `${process.env.NEXT_PUBLIC_QUESTION_SERVICE}/questions`, |
53 |
| - { |
54 |
| - method: "POST", |
55 |
| - body: JSON.stringify(body).replace(/(\"difficulty\":)\"([1-3])\"/, `$1$2`), |
56 |
| - headers: { |
57 |
| - "Content-type": "application/json; charset=UTF-8" |
58 |
| - } |
59 |
| - } |
| 59 | + `${process.env.NEXT_PUBLIC_QUESTION_SERVICE}/questions`, |
| 60 | + { |
| 61 | + method: "POST", |
| 62 | + body: JSON.stringify(body).replace( |
| 63 | + /(\"difficulty\":)\"([1-3])\"/, |
| 64 | + `$1$2` |
| 65 | + ), |
| 66 | + headers: { |
| 67 | + "Content-type": "application/json; charset=UTF-8", |
| 68 | + }, |
| 69 | + } |
60 | 70 | );
|
61 | 71 | if (response.ok) {
|
62 | 72 | return {
|
63 |
| - status: response.status |
| 73 | + status: response.status, |
64 | 74 | };
|
65 | 75 | }
|
66 | 76 | return {
|
67 | 77 | error: (await response.json())["Error adding question: "],
|
68 |
| - status: response.status |
| 78 | + status: response.status, |
| 79 | + }; |
| 80 | + } catch (err: any) { |
| 81 | + return { error: err.message, status: 0 }; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +export async function deleteQuestion(question: Question): Promise<StatusBody> { |
| 86 | + try { |
| 87 | + const response = await fetch( |
| 88 | + `${process.env.NEXT_PUBLIC_QUESTION_SERVICE}/questions/delete/${question.id}`, |
| 89 | + { |
| 90 | + method: "DELETE", |
| 91 | + headers: { |
| 92 | + "Content-type": "application/json; charset=UTF-8", |
| 93 | + }, |
| 94 | + } |
| 95 | + ); |
| 96 | + if (response.ok) { |
| 97 | + return { |
| 98 | + status: response.status, |
| 99 | + }; |
| 100 | + } |
| 101 | + return { |
| 102 | + error: (await response.json())["Error deleting question: "], |
| 103 | + status: response.status, |
69 | 104 | };
|
70 | 105 | } catch (err: any) {
|
71 |
| - return { error: err.message, status: 0}; |
| 106 | + return { error: err.message, status: 0 }; |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +export async function getAllQuestions(): Promise<Question[] | StatusBody> { |
| 111 | + try { |
| 112 | + const response = await fetch( |
| 113 | + `${process.env.NEXT_PUBLIC_QUESTION_SERVICE}/questions` |
| 114 | + ); |
| 115 | + if (!response.ok) { |
| 116 | + return { |
| 117 | + error: await response.text(), |
| 118 | + status: response.status, |
| 119 | + }; |
| 120 | + } |
| 121 | + return (await response.json()) as Question[]; |
| 122 | + } catch (err: any) { |
| 123 | + return { error: err.message, status: 400 }; |
72 | 124 | }
|
73 | 125 | }
|
0 commit comments