Internet_Programming_Librar.../lab3/all/js/lines-rest-api.js

108 lines
2.8 KiB
JavaScript
Raw Permalink Normal View History

2024-01-06 22:18:58 +04:00
const serverUrl = "http://localhost:8081";
function createLineObject(item, author, title, long, image, text) {
// console.log("createLineObject");
console.log(text);
return {
itemsId: item,
author,
title,
longsId: long,
image,
text,
};
}
export async function getAllItemTypes() {
// console.log("getAllItemTypes");
const response = await fetch(`${serverUrl}/items`);
// console.log(response);
if (!response.ok) {
throw response.statusText;
}
return response.json();
}
export async function getAllLongs() {
// console.log("getAllLongs");
const response = await fetch(`${serverUrl}/longs`);
// console.log(response);
if (!response.ok) {
throw response.statusText;
}
return response.json();
}
export async function getAllLines() {
// console.log("getAllLines");
const response = await fetch(`${serverUrl}/lines?_expand=items&_expand=longs`);
console.log(response);
if (!response.ok) {
throw response.statusText;
}
return response.json();
}
export async function getLine(id) {
// console.log("getLine");
const response = await fetch(`${serverUrl}/lines/${id}?_expand=items`);
if (!response.ok) {
throw response.statusText;
}
return response.json();
}
export async function createLine(item, author, title, long, image, text) {
// console.log("createLine");
const itemObject = createLineObject(item, author, title, long, image, text);
// console.log("created a Line");
// console.log(itemObject);
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, author, title, long, image, text) {
// console.log("updateLine");
const itemObject = createLineObject(item, author, title, long, image, text);
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) {
// console.log("deleteLine");
const options = {
method: "DELETE",
};
const response = await fetch(`${serverUrl}/lines/${id}`, options);
if (!response.ok) {
throw response.statusText;
}
return response.json();
}