2023-12-22 12:28:50 +04:00
|
|
|
|
/* eslint-disable linebreak-style */
|
2023-12-06 22:05:45 +04:00
|
|
|
|
// модуль для работы с REST API сервера
|
|
|
|
|
|
|
|
|
|
// адрес сервера
|
|
|
|
|
const serverUrl = "http://localhost:8081";
|
|
|
|
|
|
|
|
|
|
// функция возвращает объект нужной структуры для отправки на сервер
|
2023-12-22 10:03:01 +04:00
|
|
|
|
// eslint-disable-next-line max-len
|
|
|
|
|
function createLineObject(_name, _authorsType, _genresType, _year, _description, _count, _date, _image) {
|
|
|
|
|
return {
|
|
|
|
|
nameBook: _name,
|
|
|
|
|
authorsId: _authorsType,
|
|
|
|
|
genresId: _genresType,
|
|
|
|
|
year: _year,
|
|
|
|
|
description: _description,
|
|
|
|
|
count: _count,
|
|
|
|
|
date: _date,
|
|
|
|
|
image: _image,
|
|
|
|
|
};
|
2023-12-06 22:05:45 +04:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-22 10:03:01 +04:00
|
|
|
|
/// обращение к серверу для получения всех типов товара (get)
|
|
|
|
|
export async function getAllAuthorTypes() {
|
|
|
|
|
const response = await fetch(`${serverUrl}/authors`);
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw response.statusText;
|
|
|
|
|
}
|
|
|
|
|
return response.json();
|
2023-12-06 22:05:45 +04:00
|
|
|
|
}
|
2023-12-22 10:03:01 +04:00
|
|
|
|
export async function getAllGengeTypes() {
|
|
|
|
|
const response = await fetch(`${serverUrl}/genres`);
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw response.statusText;
|
|
|
|
|
}
|
|
|
|
|
return response.json();
|
2023-12-06 22:05:45 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// обращение к серверу для получения всех записей (get)
|
|
|
|
|
export async function getAllLines() {
|
2023-12-22 10:03:01 +04:00
|
|
|
|
const response = await fetch(`${serverUrl}/lines?_expand=authors&_expand=genres`);
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw response.statusText;
|
|
|
|
|
}
|
|
|
|
|
return response.json();
|
2023-12-06 22:05:45 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// обращение к серверу для получения записи по первичному ключу (id) (get)
|
|
|
|
|
// id передается в качестве части пути URL get-запроса
|
|
|
|
|
export async function getLine(id) {
|
2023-12-22 10:03:01 +04:00
|
|
|
|
const response = await fetch(`${serverUrl}/lines/${id}?_expand=genres&_expand=authors`);
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw response.statusText;
|
|
|
|
|
}
|
|
|
|
|
return response.json();
|
2023-12-06 22:05:45 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// обращение к серверу для создания записи (post)
|
|
|
|
|
// объект отправляется в теле запроса (body)
|
2023-12-22 10:03:01 +04:00
|
|
|
|
// eslint-disable-next-line max-len
|
|
|
|
|
export async function createLine(_name, _authorsType, _genresType, _year, _description, _count, _date, _image) {
|
|
|
|
|
// eslint-disable-next-line max-len
|
|
|
|
|
const itemObject = createLineObject(_name, _authorsType, _genresType, _year, _description, _count, _date, _image);
|
2023-12-06 22:05:45 +04:00
|
|
|
|
|
2023-12-22 10:03:01 +04:00
|
|
|
|
const options = {
|
|
|
|
|
method: "POST",
|
|
|
|
|
body: JSON.stringify(itemObject),
|
|
|
|
|
headers: {
|
|
|
|
|
"Accept": "application/json",
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
},
|
|
|
|
|
};
|
2023-12-06 22:05:45 +04:00
|
|
|
|
|
2023-12-22 10:03:01 +04:00
|
|
|
|
const response = await fetch(`${serverUrl}/lines`, options);
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw response.statusText;
|
|
|
|
|
}
|
|
|
|
|
return response.json();
|
2023-12-06 22:05:45 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// обращение к серверу для обновления записи по id (put)
|
|
|
|
|
// объект отправляется в теле запроса (body)
|
|
|
|
|
// id передается в качестве части пути URL get-запроса
|
2023-12-22 10:03:01 +04:00
|
|
|
|
// eslint-disable-next-line max-len
|
|
|
|
|
export async function updateLine(id, _name, _authorsType, _genresType, _year, _description, _count, _date, _image) {
|
|
|
|
|
// eslint-disable-next-line max-len
|
|
|
|
|
const itemObject = createLineObject(_name, _authorsType, _genresType, _year, _description, _count, _date, _image);
|
2023-12-06 22:05:45 +04:00
|
|
|
|
|
2023-12-22 10:03:01 +04:00
|
|
|
|
const options = {
|
|
|
|
|
method: "PUT",
|
|
|
|
|
body: JSON.stringify(itemObject),
|
|
|
|
|
headers: {
|
|
|
|
|
"Accept": "application/json",
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
},
|
|
|
|
|
};
|
2023-12-06 22:05:45 +04:00
|
|
|
|
|
2023-12-22 10:03:01 +04:00
|
|
|
|
const response = await fetch(`${serverUrl}/lines/${id}`, options);
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw response.statusText;
|
|
|
|
|
}
|
|
|
|
|
return response.json();
|
2023-12-06 22:05:45 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// обращение к серверу для удаления записи по id (delete)
|
|
|
|
|
// id передается в качестве части пути URL get-запроса
|
|
|
|
|
export async function deleteLine(id) {
|
2023-12-22 10:03:01 +04:00
|
|
|
|
const options = {
|
|
|
|
|
method: "DELETE",
|
|
|
|
|
};
|
2023-12-06 22:05:45 +04:00
|
|
|
|
|
2023-12-22 10:03:01 +04:00
|
|
|
|
const response = await fetch(`${serverUrl}/lines/${id}`, options);
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw response.statusText;
|
|
|
|
|
}
|
|
|
|
|
await response.json();
|
|
|
|
|
location.reload();
|
2023-12-06 22:05:45 +04:00
|
|
|
|
}
|