-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduct-service.js
60 lines (54 loc) · 1.59 KB
/
product-service.js
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
const DATABASE_URL = 'https://my-json-server.typicode.com/joi-gn/one-geek-games-store/products'
const productList = async () => {
const response = await fetch(DATABASE_URL)
if (response.ok) return response.json();
throw new Error('Não foi possível listar os produtos')
}
const addProduct = (imageURL, name, price, description, category) => {
return fetch(DATABASE_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({imageURL, name, price, description, category})
})
.then(response => {
if (response.ok) return response.body;
throw new Error('Não foi possível adicionar o produto');
})
}
const removeProduct = (id) => {
return fetch(`${DATABASE_URL}/${id}`, {
method: 'DELETE',
})
.then(response => {
if (!response.ok) throw new Error('Não foi possível deletar o produto');
})
}
const detailsProduct = (id) => {
return fetch(`${DATABASE_URL}/${id}`)
.then(response => {
if(response.ok) return response.json()
throw new Error('Não foi possível detalhar o produto');
})
}
const updateProduct = (id, imageURL, name, price, description, category) => {
return fetch(`${DATABASE_URL}/${id}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({imageURL, name, price, description, category})
})
.then(response => {
if (response.ok) return response.json();
throw new Error('Não foi possível atualizar o produto');
})
}
export const productServices = {
productList,
addProduct,
removeProduct,
detailsProduct,
updateProduct
}