Skip to content

Commit aaffb92

Browse files
authored
Create api.js
1 parent 000eb90 commit aaffb92

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

api.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// api.js
2+
const API_URL = 'http://localhost:8080'; //url do app, vou add dps
3+
4+
export const productService = {
5+
// Listar produtos
6+
async getProducts() {
7+
const response = await fetch(`${API_URL}/products`);
8+
return response.json();
9+
},
10+
11+
// Criar produto
12+
async createProduct(product) {
13+
const response = await fetch(`${API_URL}/products`, {
14+
method: 'POST',
15+
headers: {
16+
'Content-Type': 'application/json',
17+
},
18+
body: JSON.stringify(product),
19+
});
20+
return response.json();
21+
},
22+
23+
// Buscar produto por ID
24+
async getProduct(id) {
25+
const response = await fetch(`${API_URL}/products/${id}`);
26+
return response.json();
27+
},
28+
29+
// Atualizar produto
30+
async updateProduct(id, product) {
31+
const response = await fetch(`${API_URL}/products/${id}`, {
32+
method: 'PUT',
33+
headers: {
34+
'Content-Type': 'application/json',
35+
},
36+
body: JSON.stringify(product),
37+
});
38+
return response.json();
39+
},
40+
41+
// Deletar produto
42+
async deleteProduct(id) {
43+
await fetch(`${API_URL}/products/${id}`, {
44+
method: 'DELETE',
45+
});
46+
},
47+
};
48+
49+
// Exemplo de componente React so pra deixar de base
50+
import { useEffect, useState } from 'react';
51+
52+
function ProductList() {
53+
const [products, setProducts] = useState([]);
54+
55+
useEffect(() => {
56+
async function loadProducts() {
57+
const data = await productService.getProducts();
58+
setProducts(data);
59+
}
60+
loadProducts();
61+
}, []);
62+
63+
return (
64+
<div>
65+
{products.map(product => (
66+
<div key={product.id}>
67+
<h3>{product.name}</h3>
68+
<p>{product.description}</p>
69+
<p>R$ {product.price}</p>
70+
</div>
71+
))}
72+
</div>
73+
);
74+
}

0 commit comments

Comments
 (0)