-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodos-cmd.js
54 lines (46 loc) · 1.17 KB
/
todos-cmd.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
const service = require('./services/todos');
module.exports = function runCommand([ _, __, action, value = '', extraValue ]) {
switch (action) {
case 'get':
printFilteredTodos(value);
break;
case 'delete':
deleteTodo(value);
break;
case 'add':
addTodo(value, extraValue);
break;
case 'done':
setTodoDone(value, true);
break;
case 'undone':
setTodoDone(value, false);
break;
}
}
async function printFilteredTodos(filterString) {
const filters = {};
if (filterString.includes('done')) {
filters.isDone = true;
}
if (filterString.includes('open')) {
filters.isDone = false;
}
if (!('isDone' in filters) && filterString) {
filters.content = filterString
}
const todos = await service.getTodos(filters);
console.table(todos);
}
async function deleteTodo(id) {
await service.removeTodo(Number(id));
console.log("Item deleted successfully!");
}
async function addTodo(content, extra) {
const newTodo = await service.addTodo({ content, isDone: extra === 'done' });
console.table([ newTodo ]);
}
async function setTodoDone(id, isDone) {
const updatedTodo = await service.updateTodo(Number(id), { isDone });
console.table([ updatedTodo ]);
}