-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (65 loc) · 1.43 KB
/
index.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 mongoose = require("mongoose");
const say = require("async-sayjs");
const Todo = require("./model/Todo");
mongoose.connect("mongodb://localhost/todo-cli", { useNewUrlParser: true });
const addTodo = todo => {
Todo.create(todo)
.then(() => {
console.log("added todo...");
say.speak("todo added");
mongoose.connection.close();
})
.catch(err => {
throw err;
});
};
const listTodos = () => {
Todo.find({ isCompleted: "false" })
.then(todos => {
say.speak(`you had ${todos.length} task today , boss`);
todos.map(todo => {
console.log(todo.task);
say.speak(todo.task);
});
mongoose.connection.close();
})
.catch(err => {
throw err;
});
};
const updateTodo = (todoName, todo) => {
console.log(todo);
Todo.findOne({ task: todoName }, (err, res) => {
if (err) {
throw err;
}
console.log(res);
res.task = todo.task;
res.isCompleted = todo.isCompleted;
res
.save()
.then(() => {
console.log("updated....");
say.speak("todo updated");
mongoose.connection.close();
})
.catch(err => {
console.log(err);
});
});
};
const deleteTodo = id => {
Todo.deleteOne({ _id: id }, err => {
if (err) {
throw err;
}
say.speak("todo deleted");
mongoose.connection.close();
});
};
module.exports = {
addTodo,
deleteTodo,
listTodos,
updateTodo
};