-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
98 lines (89 loc) · 3.69 KB
/
app.js
File metadata and controls
98 lines (89 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// CRUD işlemimizin READ kısmı
let table = document.getElementById("userTable");
function getUserList(){
fetch("https://reqres.in/api/users") //verileri alacağımız API yi girdik
.then(response=>response.json()) //aldığımız cevabı json formatına çevirdik.
.then(data=>{
//console.log(data);
for (const user of data.data) { //aldığımız verideki datayı seçiyoruz(filtreliyoruz)
//console.log(user);
table.innerHTML+= `<tr>
<td><input type="text" class="from-control" id="first_name_${user.id}" value="${user.first_name}"></td>
<td><input type="text" class="from-control" id="last_name_${user.id}" value="${user.last_name}"></td>
<td><input type="text" class="from-control" id="email_${user.id}" value="${user.email}"></td>
<td>
<a class="btn btn-warning" onclick="updateUser(${user.id})">Edit</a>
<a class="btn btn-danger" onclick="deleteUser(${user.id})">Delete</a>
</td>
</tr>`
} //innerHTML ile çektiğimiz verileri table a yazdırıyoruz
})
}
getUserList(); //fonkiyonu çağırdık.
function refreshData(){
getUserList();
}
// CRUD işlemimizin CREATE kısmı
function creatUser(){
let data = { //data adında bir değişken oluşturduk ve bu dataları nereden alacağını gösteriyoruz. (Verileri aldık)
first_name:document.getElementById("first_name").value || "Değer Yok",
last_name:document.getElementById("last_name").value || "Değer Yok",
email:document.getElementById("email").value || "Değer Yok",
};
fetch("https://reqres.in/api/users",{//post kullanırken süslü parantez içerisinde tanımlama yapmamız gerekiyor.
method: "POST",
headers:{
'Content-Type' : 'application/json'
},
body:JSON.stringify(data) //bize gelen datayi stringe çevirerek çevirmeliyiz.
})
.then(response=>response.json())
.then(data=>{
console.log(data);
table.innerHTML+=
`<tr>
<td><input type="text" class="from-control" id="" value="${data.first_name}"></td>
<td><input type="text" class="from-control" id="" value="${data.last_name}"></td>
<td><input type="text" class="from-control" id="" value="${data.email}"></td>
<td>
<a class="btn btn-warning" onclick="updateUser(${data.id})">Edit</a>
<a class="btn btn-danger" onclick="deleteUser(${data.id})">Delete</a>
</td>
</tr>`
})
.catch((error)=>{
console.log("Hata: " + error);
})
}
// CRUD işlemimizin UPDATE kısmı
function updateUser(id){
console.log("Updated User id: " + id);
let data ={
first_name:document.getElementById("first_name_"+id).value || "Geçersiz değer",
last_name:document.getElementById("last_name_"+id).value || "Geçersiz değer",
email_name:document.getElementById("email_name_"+id).value || "Geçersiz değer",
};
fetch("https://reqres.in/api/users",{
method: "PUT", //put komudu ile güncelleme isteğinde bulunuyoruz
headers:{
'Content-Type':'application/json'
},
body:JSON.stringify(data)
})
.then(response => response.json)
.then(veri =>{console.log("Updated User",veri)})
.catch((error)=>console.log(error))
}
function deleteUser(id){
fetch("https://reqres.in/api/users"+id,{
method: "DELETE",
headers:{
'Content-Type':'application/json'
}
})
.then(response=>console.log(response))
.then(data=>{
console.log("Deleted User",data);
})
.catch((error)=>console.log(error));
}