Skip to content

Commit bb289e5

Browse files
authored
Merge pull request #12 from CS3219-AY2425S1/qn-refactor
Centralise styling and add delete button to question card
2 parents ff29a09 + 710229b commit bb289e5

File tree

14 files changed

+353
-179
lines changed

14 files changed

+353
-179
lines changed

peerprep/api/gateway.ts

Lines changed: 91 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,125 @@
11
import { Question, StatusBody, QuestionFullBody } from "./structs";
22

33
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+
},
1528
},
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-
}
2829
};
2930

30-
export async function fetchQuestion(questionId: string): Promise<Question|StatusBody> {
31+
export async function fetchQuestion(
32+
questionId: string
33+
): Promise<Question | StatusBody> {
3134
// remove this when services are up
3235
if (process.env.DEV_ENV === "dev") {
3336
return questions[questionId] === undefined
34-
? {error: "Question not found", status: 404}
35-
: questions[questionId];
37+
? { error: "Question not found", status: 404 }
38+
: questions[questionId];
3639
}
3740
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+
);
3944
if (!response.ok) {
4045
return {
4146
error: await response.text(),
42-
status: response.status
47+
status: response.status,
4348
};
4449
}
45-
return await response.json() as Question;
50+
return (await response.json()) as Question;
4651
} catch (err: any) {
47-
return { error: err.message, status: 400};
52+
return { error: err.message, status: 400 };
4853
}
4954
}
5055

5156
export async function addQuestion(body: QuestionFullBody): Promise<StatusBody> {
5257
try {
5358
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+
}
6270
);
6371
if (response.ok) {
6472
return {
65-
status: response.status
73+
status: response.status,
6674
};
6775
}
6876
return {
6977
error: (await response.json())["Error adding question: "],
70-
status: response.status
78+
status: response.status,
7179
};
7280
} 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 };
74124
}
75125
}

peerprep/api/structs.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
export enum Difficulty {
2+
All = 0,
23
Easy = 1,
34
Medium,
4-
Hard
5+
Hard,
56
}
67

78
export interface QuestionBody {
@@ -28,6 +29,8 @@ export interface StatusBody {
2829
error?: string;
2930
}
3031

31-
export function isError(obj: Question | StatusBody): obj is StatusBody {
32+
export function isError(
33+
obj: Question[] | Question | StatusBody
34+
): obj is StatusBody {
3235
return (obj as StatusBody).status !== undefined;
3336
}

peerprep/app/globals.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ body,
1313
p,
1414
h1,
1515
h2 {
16-
@apply text-gray-100;
16+
@apply text-text-2;
1717
}

0 commit comments

Comments
 (0)