-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f2503b7
commit c8b95b3
Showing
2 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
|
||
const {promises:fs} = require('fs') | ||
|
||
class Contenedor { | ||
constructor(filePath){ | ||
this.filePath = filePath | ||
this.lastId = 0 | ||
} | ||
|
||
async save(obj){ | ||
let data = await this.getAll() | ||
const newObj = {id: ++this.lastId, ...obj}; | ||
console.log(`Nuevo objeto: `, newObj); | ||
data = [...data, newObj]; | ||
|
||
await fs.writeFile(this.filePath, JSON.stringify(data, null, 2)); | ||
console.log(`Se creo archivo ${this.filePath}`); | ||
return newObj | ||
} | ||
|
||
async getById(id){ | ||
let data = await this.getAll() | ||
return data.find(data => data.id === id) | ||
} | ||
|
||
async getAll(){ | ||
try { | ||
let raw = await fs.readFile(this.filePath, 'utf8'); | ||
let data = JSON.parse(raw); | ||
let lastId = [0, ...data.map(item => item.id)] | ||
this.lastId = Math.max(...lastId) | ||
console.log(`Se leyo archivo ${this.filePath}, lastId: `, this.lastId); | ||
return data; | ||
} catch (e) { | ||
if( e.code === 'ENOENT') { | ||
try{ | ||
await fs.writeFile(this.filePath, JSON.stringify([])); | ||
console.log(`Se creo archivo ${this.filePath}`); | ||
return [] | ||
} catch (error) { | ||
return `No se puede modificar los datos` | ||
} | ||
} | ||
} | ||
} | ||
|
||
async deleteById(id){ | ||
let data = await this.getAll() | ||
let datafiltered = data.filter(item => item.id != id) | ||
console.log("delete by id", datafiltered) | ||
try { | ||
await fs.writeFile(this.filePath, JSON.stringify(datafiltered, null, 2)) | ||
return `Dato borrado` | ||
} catch (error) { | ||
return `No se puede modificar los datos` | ||
} | ||
} | ||
|
||
async deleteAll(){ | ||
try { | ||
await fs.writeFile(this.filePath, JSON.stringify([])); | ||
} catch (error) { | ||
return `No se puede modificar los datos` | ||
} | ||
} | ||
} | ||
|
||
module.exports = {Contenedor} | ||
// ( async () => { | ||
|
||
// let data = new Contenedor("./data.json") | ||
// let result | ||
|
||
// result = await data.save({name: "Contenedor"}) | ||
// console.log(result) | ||
|
||
// result = await data.deleteById(3) | ||
// console.log(result) | ||
// })() | ||
|
||
// data.getAll() | ||
// .then((data) => { | ||
// console.log(data) | ||
// }) | ||
|
||
// data.getById(3) | ||
// .then((data) => { | ||
// console.log(data) | ||
// }) | ||
|
||
// data.deleteAll() | ||
// .then((data) => { | ||
// console.log(data) | ||
// }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const { Contenedor } = require('./contenedor'); | ||
|
||
|
||
( async () => { | ||
const productos = new Contenedor("./products.json") | ||
|
||
let result = `` | ||
|
||
result = await productos.save({ | ||
title: "Producto 1", | ||
price: 25.4, | ||
thumbnail: "https://images.com/img1.jpg" | ||
}) | ||
console.log(result) | ||
|
||
result = await productos.save({ | ||
title: "Producto 2", | ||
price: 52.21, | ||
thumbnail: "https://images.com/img2.jpg" | ||
}) | ||
console.log(result) | ||
|
||
result = await productos.save({ | ||
title: "Producto 3", | ||
price: 1552.26, | ||
thumbnail: "https://images.com/img3.jpg" | ||
}) | ||
console.log(result) | ||
|
||
result = await productos.getById(2) | ||
console.log("Producto 2: ",result) | ||
|
||
result = await productos.getAll() | ||
console.log(result) | ||
|
||
result = await productos.deleteById(2) | ||
console.log(result) | ||
|
||
// result = await productos.deleteAll() | ||
})() | ||
|