Skip to content

Commit

Permalink
Create api.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Bulletdev authored Dec 5, 2024
1 parent 000eb90 commit aaffb92
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// api.js
const API_URL = 'http://localhost:8080'; //url do app, vou add dps

export const productService = {
// Listar produtos
async getProducts() {
const response = await fetch(`${API_URL}/products`);
return response.json();
},

// Criar produto
async createProduct(product) {
const response = await fetch(`${API_URL}/products`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(product),
});
return response.json();
},

// Buscar produto por ID
async getProduct(id) {
const response = await fetch(`${API_URL}/products/${id}`);
return response.json();
},

// Atualizar produto
async updateProduct(id, product) {
const response = await fetch(`${API_URL}/products/${id}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(product),
});
return response.json();
},

// Deletar produto
async deleteProduct(id) {
await fetch(`${API_URL}/products/${id}`, {
method: 'DELETE',
});
},
};

// Exemplo de componente React so pra deixar de base
import { useEffect, useState } from 'react';

function ProductList() {
const [products, setProducts] = useState([]);

useEffect(() => {
async function loadProducts() {
const data = await productService.getProducts();
setProducts(data);
}
loadProducts();
}, []);

return (
<div>
{products.map(product => (
<div key={product.id}>
<h3>{product.name}</h3>
<p>{product.description}</p>
<p>R$ {product.price}</p>
</div>
))}
</div>
);
}

0 comments on commit aaffb92

Please sign in to comment.