Advertisement
GeorgiLukanov87

07. Students - HTTP and REST - Exercises

Mar 28th, 2023 (edited)
635
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // 07. Students
  2. // HTTP and REST - Exercises
  3. // https://judge.softuni.org/Contests/Compete/Index/3798#6
  4.  
  5.  
  6. function attachEvents() {
  7.   const urlStudents = "http://localhost:3030/jsonstore/collections/students/";
  8.   const firstName = document.querySelector("input[name='firstName']");
  9.   const lastName = document.querySelector("input[name='lastName']");
  10.   const facNumber = document.querySelector("input[name='facultyNumber']");
  11.   const grade = document.querySelector("input[name='grade']");
  12.   const table = document.getElementById("results").tBodies[0];
  13.   const submitBtn = document.getElementById("submit");
  14.  
  15.   submitBtn.addEventListener("click", submiting);
  16.   presentData();
  17.  
  18.   function presentData() {
  19.     table.innerHTML = '';
  20.     fetch(urlStudents)
  21.       .then((response) => response.json())
  22.       .then((data) => {
  23.         for (dataRow in data) {
  24.           let studentFirstName = data[dataRow]["firstName"];
  25.           let studentLastName = data[dataRow]["lastName"];
  26.           let studentFacNumber = data[dataRow]["facultyNumber"];
  27.           let studentGrade = data[dataRow]["grade"];
  28.  
  29.           let row = document.createElement("tr");
  30.  
  31.           let tdFirstname = document.createElement("td");
  32.           tdFirstname.textContent = studentFirstName;
  33.  
  34.           let tdLastName = document.createElement("td");
  35.           tdLastName.textContent = studentLastName;
  36.  
  37.           let tdFacNumber = document.createElement("td");
  38.           tdFacNumber.textContent = studentFacNumber;
  39.  
  40.           let tdGrade = document.createElement("td");
  41.           tdGrade.textContent = studentGrade;
  42.  
  43.           row.appendChild(tdFirstname);
  44.           row.appendChild(tdLastName);
  45.           row.appendChild(tdFacNumber);
  46.           row.appendChild(tdGrade);
  47.  
  48.           table.appendChild(row);
  49.         }
  50.       });
  51.   }
  52.  
  53.   function submiting(e) {
  54.     e.preventDefault(e);
  55.     if (firstName.value && lastName.value && facNumber.value && grade.value) {
  56.       let firstNameSaved = firstName.value;
  57.       let lastNameSaved = lastName.value;
  58.       let facNumberSaved = facNumber.value;
  59.       let gradeSaved = grade.value;
  60.  
  61.       firstName.value = "";
  62.       lastName.value = "";
  63.       facNumber.value = "";
  64.       grade.value = "";
  65.  
  66.       let dataObj = {
  67.         firstName: firstNameSaved,
  68.         lastName: lastNameSaved,
  69.         facultyNumber: facNumberSaved,
  70.         grade: gradeSaved,
  71.       };
  72.  
  73.       fetch(urlStudents, {
  74.         method: "post",
  75.         headers: {
  76.           "Content-type": "applicaiton/json",
  77.         },
  78.         body: JSON.stringify(dataObj),
  79.       }).then(() => presentData())
  80.  
  81.     }
  82.  
  83.   }
  84.  
  85. }
  86.  
  87. attachEvents();
  88.  
  89. ///////////////////////////////////////////////
  90.  
  91. // Extended with Delete Button!
  92.  
  93. function attachEvents() {
  94.   const url = 'http://localhost:3030/jsonstore/collections/students/';
  95.   loadStudentsInfo();
  96.  
  97.   const submitBtn = document.getElementById('submit');
  98.   const tableResults = document.querySelector('#results tbody');
  99.   const inputs = document.querySelector('#form .inputs');
  100.  
  101.   submitBtn.addEventListener('click', addNewStudent);
  102.  
  103.   function addNewStudent() {
  104.     let inputFristName = inputs.children[0].value;
  105.     let inputLastName = inputs.children[1].value;
  106.     let inputFacNumber = inputs.children[2].value;
  107.     let inputGrade = inputs.children[3].value;
  108.  
  109.     if (inputFristName === '' || inputLastName === '' || inputFacNumber === '' || inputGrade === '') {
  110.       alert('Wrong data,fill all the inputs fields!');
  111.       return;
  112.     }
  113.  
  114.     fetch(url, {
  115.       method: 'POST',
  116.       headers: {
  117.         'content-type': 'application/json'
  118.       },
  119.       body: JSON.stringify({
  120.         'firstName': inputFristName,
  121.         'lastName': inputLastName,
  122.         'facultyNumber': inputFacNumber,
  123.         'grade': inputGrade
  124.       })
  125.     })
  126.       .then(res => res.json())
  127.       .then(loadStudentsInfo())
  128.  
  129.     inputs.children[0].value = '';
  130.     inputs.children[1].value = '';
  131.     inputs.children[2].value = '';
  132.     inputs.children[3].value = '';
  133.   }
  134.  
  135.   function loadStudentsInfo(e) {
  136.     if (e) {
  137.       e.preventDefault();
  138.     }
  139.     fetch(url)
  140.       .then(res => res.json())
  141.       .then(data => {
  142.         tableResults.innerHTML = ''
  143.         for (obj in data) {
  144.           let firstName = data[obj]['firstName'];
  145.           let lastName = data[obj]['lastName'];
  146.           let facultyNumber = data[obj]['facultyNumber'];
  147.           let grade = data[obj]['grade'];
  148.           let id = data[obj]['_id'];
  149.  
  150.           let newTr = document.createElement('tr');
  151.  
  152.           let firstNameTd = document.createElement('td');
  153.           firstNameTd.textContent = firstName;
  154.           let lastNameTd = document.createElement('td');
  155.           lastNameTd.textContent = lastName;
  156.           let facultyNumberTd = document.createElement('td');
  157.           facultyNumberTd.textContent = facultyNumber;
  158.           let gradeTd = document.createElement('td');
  159.           gradeTd.textContent = grade;
  160.  
  161.           newTr.appendChild(firstNameTd);
  162.           newTr.appendChild(lastNameTd);
  163.           newTr.appendChild(facultyNumberTd);
  164.           newTr.appendChild(gradeTd);
  165.  
  166.           let deleteBtn = document.createElement('button');
  167.           deleteBtn.textContent = 'delete';
  168.           deleteBtn.style = 'width:100px'
  169.           deleteBtn.id = id;
  170.           deleteBtn.addEventListener('click', deleteContantc);
  171.  
  172.           newTr.appendChild(deleteBtn);
  173.           tableResults.appendChild(newTr);
  174.         }
  175.       })
  176.   }
  177.  
  178.   function deleteContantc(e) {
  179.     let currnetId = e.target.id;
  180.     let deleteUrl = `${url}${currnetId}`
  181.     e.target.parentNode.remove();
  182.  
  183.     fetch(deleteUrl, {
  184.       method: 'delete',
  185.       headers: {
  186.         'content-type': 'application/json'
  187.       },
  188.     }
  189.     )
  190.   }
  191.  
  192. }
  193.  
  194. attachEvents();
  195.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement