forked from github/copilot-cli-for-beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproductService.js
More file actions
69 lines (54 loc) · 1.43 KB
/
Copy pathproductService.js
File metadata and controls
69 lines (54 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* Product service for e-commerce operations
*/
async function getAllProducts(filters = {}) {
const params = new URLSearchParams(filters);
const response = await fetch(`/api/products?${params}`);
if (!response.ok) {
throw new Error('Failed to fetch products');
}
return response.json();
}
async function getProduct(productId) {
const response = await fetch(`/api/products/${productId}`);
if (!response.ok) {
throw new Error('Product not found');
}
return response.json();
}
async function createProduct(productData) {
const response = await fetch('/api/products', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(productData)
});
if (!response.ok) {
throw new Error('Failed to create product');
}
return response.json();
}
async function updateProduct(productId, updates) {
const response = await fetch(`/api/products/${productId}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(updates)
});
if (!response.ok) {
throw new Error('Failed to update product');
}
return response.json();
}
function formatPrice(cents) {
return `$${(cents / 100).toFixed(2)}`;
}
function calculateDiscount(price, discountPercent) {
return price * (1 - discountPercent / 100);
}
module.exports = {
getAllProducts,
getProduct,
createProduct,
updateProduct,
formatPrice,
calculateDiscount
};