const BASE_URL = 'http://qstack.pl:1111'; // Zaktualizuj zgodnie z konfiguracją Twojego serwera document.addEventListener('DOMContentLoaded', function() { loadAuthors(); loadBooks(); }); document.getElementById('addAuthorForm').addEventListener('submit', function(e) { e.preventDefault(); const authorName = document.getElementById('authorName').value; fetch(`${BASE_URL}/authors/`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: authorName }), }) .then(response => response.json()) .then(() => { loadAuthors(); // Ponowne ładowanie listy autorów po dodaniu nowego autora }) .catch(error => console.error('Error:', error)); }); document.getElementById('addBookForm').addEventListener('submit', function(e) { e.preventDefault(); const bookTitle = document.getElementById('bookTitle').value; const bookAuthorId = document.getElementById('bookAuthorId').value; fetch(`${BASE_URL}/books/`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ title: bookTitle, author_id: parseInt(bookAuthorId, 10) }), }) .then(response => response.json()) .then(() => { loadBooks(); // Ponowne ładowanie listy książek po dodaniu nowej książki }) .catch(error => console.error('Error:', error)); }); function loadAuthors() { fetch(`${BASE_URL}/authors/`) .then(response => response.json()) .then(data => { const authorsSelect = document.getElementById('bookAuthorId'); authorsSelect.innerHTML = ''; // Dodaj domyślną opcję data.forEach(author => { const option = document.createElement('option'); option.value = author.id; option.textContent = author.name; authorsSelect.appendChild(option); }); }) .catch(error => console.error('Error:', error)); } function loadBooks() { fetch(`${BASE_URL}/books/`) .then(response => response.json()) .then(data => { const booksList = document.getElementById('booksList'); booksList.innerHTML = ''; data.forEach(book => { const listItem = document.createElement('li'); listItem.textContent = `${book.title} - Author ID: ${book.author_id}`; booksList.appendChild(listItem); }); }) .catch(error => console.error('Error:', error)); }