-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
202 lines (170 loc) · 5.09 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
const bodyParser = require("body-parser");
const express = require("express");
const mongoose = require("mongoose");
const _ = require("lodash");
require("dotenv").config();
let item;
//connect to MongoDB by specifying port to access MongoDB server
main().catch((err) => console.log(err));
//creating a todolistDB after / localhost
async function main() {
await mongoose.connect(
"mongodb+srv://admin-hassan:" +
process.env.MONGOATLASPASWORD +
"@cluster0.yipmq.mongodb.net/todolistDB"
);
console.log("DB Server is up and running");
}
//schema -> model -> document -> save
// SCHEMA
const itemsSchema = new mongoose.Schema({
name: String,
});
// MODEL: used for queries, will be displayed as items in mongo collections
const Item = mongoose.model("Item", itemsSchema);
// DOCUMENTS
const item1 = new Item({
name: "WELCOME TO YOUR TO DO LIST!",
});
// Schema
const listSchema = new mongoose.Schema({
name: String,
items: [itemsSchema],
});
//Model: : another one for queries, will be displayed as lists in mongo collections
const List = mongoose.model("List", listSchema);
//defaultItems for adding to null db
const defaultItems = [item1];
// REQUIRING OUR LOCAL MODULE FROM date.js
const date = require(__dirname + "/date.js");
// DATE MODULE SAVED IN DAY AND CALLING GETDATE()
const day = date.getDate();
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
//EJS FOR USING TEMPLATES (TO AVOID MAKING MANY HTML FILES)
app.set("view engine", "ejs");
app.get("/", function (req, res) {
Item.find({}, function (err, foundItems) {
// checking to see if the DB is empty then add defaultItems
// if(foundItems.length === 0){
// Item.insertMany(defaultItems, function(err){
// if(err){
// console.log(err);
// }
// else{
// console.log("Successfully saved the items to list");
// }
// });
// res.redirect("/");
// }
// else{
//RENDERING LIST.EJS FILE WHERE THE VARIABLE 'ListTitle' GETS CHANGED ON THE BASIS OF day
res.render("list", { ListTitle: day, newListItems: foundItems });
});
});
//for custom links
app.get("/:customListName", function (req, res) {
//req.params for getting different routes
const customListName = _.capitalize(req.params.customListName);
//first it will find the already existed list on that customListName route
List.findOne({ name: customListName }, function (err, foundList) {
if (!err) {
if (!foundList) {
//if not found create the list and save
const list = new List({
name: customListName,
items: defaultItems,
});
list.save();
res.redirect("/" + customListName);
} else {
//if found show/render the list
res.render("list", {
ListTitle: foundList.name,
newListItems: foundList.items,
});
}
}
});
});
//post request of adding into the list
app.post("/", function (req, res) {
const itemName = req.body.newItems;
const listName = req.body.list;
//if the listName belongs to day then add to day list and redirect
item = new Item({
name: itemName,
});
if (
listName === "Tuesday," ||
listName === "Wednesday," ||
listName === "Thursday," ||
listName === "Friday," ||
listName === "Saturday," ||
listName === "Sunday," ||
listName === "Monday,"
) {
item.save();
res.redirect("/");
}
//if the listName belongs to any custom one then add to custom list and redirect
else {
List.findOne({ name: listName }, function (err, foundList) {
foundList.items.push(item);
foundList.save();
res.redirect("/" + listName);
});
}
});
// Handling remove post request
app.post("/remove", function (req, res) {
const itemName = req.body.newItems;
const listName = req.body.Removebutton;
let foundItem;
//if home "/" route with title as day then find and delete from Item
if (
listName === "Tuesday," ||
listName === "Wednesday," ||
listName === "Thursday," ||
listName === "Friday," ||
listName === "Saturday," ||
listName === "Sunday," ||
listName === "Monday,"
) {
// traverse the DB to the end and store the last element in foundItem
Item.find(function (err, itemFind) {
if (err) {
console.log(err);
} else {
itemFind.forEach((element) => {
foundItem = element.name;
});
}
//Then delete the stored item in foundItem
Item.deleteOne({ name: foundItem }, function (err) {
if (err) {
console.log(err);
} else {
console.log("Successfully Deleted");
}
});
res.redirect("/");
});
}
//if other then home route then find and delete from List
else {
List.findOne({ name: listName }, function (err, foundList) {
foundList.items.pop(item);
foundList.save();
res.redirect("/" + listName);
});
}
});
let port = process.env.PORT;
if (port == null || port == "") {
port = 3000;
}
app.listen(port, function (req, res) {
console.log("Server has been started and running");
});