upas-web-site/js/api.js

55 lines
1.4 KiB
JavaScript
Raw Normal View History

const API = "http://localhost:3001";
const STUDENTS = `${API}/students`;
const GROUPS = `${API}/groups`;
const addStudent = async (data) => {
const response = await fetch(STUDENTS, {
method: "post",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});
if (!response.ok) {
throw new Error(`Response status: ${response.status}`);
}
};
const editStudent = async (id, data) => {
const response = await fetch(`${STUDENTS}/${id}`, {
method: "put",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});
if (!response.ok) {
throw new Error(`Response status: ${response.status}`);
}
};
const deleteStudent = async (id) => {
const response = await fetch(`${STUDENTS}/${id}`, { method: "delete" });
if (!response.ok) {
throw new Error(`Response status: ${response.status}`);
}
};
const getGroups = async () => {
const response = await fetch(GROUPS);
if (!response.ok) {
throw new Error(`Response status: ${response.status}`);
}
return response.json();
};
const getStudents = async () => {
const response = await fetch(`${STUDENTS}?_expand=group`);
if (!response.ok) {
throw new Error(`Response status: ${response.status}`);
}
return response.json();
};
export { addStudent, deleteStudent, editStudent, getGroups, getStudents };