-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandleData.js
73 lines (59 loc) · 2.34 KB
/
handleData.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
const colors = require('colors');
const fs = require('fs');
const handleData = (type, title) => {
//type - number ( 1 - add, 2 - remove, 3 - list )
//title ( string || null )
const data = fs.readFileSync('datadb.json');
// let data = fs.readFileSync('datadb.json');
// data = data.toString();
let tasks = JSON.parse(data);
// console.log(tasks);
if(type === 1 || type === 2) {
isExisted = tasks.find(task => task.title === title) ? true : false;
if(type === 1 && isExisted) {
return console.log('That task already exists.'.red);
} else if (type === 2 && !isExisted) {
return console.log("I can't remove task, that already exists.".red)
}
}
let dataJSON = "";
switch (type) {
case 1:
//reconstruction of array
// console.log(tasks);
tasks = tasks.map((task, index) => ({id: index + 1, title: task.title}));
// console.log(tasks);
const id = tasks.length + 1;
tasks.push({ id, title });
// console.log(tasks);
dataJSON = JSON.stringify(tasks);
// console.log(dataJSON);
fs.writeFileSync('datadb.json', dataJSON);
console.log(`I'm adding task: ${title}`.white.bgGreen);
break;
case 2:
// console.log("Usuwam zadanie.");
// console.log(tasks);
const index = tasks.findIndex(task => task.title === title);
tasks.splice(index, 1);
// console.log(tasks);
tasks = tasks.map((task, index) => ({id: index + 1, title: task.title}));
// console.log(tasks);
dataJSON = JSON.stringify(tasks);
fs.writeFile('datadb.json', dataJSON, 'utf8', (err) => {
if(err) throw err;
console.log(`Task ${title} has been removed.`.white.bgGreen);
})
break;
case 3:
console.log(`List of tasks includes ${tasks.length} positions. To do, you have: `);
if (tasks.length) {
tasks.forEach((task, index) => {
if (index % 2) return console.log(task.title.green);
return console.log(task.title.yellow);
});
}
break;
}
};
module.exports = handleData;