-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
132 lines (107 loc) · 3.66 KB
/
index.js
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const fs = require('fs');
class ProductManager {
#priceBaseGain = 0.15;
constructor(path) {
this.path = path;
}
async getProducts()
{
try
{
if(fs.existsSync(this.path))
{
const products = await fs.promises.readFile(this.path, 'utf8');
return JSON.parse(products);
} else return [];
}
catch(error)
{
console.log(error);
}
}
async addProduct(title, description, price, thumbnail, code, stock)
{
if (!title || !description || !price || !thumbnail || !code || !stock)
{
console.log("Es obligatorio cargar todos los parametros del producto.")
}
else
{
const codeExists = await this.#isCodeExists(code);
if (codeExists) {
console.log("El codigo Ingresado ya esta cargado.")
}
else
{
const product = {
id: await this.#getMaxID() + 1,
title,
description,
price,
thumbnail,
code,
stock
};
try
{
const products = await this.getProducts();
products.push(product);
console.log(products);
await fs.promises.writeFile(this.path, JSON.stringify(products, null, 2));
}
catch(error)
{
console.log(error);
}
}
}
}
async #getMaxID()
{
try {
const products = await this.getProducts();
return products.reduce((maxId, product) => Math.max(maxId, product.id), 0);
} catch (error) {
console.error('Error al verificar la existencia del código:', error);
return false; // Asumir que el código no existe si hay un error
}
return 1;
}
async #isCodeExists(code)
{
try
{
const products = await this.getProducts();
return products.some(product => product.code === code);
} catch (error) {
console.error('Error linea 86:', error);
return false;
}
}
async getProductById(idProduct) {
try
{
const products = await this.getProducts();
return products.find(product => product.id === idProduct);
} catch (error) {
console.error('Error linea 86:', error);
return false;
}
}
}
const productManager = new ProductManager('./products.json');
const test = async() =>{
//leo el archivo
console.log(await productManager.getProducts())
//agrego cocacola.
await productManager.addProduct("Coca Cola L 1.5", "Bebida Cola de 1.5 Litros", "15.00", "https://m.media-amazon.com/images/I/51v8nyxSOYL._SL1500_.jpg",10001, 200)
//agrego pepsi
await productManager.addProduct("Pepsi L 2.25", "Bebida Cola de 2.25 Litros", "12.00", "https://www.cdparque.com/img/sections/productos/pepsi.png",10002, 300)
//Intento repetir el codigo...
await productManager.addProduct("Fanta L 2", "Bebida Naranja de 2 Litros", "10.00", "https://example.com/fanta.png", 10001, 150);
// Verifico si sale el error cuando no cargo un parametro.
await productManager.addProduct("Fanta L 2", "Bebida Naranja de 2 Litros", "10.00", "https://example.com/fanta.png", 10001);
//Busco y Muestro el Producto
console.log(await productManager.getProductById(2));
}
test();