47 lines
1.3 KiB
JavaScript
47 lines
1.3 KiB
JavaScript
// js/model.js
|
|
|
|
const API_URL = 'http://localhost:5000';
|
|
|
|
export default {
|
|
// --- PRODUCTS ---
|
|
async getProducts() {
|
|
const res = await fetch(`${API_URL}/products?_expand=category&_expand=brand`);
|
|
return res.json();
|
|
},
|
|
async getProductById(id) {
|
|
const res = await fetch(`${API_URL}/products/${id}`);
|
|
return res.json();
|
|
},
|
|
async addProduct(product) {
|
|
const res = await fetch(`${API_URL}/products`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(product)
|
|
});
|
|
return res.json();
|
|
},
|
|
async updateProduct(id, product) {
|
|
const res = await fetch(`${API_URL}/products/${id}`, {
|
|
method: 'PUT',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(product)
|
|
});
|
|
return res.json();
|
|
},
|
|
async deleteProduct(id) {
|
|
await fetch(`${API_URL}/products/${id}`, { method: 'DELETE' });
|
|
},
|
|
|
|
// --- CATEGORIES ---
|
|
async getCategories() {
|
|
const res = await fetch(`${API_URL}/categories`);
|
|
return res.json();
|
|
},
|
|
|
|
// --- BRANDS ---
|
|
async getBrands() {
|
|
const res = await fetch(`${API_URL}/brands`);
|
|
return res.json();
|
|
}
|
|
};
|