-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesafio2.js
57 lines (45 loc) · 1.49 KB
/
desafio2.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
const fs = require("fs");
let products = [];
let pathFile = "./data/products.json";
const addProduct = async (title, description, price, thumbnail, code, stock) => {
const newProduct = {
id: products.length + 1,
title,
description,
price,
thumbnail,
code,
stock,
};
if (Object.values(newProduct).includes(undefined)) {
console.log("Todos los campos son obligatorios");
return;
}
const productExists = products.find((product) => product.code === code);
if (productExists) {
console.log(`El producto ${title} con el código ${code} ya existe`);
return;
}
products.push(newProduct);
await fs.promises.writeFile(pathFile, JSON.stringify(products));
};
const getProducts = () => {
console.log(products);
return products;
};
const getProductById = (id) => {
const product = products.find((product) => product.id === id);
if (!product) {
console.log(`No se encontró el producto con el id ${id}`);
return;
}
console.log(product);
return product;
};
// Test
addProduct("Producto 5", "el quinto producto", 899, "http://www.google.com", "ADF126");
addProduct("Producto 1", "el primer producto", 299, "http://www.google.com", "ADF123", 10);
addProduct("Producto 2", "el segundo producto", 899, "http://www.google.com", "ADF124", 10);
addProduct("Producto 3", "el tercer producto", 899, "http://www.google.com", "ADF124", 10);
addProduct("Producto 4", "el cuarto producto", 899, "http://www.google.com", "ADF125", 10);
// getProducts();