-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
113 lines (106 loc) · 2.97 KB
/
script.js
File metadata and controls
113 lines (106 loc) · 2.97 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const fs = require("fs");
const path = "User.json";
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let id = 1;
function saveTask() {
rl.question("Quelle tâche voulez-vous ajouter ? ", (answer) => {
fs.readFile(path, { encoding: "utf8" }, (err, data) => {
if (data === "") {
const User = [];
User.push({ ID: id++, task: answer });
fileData = JSON.stringify(User);
fs.writeFile(path, fileData, (err) => {
if (err) {
console.log(err);
}
});
}
if (data !== "") {
fileData = JSON.parse(data);
fileData.push({ ID: id++, task: answer });
fileData = JSON.stringify(fileData);
fs.writeFile(path, fileData, (err) => {
if (err) {
console.log(err);
}
});
}
console.log("Tâche ajoutée avec succès");
});
});
rl.close;
}
function updateUser() {
let deleteTask;
let deleteId;
rl.question("Quel est l'ID de la tâche:", (answer) => {
deleteId = answer;
rl.question("Quel est la nouvelle tâche ?", (answer) => {
deleteTask = answer;
fs.readFile(path, { encoding: "utf8" }, (err, data) => {
const UserContain = JSON.parse(data);
for (let index = 0; index < UserContain.length; index++) {
if (UserContain[index].ID == deleteId) {
UserContain[index].task = deleteTask;
}
fs.writeFile(path, JSON.stringify(UserContain), (err) => {
if(err){
throw err
}
});
}
});
});
});
rl.close;
}
function deletingTask() {
let deleteTask;
let deleteId;
rl.question("Quel est l'ID de la tâche:", (answer) => {
deleteId = answer;
rl.question("La tâche va être supprimé...", (answer) => {
deleteTask = answer;
fs.readFile(path, { encoding: "utf8" }, (err, data) => {
const UserContain = JSON.parse(data);
for (let index = 0; index < UserContain.length; index++) {
if (UserContain[index].ID == deleteId) {
UserContain[index].task = deleteTask;
}
fs.writeFile(path, JSON.stringify(UserContain), (err) => {
if(err){
throw err
}
});
}
});
});
});
rl.close;
}
function mainUtilisateurs() {
console.log("1. Ajouter une tâche");
console.log("2. Modifier une tâche");
console.log("3. Supprimer une tâche");
rl.question("Choisir une option: ", (choice) => {
switch (choice) {
case "1":
saveTask();
break;
case "2":
updateUser();
break;
case "3":
deletingTask();
return;
default:
console.log("Choix Erronné");
mainUtilisateurs();
}
});
}
mainUtilisateurs();