Skip to content

Commit

Permalink
crear funcion para crear tarea
Browse files Browse the repository at this point in the history
  • Loading branch information
andreybejarano committed Jun 3, 2022
1 parent af5492a commit 10d0143
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
21 changes: 16 additions & 5 deletions clase6/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,24 @@ const accion = process.argv[2];

switch (accion) {
case 'listar':
const tareas = funcTareas.listar();
const tareas = funcTareas.leerArchivo();
console.log('Listado de tareas');
console.log('-----------------');
for (let index = 0; index < tareas.length; index++) {
// console.log((index + 1) + '. ' + tareas[index].titulo + ' - ' + tareas[index].estado);
console.log(`${index + 1}. ${tareas[index].titulo} - ${tareas[index].estado}`);
}
tareas.forEach((tarea, index) => {
console.log(`${index + 1}. ${tarea.titulo} - ${tarea.estado}`);
});
break;
case 'crear':
console.log();
console.log('Nueva tarea creada');
console.log('------------------');
const titulo = process.argv[3];
const tarea = {
titulo,
estado: "pendiente"
};
funcTareas.guardarTarea(tarea);
console.log(`${tarea.titulo} -> ${tarea.estado}`);
break;
case undefined:
console.log('Atención - Tienes que pasar una acción.');
Expand Down
16 changes: 13 additions & 3 deletions clase6/funcionesArchivos.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@
const fs = require('fs');

module.exports = {
listar: () => {
const fileTareas = fs.readFileSync('tareas.json', 'utf-8');
file: 'tareas.json',
leerArchivo: function() {
const fileTareas = fs.readFileSync(this.file, 'utf-8');
return JSON.parse(fileTareas);
},
escribirJSON: function(tareas) {
const tarea = JSON.stringify(tareas, null, ' ');
fs.writeFileSync(this.file, tarea);
},
guardarTarea: function(nuevaTarea) {
let tareas = this.leerArchivo();
tareas.push(nuevaTarea);
this.escribirJSON(tareas);
}
}
};
12 changes: 12 additions & 0 deletions clase6/tareas.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,17 @@
{
"titulo": "Break",
"estado": "pendiente"
},
{
"titulo": "Titulo de la tarea",
"estado": "pendiente"
},
{
"titulo": "Titulo de la tarea 2",
"estado": "pendiente"
},
{
"titulo": "Titulo de la tarea 3",
"estado": "pendiente"
}
]

0 comments on commit 10d0143

Please sign in to comment.