Files
PIbd-22_Savinov_R.D._LibraNet/html/components/bookComponent/model.js
2025-04-25 19:35:37 +04:00

37 lines
1.2 KiB
JavaScript

import { createItem, deleteItem, getAllItems, getItem, updateItem } from "./api/client";
const BOOKS_URL = "books";
const AUTHORS_URL = "authors";
const GENRES_URL = "genres";
export default {
async getBooks() {
const books = await getAllItems(BOOKS_URL);
const authors = await getAllItems(AUTHORS_URL);
const genres = await getAllItems(GENRES_URL);
const booksWithDetails = books.map((book) => {
const author = authors.find((author) => author.id === book.author_id);
const genre = genres.find((genre) => genre.id === book.genre_id);
return {
...book,
author: author ? author.name : "Неизвестный автор",
genre: genre ? genre.name : "Неизвестный жанр",
};
});
return booksWithDetails;
},
async getBook(id) {
const book = await getItem(BOOKS_URL, id);
return book;
},
async addBook(book) {
const res = await createItem(BOOKS_URL, book);
return res;
},
async deleteBook(id) {
return await deleteItem(BOOKS_URL, id);
},
async updateBook(model) {
return updateItem(BOOKS_URL, model.id, model);
},
};