// const port = 4321;
// const SERVER_URL = `http://localhost:${port}`;
import { SERVER_URL } from '../App';
const id = "sections"
const dir = "DOWN"
const queryYear = 2010;
const queryDept = "cpsc";
const queryId = "310"
//const queryInstructor = "*baniassad, elisa*";
export const onSubmitAddDataset = (e: any) => {
e.preventDefault();
const requestOptions = {
method: "PUT",
body: "sectionsBinary",
headers: {'Content-Type': 'application/x-zip-compressed'},
}
const ENDPOINT_URL = `/dataset/${id}/sections`;
fetch(`${SERVER_URL}${ENDPOINT_URL}`, requestOptions)
.then((res) => {
if (!res.ok) {
// If not a 200 response, reject with error message
throw new Error(`Failed to add: ${res.json()}`);
}
return res.json();
})
.then((data) => {
console.log("Received data:", data);
alert("Add dataset executed successfully!");
})
.catch((error) => {
console.error("Error:", error);
alert("Failed to add dataset. Please try again.");
});
}
export const onSubmitListDataset = (e: any) => {
e.preventDefault();
const requestOptions = {
method: "GET",
}
const ENDPOINT_URL = `/datasets`;
fetch(`${SERVER_URL}${ENDPOINT_URL}`, requestOptions)
.then((res) => {
if (!res.ok) {
throw new Error(`Failed to list: ${res.json()}`);
}
return res.json();
})
.then((data) => {
console.log("Received data:", data);
alert("List dataset executed successfully!");
})
.catch((error) => {
console.error("Error:", error);
alert("Failed to list dataset. Please try again.");
});
}
export const onSubmitRemoveDataset = (e: any) => {
e.preventDefault();
const requestOptions = {
method: "DELETE",
}
const ENDPOINT_URL = `/dataset/${id}`;
fetch(`${SERVER_URL}${ENDPOINT_URL}`, requestOptions)
.then((res) => {
if (!res.ok) {
throw new Error(`Failed to remove: ${res.json()}`);
}
return res.json();
})
.then((data) => {
console.log("Received data:", data);
alert("Remove dataset executed successfully!");
})
.catch((error) => {
console.error("Error:", error);
alert("Failed to remove dataset. Please try again.");
});
}
export const onSubmitPerformQuery = (e: any, setResults: any) => {
e.preventDefault();
const querySections = {
"WHERE": {
"AND": [
{
"EQ": {
[`${id}_year`]: queryYear
}
},
{
"IS": {
[`${id}_dept`]: queryDept
}
},
{
"IS": {
[`${id}_id`]: queryId
}
}
]
},
"OPTIONS": {
"COLUMNS": [
`${id}_dept`,
`${id}_id`,
`${id}_year`,
`${id}_instructor`,
`${id}_avg`,
`${id}_pass`,
`${id}_fail`
],
"ORDER": {
"dir": dir,
"keys": [
`${id}_avg`
]
}
}
}
const requestOptions = {
method: "POST",
body: JSON.stringify(querySections),
headers: {'Content-Type': 'application/json'},
}
const ENDPOINT_URL = `/query`;
fetch(`${SERVER_URL}${ENDPOINT_URL}`, requestOptions)
.then((res) => {
if (!res.ok) {
throw new Error(`Failed to query: ${res.json()}`);
}
return res.json();
})
.then((data) => {
console.log("Received data:", data);
alert("Perform query executed successfully!");
setResults(data.result);
})
.catch((error) => {
console.error("Error:", error);
alert("Failed to perform query. Please try again.");
});
}