Files
PIbd-22_Ulybin_A.A._Interne…/js/model.js
qkrlnt 9c80373c59 lab_4
2025-05-20 16:04:25 +04:00

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();
}
};