2023-11-03 11:28:48 +04:00
|
|
|
const serverUrl = "http://localhost:8081";
|
|
|
|
|
2023-11-03 15:20:56 +04:00
|
|
|
function createLineObject(item, price, stock, count, image) {
|
2023-11-03 11:28:48 +04:00
|
|
|
return {
|
|
|
|
itemsId: item,
|
|
|
|
price: parseFloat(price).toFixed(2),
|
2023-11-03 15:20:56 +04:00
|
|
|
stock,
|
2023-11-03 11:28:48 +04:00
|
|
|
count,
|
2023-11-03 15:20:56 +04:00
|
|
|
sum: parseFloat((price * ((100 - stock) / 100)) * count).toFixed(2),
|
2023-11-03 11:28:48 +04:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2023-11-03 15:20:56 +04:00
|
|
|
export async function createLine(item, price, stock, count, image) {
|
|
|
|
const itemObject = createLineObject(item, price, stock, count, image);
|
2023-11-03 11:28:48 +04:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2023-11-03 15:20:56 +04:00
|
|
|
export async function updateLine(id, item, price, stock, count, image) {
|
|
|
|
const itemObject = createLineObject(item, price, stock, count, image);
|
2023-11-03 11:28:48 +04:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
return response.json();
|
|
|
|
}
|