Skip to content

Commit 6263f32

Browse files
committed
feat: add the meta solver strategy api
1 parent 78a58c0 commit 6263f32

4 files changed

Lines changed: 129 additions & 0 deletions

File tree

.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
NEXT_PUBLIC_API_BASE_URL=http://localhost:8080
2+
NEXT_PUBLIC_MSS_EDITOR_BASE_URL=http://localhost:5173
3+
NEXT_PUBLIC_MSS_API_BASE_URL=http://localhost:5000

src/api/strategy/StrategyAPI.ts

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import { MetaSolverStrategyDto } from "./data-model/MetaSolverStrategyDto";
2+
import { MetaSolverStrategyExecutionOutput } from "./data-model/MetaSolverStrategyExecutionOutput";
3+
4+
export class StrategyApi {
5+
baseUrl: string;
6+
7+
constructor(baseUrl: string) {
8+
this.baseUrl = baseUrl;
9+
}
10+
11+
async listStrategies(
12+
problemTypeId: string = ""
13+
): Promise<MetaSolverStrategyDto[]> {
14+
const query = problemTypeId ? "?type=" + problemTypeId : "";
15+
16+
return await fetch(`${this.baseUrl}/strategies` + query)
17+
.then((res) => res.json())
18+
.then((json) => json as MetaSolverStrategyDto[])
19+
.catch((error) => {
20+
console.error("Failed to list strategies", error);
21+
return [];
22+
});
23+
}
24+
25+
listAllStrategies(): Promise<MetaSolverStrategyDto[]> {
26+
return fetch(`${this.baseUrl}/strategies`)
27+
.then((res) => res.json())
28+
.then((json) => json as MetaSolverStrategyDto[])
29+
.catch((error) => {
30+
console.error("Failed to list all strategies", error);
31+
throw error;
32+
});
33+
}
34+
35+
getStrategy(strategyId: string): Promise<MetaSolverStrategyDto> {
36+
return fetch(`${this.baseUrl}/strategies/${strategyId}`)
37+
.then((res) => res.json())
38+
.then((json) => json as MetaSolverStrategyDto)
39+
.catch((error) => {
40+
console.error(`Failed to get strategy ${strategyId}`, error);
41+
throw error;
42+
});
43+
}
44+
45+
createStrategy(payload: {
46+
name: string;
47+
code: string;
48+
}): Promise<MetaSolverStrategyDto> {
49+
return fetch(`${this.baseUrl}/strategies`, {
50+
method: "POST",
51+
headers: { "Content-Type": "application/json" },
52+
body: JSON.stringify(payload),
53+
})
54+
.then((res) => res.json())
55+
.then((json) => json as MetaSolverStrategyDto)
56+
.catch((error) => {
57+
console.error("Failed to create strategy", error);
58+
throw error;
59+
});
60+
}
61+
62+
updateStrategy(
63+
strategyId: string,
64+
payload: { name: string; code: string }
65+
): Promise<MetaSolverStrategyDto> {
66+
return fetch(`${this.baseUrl}/strategies/${strategyId}`, {
67+
method: "PATCH",
68+
headers: { "Content-Type": "application/json" },
69+
body: JSON.stringify(payload),
70+
})
71+
.then((res) => res.json())
72+
.then((json) => json as MetaSolverStrategyDto)
73+
.catch((error) => {
74+
console.error(`Failed to update strategy ${strategyId}`, error);
75+
throw error;
76+
});
77+
}
78+
79+
deleteStrategy(strategyId: string): Promise<void> {
80+
return fetch(`${this.baseUrl}/strategies/${strategyId}`, {
81+
method: "DELETE",
82+
})
83+
.then((res) => res.json())
84+
.then(() => undefined)
85+
.catch((error) => {
86+
console.error(`Failed to delete strategy ${strategyId}`, error);
87+
throw error;
88+
});
89+
}
90+
91+
executeStrategy(
92+
strategyId: string,
93+
problemId: string
94+
): Promise<MetaSolverStrategyExecutionOutput> {
95+
return fetch(`${this.baseUrl}/strategies/${strategyId}/execute`, {
96+
method: "POST",
97+
headers: { "Content-Type": "application/json" },
98+
body: JSON.stringify({ problemId: problemId }),
99+
})
100+
.then((res) => res.json())
101+
.then((json) => json as MetaSolverStrategyExecutionOutput)
102+
.catch((error) => {
103+
console.error(
104+
`Failed to execute strategy ${strategyId} on problem ${problemId}`,
105+
error
106+
);
107+
throw error;
108+
});
109+
}
110+
}
111+
112+
export const baseUrl = () => process.env.NEXT_PUBLIC_MSS_API_BASE_URL;
113+
114+
export const strategyApi = new StrategyApi(baseUrl() || "");
115+
116+
console.log(baseUrl());
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export interface MetaSolverStrategyDto {
2+
id: string;
3+
name: string;
4+
code: string;
5+
problemTypeId: string;
6+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { ProblemDto } from "../../toolbox/data-model/ProblemDto";
2+
3+
export interface MetaSolverStrategyExecutionOutput {
4+
result: ProblemDto<any> | undefined;
5+
}

0 commit comments

Comments
 (0)