vueGroup.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. const requestFunc = async(url, method = "GET", data = null, token = null) => {
  2. apihost = 'http://127.0.0.1/api'
  3. method = method.toLocaleUpperCase()
  4. let fullurl = `${apihost}${url}`;
  5. let options = {
  6. method: method,
  7. headers: {
  8. "Content-Type": "application/json",
  9. "Authorization": `Bearer ${token}`,
  10. },
  11. };
  12. switch(method) {
  13. case "PUT":
  14. delete options.headers["Content-Type"];
  15. options.body = data;
  16. break;
  17. case "POST": case "PATCH": case "DELETE":
  18. options.body = JSON.stringify(data);
  19. break;
  20. }
  21. const res = await fetch(fullurl, options);
  22. return await res.json();
  23. };
  24. var vueapp = new Vue({
  25. el: '#app',
  26. delimiters: ['${', '}'],
  27. data: {
  28. groups:[],
  29. teachers:[],
  30. specialties:[],
  31. },
  32. methods: {
  33. showGroups() {
  34. console.log(this.groups)
  35. },
  36. async getGroups(){
  37. this.groups = await requestFunc("/group/", "GET")
  38. },
  39. async getTeachers(){
  40. teachers = await requestFunc("/teacher/", "GET")
  41. for(let i =0; i < teachers.length; i++) {
  42. Vue.set(this.teachers, i, teachers[i]);
  43. }
  44. },
  45. async getSpecilties(){
  46. specialties = await requestFunc("/specialty/", "GET")
  47. for(let i =0; i < specialties.length; i++) {
  48. Vue.set(this.specialties, i, specialties[i]);
  49. }
  50. },
  51. async mountFunc(){
  52. await this.getTeachers()
  53. await this.getGroups()
  54. await this.getSpecilties()
  55. }
  56. },
  57. async mounted() {
  58. await this.mountFunc()
  59. console.log(this.teachers)
  60. }
  61. });
  62. addTeacher = () =>{
  63. let surname = document.querySelector('.surname').value
  64. let name = document.querySelector('.name').value
  65. let patronymic = document.querySelector('.patronymic').value
  66. var select = document.getElementById("selectClassroom");
  67. var value = select.value;
  68. var myHeaders = new Headers();
  69. myHeaders.append("Content-Type", "application/json");
  70. var raw = {"Surname":surname,"Name":name,"IDCLassroom":Number(value),"Patronymic": patronymic};
  71. requestFunc(url="/teacher/", method="POST", data=raw)
  72. alert("Добавлено")
  73. }