-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProducto.js
60 lines (47 loc) · 1.46 KB
/
Producto.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
class Producto{
constructor(titulo, precio, thumbnail){
this.titulo = titulo;
this.precio = precio;
this.thumbnail = thumbnail;
}
}
let Productos = [];
function getAll(){
return Productos
}
function save(titulo, precio, thumbnail){
let obj = new Producto(titulo, precio, thumbnail)
let idMasAlto = 0;
if (Productos.length > 0) {
idMasAlto = Productos.reduce((acum, proximo) => acum > proximo.id ? acum : proximo.id, 0);
}
obj.id = parseInt(idMasAlto) + 1
Productos.push(obj);
return obj
}
function getById(id) {
const objError = { "error": "producto no encontrado" }
const find = Productos.find(producto => producto.id == id) || objError;
return find
}
function updateById(id, titulo, precio, thumbnail) {
const objError = { "error": "producto no encontrado" }
const find = Productos.find(producto => producto.id == id) || objError;
if (find !== objError) {
find.titulo = titulo;
find.precio = precio;
find.thumbnail = thumbnail;
}
return find
}
function deleteById(id) {
const objError = { "error": "producto no encontrado" }
const find = Productos.find(producto => producto.id == id) || objError;
if (find !== objError) {
Productos = Productos.filter(producto => producto.id != id);
return Productos
} else {
return find
}
}
module.exports ={Producto, Productos, getAll, save, getById, updateById, deleteById};