-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
101 lines (92 loc) · 2.69 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
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
const mongoose = require("mongoose");
const colors = require("colors");
const db = mongoose.connect("mongodb://localhost:27017/todos", {
useNewUrlParser: true,
useUnifiedTopology: true,
});
// import the model
const Todo = require("./models/todo");
// List all todos
const listTodos = () => {
Todo.find().then((todos) => {
if (todos.length == 0) {
console.info("No Todos Found".red);
// db.close();
} else {
todos.forEach((todo) => {
if (todo.status) {
console.info(
`Title: ${todo.title.green.bold}\nDescription: ${todo.description.yellow.bold}\nId: ${todo._id}\n\n`
);
// db.close();
} else {
console.info(
`Title: ${todo.title.red.bold}\nDescription: ${todo.description.yellow.bold}\nId: ${todo._id}\n\n`
);
// db.close();
}
});
}
});
};
// Find a todo
const findTodo = (title) => {
const search = new RegExp(title, "i");
Todo.find({ title: search }).then((todo) => {
if (todo.length == 0) {
console.info("No Todos Found".red);
// db.close();
} else {
todo.forEach((todo) => {
if (todo.status) {
console.info(
`Title: ${todo.title.green.bold}\nDescription: ${todo.description.yellow.bold}\nId: ${todo._id}\n\n`
);
// db.close();
} else {
console.info(
`Title: ${todo.title.red.bold}\nDescription: ${todo.description.yellow.bold}\nId: ${todo._id}\n\n`
);
// db.close();
}
});
}
});
};
// add a todo
const addTodo = (todo) => {
Todo.create(todo).then((todo) => {
console.info("New Todo Added".green.bold);
// db.close();
});
};
// update a todo
const updateTodo = (_id, todo) => {
Todo.updateOne({ _id }, todo).then((todo) => {
console.info("Todo Updated".green.bold);
// db.close();
});
};
// remove a todo
const removeTodo = (_id) => {
Todo.findByIdAndDelete({ _id }).then((todo) => {
console.info("Todo Removed".red.bold);
// db.close();
});
};
const markDone = (_id) => {
Todo.findById({ _id }).then((todo) => {
todo.status = true;
todo.save();
console.info("Todo Marked Done".green.bold);
// db.close();
});
};
module.exports = {
addTodo,
findTodo,
updateTodo,
removeTodo,
listTodos,
markDone
};