-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
140 lines (108 loc) · 3.37 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const express = require('express');
const bodyParser = require('body-parser');
const _ = require('lodash');
const port = 3000;
const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
// for db connection
const mongoose = require('mongoose');
mongoose.connect('mongodb://127.0.0.1:27017/todoListDB', { useNewUrlParser: true });
// creating items db schema
const itemSchema = mongoose.Schema({
name: String
});
// creating model from schema
const Item = mongoose.model('Item', itemSchema);
const item1 = new Item({
name: 'Welcome to ToDo List'
});
const item2 = new Item({
name: 'Press + button to add new items'
});
const item3 = new Item({
name: 'Tick checkbox to delete an entry'
});
const defaultItem = [item1, item2, item3];
// creating lists db schema
const listSchema = mongoose.Schema({
name: String,
items: [itemSchema]
});
const List = mongoose.model("List", listSchema);
app.get("/", (req, res) => {
Item.find({}).then(items => {
const foundItem = items;
// console.log(foundItem.length);
if (foundItem.length == 0) {
Item.insertMany(defaultItem);
res.redirect("/");
} else {
res.render('index', { listTitle: "Today", newItem: foundItem });
}
}).catch(err => console.log('Caught', err.message));
});
app.get("/:customListName", (req, res) => {
let customListName = _.capitalize(req.params.customListName);
const list = new List({
name: customListName,
items: defaultItem
});
List.findOne({ name: customListName }).then((lists) => {
if (!lists) {
list.save().then((list) => {
res.render('index', { listTitle: customListName, newItem: list.items });
}).catch(err => err.message);
} else {
res.render('index', { listTitle: customListName, newItem: lists.items });
}
}).catch((err) => {
console.log(err.message);
})
});
app.post("/", (req, res) => {
let enteredItem = req.body.item;
let currentListName = req.body.list;
if (currentListName == "Today") {
const newItem = new Item({
name: enteredItem
});
newItem.save();
res.redirect("/");
} else {
const newItem = new Item({
name: enteredItem
});
List.findOne({ name: currentListName }).then((list) => {
list.items.push(newItem);
list.save();
})
res.redirect("/" + currentListName);
}
});
app.post("/delete", (req, res) => {
let itemId = req.body.checkbox;
let currentListName = req.body.listName;
if (currentListName == "Today") {
Item.findByIdAndDelete(itemId).then((item) => {
console.log(item.name + " is deleted");
}).catch((err) => {
console.log(err);
});
res.redirect("/");
} else {
List.findOneAndUpdate({ name: currentListName }, { $pull: { items: { _id: itemId } } }).then((foundList) => {
console.log('updated data ' + foundList);
res.redirect("/" + currentListName);
}).catch((err) => {
console.log(err.message);
});
}
})
app.get("/about", (req, res) => {
res.render("about");
})
app.listen(port, () => {
console.log("server up and running");
});