Internet-programming-Pibd-2.../js/lines-rest-api.js

88 lines
2.2 KiB
JavaScript
Raw Normal View History

2023-12-07 22:24:21 +04:00
const serverUrl = "http://localhost:8081";
export function createLineObject(item,name, rating, release_date, director,image) {
return {
itemsId: parseInt(item),
name,
rating,
release_date,
director,
image,
};
}
export async function getAllItemTypes() {
const response = await fetch(`${serverUrl}/items`);
if (!response.ok) {
throw response.statusText;
}
return response.json();
}
export async function getAllLines() {
const response = await fetch(`${serverUrl}/lines?_expand=items`);
if (!response.ok) {
throw response.statusText;
}
return response.json();
}
export async function getLine(id) {
const response = await fetch(`${serverUrl}/lines/${id}?_expand=items`);
if (!response.ok) {
throw response.statusText;
}
return response.json();
}
export async function createLine(item,name, rating, release_date, director,image) {
const itemObject = createLineObject(item,name, rating, release_date, director,image);
const options = {
method: "POST",
body: JSON.stringify(itemObject),
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
},
};
const response = await fetch(`${serverUrl}/lines`, options);
if (!response.ok) {
throw response.statusText;
}
return response.json();
}
export async function updateLine(id,item,name, rating, release_date, director,image) {
const itemObject = createLineObject(item,name, rating, release_date, director,image);
const options = {
method: "PUT",
body: JSON.stringify(itemObject),
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
},
};
const response = await fetch(`${serverUrl}/lines/${id}`, options);
if (!response.ok) {
throw response.statusText;
}
return response.json();
}
export async function deleteLine(id) {
const options = {
method: "DELETE",
};
const response = await fetch(`${serverUrl}/lines/${id}`, options);
if (!response.ok) {
throw response.statusText;
}
2023-12-08 01:57:42 +04:00
window.location.reload(true);
2023-12-07 22:24:21 +04:00
return response.json();
}