SergeyRasubaev_PIbd-21_IP_R.../3 lab/js/lines-rest-api.js

77 lines
1.9 KiB
JavaScript

const serverUrl = "http://localhost:8081";
function createLineObject(itemName, itemEmail, itemPassword, image) {
return {
nickname: itemName,
email: itemEmail,
password: itemPassword,
image,
};
}
export async function getAllLines() {
const response = await fetch(`${serverUrl}/lines`);
if (!response.ok) {
throw response.statusText;
}
return response.json();
}
export async function getLine(id) {
const response = await fetch(`${serverUrl}/lines/${id}`);
if (!response.ok) {
throw response.statusText;
}
return response.json();
}
export async function createLine(itemName, itemEmail, itemPassword, image) {
const itemObject = createLineObject(itemName, itemEmail, itemPassword, 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, itemName, itemEmail, itemPassword, image) {
const itemObject = createLineObject(itemName, itemEmail, itemPassword, 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;
}
return response.json();
}