38 lines
899 B
JavaScript
38 lines
899 B
JavaScript
export default class ProductModel {
|
|
constructor(apiUrl) {
|
|
this.apiUrl = apiUrl;
|
|
}
|
|
|
|
async getAll() {
|
|
const res = await fetch(`${this.apiUrl}/products`);
|
|
return await res.json();
|
|
}
|
|
|
|
async getById(id) {
|
|
const res = await fetch(`${this.apiUrl}/products/${id}`);
|
|
return await res.json();
|
|
}
|
|
|
|
async create(data) {
|
|
const res = await fetch(`${this.apiUrl}/products`, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(data),
|
|
});
|
|
return await res.json();
|
|
}
|
|
|
|
async update(id, data) {
|
|
const res = await fetch(`${this.apiUrl}/products/${id}`, {
|
|
method: "PUT",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(data),
|
|
});
|
|
return await res.json();
|
|
}
|
|
|
|
async delete(id) {
|
|
return await fetch(`${this.apiUrl}/products/${id}`, { method: "DELETE" });
|
|
}
|
|
}
|