Skip to content

Commit 4243ae9

Browse files
committed
feat(frontend): add fetch functions for testcases
1 parent 1255bb1 commit 4243ae9

File tree

1 file changed

+160
-83
lines changed

1 file changed

+160
-83
lines changed
Lines changed: 160 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,203 @@
11
const EXECUTION_SERVICE_URL = process.env.NEXT_PUBLIC_EXECUTION_SERVICE_URL;
22

33
export interface TestData {
4-
input: string
5-
expected: string
4+
input: string;
5+
expected: string;
66
}
77

88
export interface TestResult {
9-
input: string
10-
expected: string
11-
actual: string
12-
passed: boolean
13-
error: string
9+
input: string;
10+
expected: string;
11+
actual: string;
12+
passed: boolean;
13+
error: string;
1414
}
1515

16-
export type Test = TestResult | TestData
16+
export type Test = TestResult | TestData;
1717

1818
export const isTestResult = (test: Test): test is TestResult => {
19-
return 'actual' in test && 'passed' in test && 'error' in test;
19+
return "actual" in test && "passed" in test && "error" in test;
2020
};
2121

2222
export interface GeneralTestResults {
23-
passed: number;
24-
total: number;
23+
passed: number;
24+
total: number;
2525
}
2626

2727
export interface SubmissionHiddenTestResultsAndStatus {
28-
hiddenTestResults: GeneralTestResults;
29-
status: string;
28+
hiddenTestResults: GeneralTestResults;
29+
status: string;
3030
}
3131

32-
export interface SubmissionResults extends SubmissionHiddenTestResultsAndStatus {
33-
visibleTestResults: TestResult[];
32+
export interface SubmissionResults
33+
extends SubmissionHiddenTestResultsAndStatus {
34+
visibleTestResults: TestResult[];
3435
}
3536

3637
export interface ExecutionResults {
37-
visibleTestResults: TestResult[];
38-
customTestResults: TestResult[];
38+
visibleTestResults: TestResult[];
39+
customTestResults: TestResult[];
3940
}
4041

4142
export interface Code {
42-
code: string;
43-
language: string;
44-
customTestCases: string;
43+
code: string;
44+
language: string;
45+
customTestCases: string;
4546
}
4647

4748
export interface Submission {
48-
title: string
49-
code: string
50-
language: string
51-
user: string
52-
matchedUser: string
53-
matchedTopics: string[]
54-
questionDifficulty: string
55-
questionTopics: string[]
49+
title: string;
50+
code: string;
51+
language: string;
52+
user: string;
53+
matchedUser: string;
54+
matchedTopics: string[];
55+
questionDifficulty: string;
56+
questionTopics: string[];
5657
}
5758

5859
export const GetVisibleTests = async (
59-
questionDocRefId: string,
60+
questionDocRefId: string
6061
): Promise<TestData[]> => {
61-
const response = await fetch(
62-
`${EXECUTION_SERVICE_URL}tests/${questionDocRefId}`,
63-
{
64-
method: "GET",
65-
headers: {
66-
"Content-Type": "application/json",
67-
},
68-
}
69-
);
70-
71-
if (response.status === 200) {
72-
return response.json();
73-
} else {
74-
throw new Error(
75-
`Error fetching test cases: ${response.status} ${response.statusText}`
76-
);
62+
const response = await fetch(
63+
`${EXECUTION_SERVICE_URL}tests/${questionDocRefId}`,
64+
{
65+
method: "GET",
66+
headers: {
67+
"Content-Type": "application/json",
68+
},
7769
}
78-
}
70+
);
71+
72+
if (response.status === 200) {
73+
return response.json();
74+
} else {
75+
throw new Error(
76+
`Error fetching test cases: ${response.status} ${response.statusText}`
77+
);
78+
}
79+
};
7980

8081
export const ExecuteVisibleAndCustomTests = async (
81-
questionDocRefId: string,
82-
code: Code,
82+
questionDocRefId: string,
83+
code: Code
8384
): Promise<ExecutionResults> => {
84-
const response = await fetch(
85-
`${EXECUTION_SERVICE_URL}tests/${questionDocRefId}/execute`,
86-
{
87-
method: "POST",
88-
headers: {
89-
"Content-Type": "application/json",
90-
},
91-
body: JSON.stringify(code),
92-
}
85+
const response = await fetch(
86+
`${EXECUTION_SERVICE_URL}tests/${questionDocRefId}/execute`,
87+
{
88+
method: "POST",
89+
headers: {
90+
"Content-Type": "application/json",
91+
},
92+
body: JSON.stringify(code),
93+
}
94+
);
95+
96+
if (response.status === 200) {
97+
return response.json();
98+
} else {
99+
throw new Error(
100+
`Error executing code: ${response.status} ${response.statusText}`
93101
);
102+
}
103+
};
94104

95-
if (response.status === 200) {
96-
return response.json();
97-
} else {
98-
throw new Error(
99-
`Error executing code: ${response.status} ${response.statusText}`
100-
);
105+
export const ExecuteVisibleAndHiddenTestsAndSubmit = async (
106+
questionDocRefId: string,
107+
collaboration: Submission
108+
): Promise<SubmissionResults> => {
109+
const response = await fetch(
110+
`${EXECUTION_SERVICE_URL}tests/${questionDocRefId}/submit`,
111+
{
112+
method: "POST",
113+
headers: {
114+
"Content-Type": "application/json",
115+
},
116+
body: JSON.stringify(collaboration),
101117
}
102-
}
118+
);
103119

104-
export const ExecuteVisibleAndHiddenTestsAndSubmit = async (
105-
questionDocRefId: string,
106-
collaboration: Submission,
120+
if (response.status === 200) {
121+
return response.json();
122+
} else {
123+
throw new Error(
124+
`Error submitting code: ${response.status} ${response.statusText}`
125+
);
126+
}
127+
};
128+
129+
export const CreateTestcases = async (
130+
questionDocRefId: string,
131+
questionTitle: string,
132+
visibleTestcases: TestData[],
133+
hiddenTestcases: TestData[]
107134
): Promise<SubmissionResults> => {
108-
const response = await fetch(
109-
`${EXECUTION_SERVICE_URL}tests/${questionDocRefId}/submit`,
110-
{
111-
method: "POST",
112-
headers: {
113-
"Content-Type": "application/json",
114-
},
115-
body: JSON.stringify(collaboration),
116-
}
135+
const response = await fetch(`${EXECUTION_SERVICE_URL}tests`, {
136+
method: "POST",
137+
headers: {
138+
"Content-Type": "application/json",
139+
},
140+
body: JSON.stringify({
141+
questionDocRefId: questionDocRefId,
142+
questionTitle: questionTitle,
143+
visibleTestCases: visibleTestcases,
144+
hiddenTestCases: hiddenTestcases,
145+
}),
146+
});
147+
148+
if (response.ok) {
149+
return response.json();
150+
} else {
151+
throw new Error(
152+
`Error creating testcases: ${response.status} ${response.statusText}`
117153
);
154+
}
155+
};
118156

119-
if (response.status === 200) {
120-
return response.json();
121-
} else {
122-
throw new Error(
123-
`Error submitting code: ${response.status} ${response.statusText}`
124-
);
157+
export const UpdateTestcases = async (
158+
questionDocRefId: string,
159+
visibleTestcases: TestData[],
160+
hiddenTestcases: TestData[]
161+
): Promise<SubmissionResults> => {
162+
const response = await fetch(
163+
`${EXECUTION_SERVICE_URL}tests/${questionDocRefId}`,
164+
{
165+
method: "PUT",
166+
headers: {
167+
"Content-Type": "application/json",
168+
},
169+
body: JSON.stringify({
170+
visibleTestCases: visibleTestcases,
171+
hiddenTestCases: hiddenTestcases,
172+
}),
125173
}
126-
}
174+
);
175+
176+
if (response.ok) {
177+
return response.json();
178+
} else {
179+
throw new Error(
180+
`Error updating testcases: ${response.status} ${response.statusText}`
181+
);
182+
}
183+
};
184+
185+
export const DeleteTestcases = async (
186+
questionDocRefId: string
187+
): Promise<void> => {
188+
const response = await fetch(
189+
`${EXECUTION_SERVICE_URL}tests/${questionDocRefId}`,
190+
{
191+
method: "DELETE",
192+
headers: {
193+
"Content-Type": "application/json",
194+
},
195+
}
196+
);
197+
198+
if (!response.ok) {
199+
throw new Error(
200+
`Error deleting testcases: ${response.status} ${response.statusText}`
201+
);
202+
}
203+
};

0 commit comments

Comments
 (0)