|
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 |
| - "categories": ["Hash Table", "Array"], |
10 |
| - "test_cases": { |
11 |
| - "[2, 7, 11, 15], 9" : "[0, 1]", |
12 |
| - "[3, 2, 4], 6" : "[1, 2]", |
13 |
| - "[3, 3], 6" : "[0, 1]" |
14 |
| - } |
| 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 | + }, |
15 | 28 | },
|
16 |
| - "1" : { |
17 |
| - "id": 1, |
18 |
| - "difficulty": 1, |
19 |
| - "title": "Reverse Integer", |
20 |
| - "description": "Given a 32-bit signed integer, reverse digits of an integer.", |
21 |
| - "categories": ["Math"], |
22 |
| - "test_cases": { |
23 |
| - "123" : "321", |
24 |
| - "1" : "1", |
25 |
| - "22" : "22" |
26 |
| - } |
27 |
| - } |
28 | 29 | };
|
29 | 30 |
|
30 |
| -export async function fetchQuestion(questionId: string): Promise<Question|StatusBody> { |
| 31 | +export async function fetchQuestion( |
| 32 | + questionId: string |
| 33 | +): Promise<Question | StatusBody> { |
31 | 34 | // remove this when services are up
|
32 | 35 | if (process.env.DEV_ENV === "dev") {
|
33 | 36 | return questions[questionId] === undefined
|
34 |
| - ? {error: "Question not found", status: 404} |
35 |
| - : questions[questionId]; |
| 37 | + ? { error: "Question not found", status: 404 } |
| 38 | + : questions[questionId]; |
36 | 39 | }
|
37 | 40 | try {
|
38 |
| - 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 | + ); |
39 | 44 | if (!response.ok) {
|
40 | 45 | return {
|
41 | 46 | error: await response.text(),
|
42 |
| - status: response.status |
| 47 | + status: response.status, |
43 | 48 | };
|
44 | 49 | }
|
45 |
| - return await response.json() as Question; |
| 50 | + return (await response.json()) as Question; |
46 | 51 | } catch (err: any) {
|
47 |
| - return { error: err.message, status: 400}; |
| 52 | + return { error: err.message, status: 400 }; |
48 | 53 | }
|
49 | 54 | }
|
50 | 55 |
|
51 | 56 | export async function addQuestion(body: QuestionFullBody): Promise<StatusBody> {
|
52 | 57 | try {
|
53 | 58 | const response = await fetch(
|
54 |
| - `${process.env.NEXT_PUBLIC_QUESTION_SERVICE}/questions`, |
55 |
| - { |
56 |
| - method: "POST", |
57 |
| - body: JSON.stringify(body).replace(/(\"difficulty\":)\"([1-3])\"/, `$1$2`), |
58 |
| - headers: { |
59 |
| - "Content-type": "application/json; charset=UTF-8" |
60 |
| - } |
61 |
| - } |
| 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 | + } |
62 | 70 | );
|
63 | 71 | if (response.ok) {
|
64 | 72 | return {
|
65 |
| - status: response.status |
| 73 | + status: response.status, |
66 | 74 | };
|
67 | 75 | }
|
68 | 76 | return {
|
69 | 77 | error: (await response.json())["Error adding question: "],
|
70 |
| - status: response.status |
| 78 | + status: response.status, |
71 | 79 | };
|
72 | 80 | } catch (err: any) {
|
73 |
| - return { error: err.message, status: 0}; |
| 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, |
| 104 | + }; |
| 105 | + } catch (err: any) { |
| 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 }; |
74 | 124 | }
|
75 | 125 | }
|
0 commit comments