Advertisement
GeorgiLukanov87

08. Book Library - HTTP and REST - Exercises

Mar 30th, 2023 (edited)
441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // 08. Book Library
  2. // HTTP and REST - Exercises
  3. // https://judge.softuni.org/Contests/Compete/Index/3798#7
  4.  
  5. function attachEvents() {
  6.   const baseUrl = `http://localhost:3030/jsonstore/collections/books/`;
  7.   const loadAllBtn = document.getElementById('loadBooks');
  8.   const tbodyElement = document.querySelector('body > table > tbody');
  9.  
  10.   const inputsElements = document.querySelectorAll('input');
  11.   const titleInputElement = inputsElements[0];
  12.   const authorInputElement = inputsElements[1];
  13.   const submitBtn = document.querySelector('#form button');
  14.   const formElement = document.getElementById('form');
  15.  
  16.   submitBtn.addEventListener('click', addBook)
  17.   loadAllBtn.addEventListener('click', loadAllBooks);
  18.  
  19.   // Load All books!
  20.   function loadAllBooks(event) {
  21.     event?.preventDefault();
  22.     tbodyElement.innerHTML = '';
  23.     fetch(baseUrl)
  24.       .then(res => res.json())
  25.       .then(data => {
  26.         for (let index in data) {
  27.           let title = data[index]['title'];
  28.           let author = data[index]['author'];
  29.           let id = index;
  30.  
  31.           let newTr = document.createElement('tr');
  32.           newTr.id = id;
  33.           newTr.innerHTML = `
  34.           <td>${title}</td>
  35.           <td>${author}</td>
  36.           <td>
  37.             <button>Edit</button>
  38.             <button>Delete</button>
  39.           </td>`
  40.           tbodyElement.appendChild(newTr);
  41.         }
  42.       }).then(() => {
  43.         Array.from(tbodyElement.querySelectorAll('button')).forEach(btn => {
  44.           if (btn.textContent == 'Edit') {
  45.             btn.addEventListener('click', editBook);
  46.           } else {
  47.             btn.addEventListener('click', deleteBook);
  48.           }
  49.         })
  50.       }
  51.  
  52.       )
  53.   }
  54.  
  55.   // Add book!
  56.   function addBook(e) {
  57.     if (!titleInputElement.value || !authorInputElement.value) {
  58.       alert('Missing information!')
  59.       return;
  60.     }
  61.     let bookObj = {
  62.       author: authorInputElement.value,
  63.       title: titleInputElement.value,
  64.     }
  65.    
  66.     const headers = {
  67.       method: 'POST',
  68.       body: JSON.stringify(bookObj),
  69.     }
  70.    
  71.     fetch(baseUrl, headers)
  72.       .then(() => loadAllBooks())
  73.       .then(() => titleInputElement.value = '',
  74.         authorInputElement.value = '');
  75.   }
  76.  
  77.   //Edit book.
  78.   function editBook(e) {
  79.     let book = e.target.parentNode.parentNode;
  80.     let id = book.id;
  81.     let title = book.children[0].textContent;
  82.     let author = book.children[1].textContent;
  83.     titleInputElement.value = title;
  84.     authorInputElement.value = author;
  85.      
  86.     formElement.children[0].textContent = 'Edit FORM';
  87.     submitBtn.removeEventListener('click', addBook)
  88.     submitBtn.textContent = 'Save';
  89.      
  90.     // Update book -> Innner function to use the ID!
  91.     submitBtn.addEventListener('click', function updateBook() {
  92.       let putUrl = baseUrl + `${id}`;
  93.       let editedBookObj = {
  94.         "author": authorInputElement.value,
  95.         "title": titleInputElement.value,
  96.       }
  97.       const headers = { method: 'PUT', body: JSON.stringify(editedBookObj) };
  98.        
  99.       fetch(putUrl, headers)
  100.         .then(() => loadAllBooks(),
  101.           titleInputElement.value = '',
  102.           authorInputElement.value = '',
  103.           formElement.children[0].textContent = 'FORM',
  104.           submitBtn.removeEventListener('click', updateBook),
  105.           submitBtn.addEventListener('click', addBook),
  106.         );
  107.  
  108.     });
  109.  
  110.   }
  111.  
  112.   // Delete book.
  113.   function deleteBook(e) {
  114.     const id = e.target.parentNode.parentNode.id;
  115.     const delUrl = baseUrl + `${id}`;
  116.     const headers = { method: 'DELETE' };
  117.  
  118.     fetch(delUrl, headers)
  119.       .then(() => loadAllBooks());
  120.   }
  121.  
  122. }
  123.  
  124. attachEvents();
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement