-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproductManager.js
129 lines (94 loc) · 4.08 KB
/
productManager.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
const fs = require("fs/promises");
class ProductManager{
constructor(path) {
this.path = path;
}
#allProperties(props){
const allProps = [ 'title', 'description', 'price', 'thumbnail', 'code', 'stock' ];
if(props.length === allProps.length && props.every((val, index) => val === allProps[index])){
return true;
} else{
return false;
}
}
async addProduct(product){
try {
const allProducts = await this.getProducts();
//Chequeo que el codigo no se repita
const codigos = allProducts.products.map((product) => product.code);
if(codigos.includes(product.code)){
console.log('Producto invalido. El codigo ya existe');
return;
}
//Chequeo que esten todos los campos
if(!this.#allProperties(Object.keys(product))){
console.log('Producto no agregado. Faltan datos!');
return;
}
//Id autoincrementable
(allProducts.products.length === 0)
? product.id = 1
: product.id = allProducts.products[allProducts.products.length - 1].id + 1;
//Agrego el producto
allProducts.products.push(product);
await fs.writeFile(this.path, JSON.stringify(allProducts));
console.log("Producto agregado!");
} catch (error) {
console.log("~ ProductManager ~ addProduct ~ error:", error);
}
}
async getProducts(){
try {
const allProducts = await fs.readFile(this.path);
return JSON.parse(allProducts);
} catch (error) {
console.log("~ ProductManager ~ getProducts ~ error:", error);
}
}
async getProductById(id){
try {
const allProducts = await this.getProducts();
const productIndex = allProducts.products.findIndex((product) => product.id === id);
(productIndex === -1) ? console.log("Not found") : console.log(allProducts.products[productIndex]);
} catch (error) {
console.log("~ ProductManager ~ getProductById ~ error:", error);
}
}
async updateProduct(id, newData) {
try {
const allProducts = await this.getProducts();
const productIndex = allProducts.products.findIndex((product) => product.id === id);
if(productIndex === -1){
console.log(`No se puede actualizar el producto con id ${id} porque no existe`);
} else{
const product = allProducts.products[productIndex];
Object.keys(newData).forEach((element) => {
if(Object.keys(product).includes(element)){
product[element] = newData[element];
}
});
allProducts.products[productIndex] = product;
await fs.writeFile(this.path, JSON.stringify(allProducts));
console.log(`Se actualizó el producto con id ${id}`);
}
} catch (error) {
console.log("~ ProductManager ~ updateProduct ~ error:", error);
}
}
async deleteProduct(id) {
try {
const allProducts = await this.getProducts();
const productIndex = allProducts.products.findIndex((product) => product.id === id);
if(productIndex === -1){
console.log(`No se puede eliminar el producto con id ${id} porque no existe`);
} else{
allProducts.products.splice(productIndex,1);
await fs.writeFile(this.path, JSON.stringify(allProducts));
console.log(`Se eliminó el producto con id ${id}`);
}
} catch (error) {
console.log("~ ProductManager ~ deleteProduct ~ error:", error);
}
}
}
module.exports = ProductManager;