-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
156 lines (135 loc) · 4.12 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const express = require("express");
const mongoose = require("mongoose");
const _ = require("lodash");
const {check, validationResult} = require("express-validator");
// importing my created modules
const date = require(__dirname + "/date.js");
const itemList = require(__dirname + "/model/item.model.js");
const listList = require(__dirname + "/model/list.model.js");
const app = express();
// tells our app (generated using express module) to use as view engine
app.set('view engine', 'ejs');
app.use(express.urlencoded({extended: true}));
// we are telling express to serve this public folder as static resource
app.use(express.static("public"));
// connected to mongoDB database server and created a new database
mongoose.connect("mongodb+srv://mongodb_cluster_username:[email protected]/todolistDB", {useNewUrlParser: true, useUnifiedTopology: true});
// calling the date() function of date module
const day = date();
const defaultItem1 = new itemList({
name: "Hit checkbox to delete item"
});
// array of item objects
const defaultItems = [defaultItem1];
// client's GET request to home route
app.get("/", function(req, res)
{
itemList.estimatedDocumentCount(function (error, count) {
if (error) {
console.log(error);
} else {
// Item collection empty
if(count === 0){
itemList.insertMany(defaultItems, function(error) {
if (error) {
console.log(error);
} else {
console.log("Item inserted");
}
});
}
}
});
// performing READ operation in Item collection
itemList.find(function (error, result) {
if (error) {
console.log(error);
} else {
res.render('list', {typeOfList : day, newListItems : result});
}
});
});
// client's GET request to home/dynamic route
app.get("/:routeName", function(req, res) {
const listName = _.capitalize(req.params.routeName);
listList.findOne({name: listName}, function (error, found) {
if (error) {
console.log(error);
} else {
if(!found) {
// create new Item document
const list = new listList({
name: listName,
itemsCollection: defaultItems
});
list.save();
res.redirect("/" + listName);
} else {
// display items
res.render("list", {typeOfList: found.name, newListItems: found.itemsCollection});
}
}
});
});
// client's POST request to home route
app.post("/", [ check('newItem', 'Item name must be of 4 characters minimum.').isLength({min: 4}), check('newItem', "invalid name").matches(/^[A-Za-z]/) ], (req, res) => {
const errors = validationResult(req);
if(!errors.isEmpty()){
res.json(errors);
}else{
// collecting the item user entered & the list name
const itemName = req.body.newItem;
const routeName = req.body.list;
// creating new document in Item collection
const item = new itemList({
name: itemName
});
// list from which user has made a POST request match with home route
if(routeName === day){
item.save();
res.redirect("/");
}else{
// list from which user has made a POST request match with any of custom list route
listList.findOne({name: routeName}, function (error, result) {
if(error){
console.log(error);
}else{
result.itemsCollection.push(item);
result.save();
res.redirect("/" + routeName);
}
});
}
}
});
// client's POST request to delete route
app.post("/delete", function (req, res) {
const checkedItemId = req.body.checkitem;
const typeOfListRoute = req.body.listType;
if(typeOfListRoute === day){
// performing DELETE operation on Item collection
itemList.findByIdAndRemove(checkedItemId, function(error){
if(error){
console.log(error);
}else{
console.log("Successfully deleted checked item");
res.redirect("/");
}
});
}else{
// performing DELETE operation on List collection
listList.findOne({name: typeOfListRoute}, function(error, result) {
if(error){
console.log(error);
}else{
result.itemsCollection.remove(checkedItemId);
result.save();
res.redirect("/" + typeOfListRoute);
}
});
}
});
// starting server
app.listen(process.env.PORT || 3000, function () {
console.log("Server is listening on port 3000...");
});