-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
83 lines (78 loc) · 2.02 KB
/
Copy pathscript.js
File metadata and controls
83 lines (78 loc) · 2.02 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
const text=document.getElementById("text")
const text1=document.getElementById("text1")
const brand=document.getElementById("brand")
const price=document.getElementById("price")
const category=document.getElementById("category")
const date=document.getElementById("date")
const available=document.getElementById("available")
const quantity=document.getElementById("quantity")
const button=document.getElementById('btn')
button.addEventListener('click',message)
function message(){
fetch('http://localhost:8080/api/products')
.then(res=>res.json())
.then(datas=>{
const fetch= document.getElementById('fetch')
datas.forEach(data=>{
const tr=document.createElement('tr')
tr.innerHTML+=
`<tr id="row-${data.id}">
<td>${data.id}</td>
<td>${data.name}</td
td>${data.desc}</tr>
<td>${data.brand}</td>
<td>${data.price}</td>
<td>${data.category}</td>
<td>${data.date}</td>
<td>${data.available}</td>
<td>${data.quantity}</td>
<td><button onclick="deleteproduct(${data.id})">Delete</button></td>
</tr>`
fetch.appendChild(tr)
})
})
}
const updatebtn=document.getElementById('update')
.addEventListener('click',postrequest);
function postrequest(){
fetch("http://localhost:8080/api/product",{
method:"POST",
headers:{
"Content-Type":"application/json"
},
body:JSON.stringify({
name :text.value,
desc: text1.value,
brand: brand.value,
price: price.value,
category: category.value,
date: date.value,
available: available.value,
quantity:quantity.value
})
})
.then(res=>res.json())
.then(datas=>{
console.log(datas)
})
.catch(err=>console.log(err))
console.log("hello")
}
function deleteproduct(productId){
fetch("http://localhost:8080/api/product`${productId}`",{
method:"DELETE",
}
).then(res=>{
if(res.ok){
alert(`" Product ${productId}deleted sucessfully"`)
const row=document.getElementById(`row-${productId}`)
if(row){
row.remove();
}
}
else{
alert(`" product ${productId} not deleted"`)
}
})
.catch(err=>console.log(err))
}