-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
118 lines (105 loc) · 3.16 KB
/
app.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
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
114
115
116
117
118
// ------------ Begin - The block of the code -----------------
const fs = require('fs');
const _ = require('lodash');
const yargs = require('yargs');
const DataFile = require('./data.js');
// ------------ End - The block of the code -----------------
// ------------ Begin - command configuration -----------------
const customerIDopt = {
describe: 'Customer ID number',
demand : true,
alias : 'i'
}
const customerNameopt = {
describe: 'Customer name',
demand : true,
alias : 'n'
}
const customerEmailopt = {
describe: 'Customer Email',
demand : true,
alias : 'e'
}
const customerPhoneopt = {
describe: 'Customer phone number',
demand : true,
alias : 'p'
}
const argv = yargs
.command('add','Add a new customer entry',{
customerID: customerIDopt,
customerName: customerNameopt,
customerEmail: customerEmailopt,
customerPhone: customerPhoneopt
})
.command('update','Update a customer entry',{
customerID: customerIDopt,
customerName: customerNameopt,
customerEmail: customerEmailopt,
customerPhone: customerPhoneopt
})
.command('list','List all customers entries')
.command('read','Read a specific customer information',{
customerID: customerIDopt
})
.command('remove','Remove a customer',{
customerID: customerIDopt
})
.help()
.argv;
// ------------ End - command configuration -----------------
var command = argv._[0];
// ------------ Begin - Adding -----------------
if (command === 'add'){
var info = DataFile.addData(argv.customerID,argv.customerName,argv.customerEmail,argv.customerPhone);
if (info){
DataFile.logData(info);
} else{
console.log("Data already exists");
}
}
// ------------ End - Adding -----------------
// ------------ Begin - Updating -----------------
else if (command === 'update'){
var info = DataFile.updateData(argv.customerID,argv.customerName,argv.customerEmail,argv.customerPhone);
if (info){
DataFile.logData(info);
} else{
console.log("Data updated");
}
}
// ------------ End - Updating -----------------
// ------------ Begin - listing -----------------
else if (command === 'list') {
var AllData = DataFile.getAll();
console.log(`Printing ${AllData.length} info(s).`);
AllData.forEach((info)=>{
DataFile.logData(info);
});
}
// ------------ End - listing -----------------
// ------------ Begin - Reading -----------------
else if (command === 'read') {
var info = DataFile.readData(argv.customerID);
if(info){
DataFile.logData(info);
}
else{
console.log("Data not found");
}
}
// ------------ End - Reading -----------------
// ------------ Begin - Deleting -----------------
else if (command === 'remove') {
var info = DataFile.removeData(argv.customerID);
if(info){
console.log(`This ID (${argv.customerID}) was deleted successfully`);
}
else{
console.log("Data not found");
}
}
// ------------ End - Deleting -----------------
else{
console.log('Please type the right command');
}