-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
146 lines (122 loc) · 3.54 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
require("dotenv").config();
const express = require("express");
const bodyParser = require("body-parser");
const date = require("./date");
const mongoose = require("mongoose");
const today = date.getDate(); // get today's date from date module
const app = express();
app.set("view engine", "ejs");
// set body-parser
app.use(
bodyParser.urlencoded({
extended: true,
})
);
app.use(express.static(__dirname + "/public"));
mongoose.connect(
`mongodb+srv://${process.env.MONGO_CREDENTIALS}@cluster0.vfnca.mongodb.net/todoDB`,
{ useNewUrlParser: true, useUnifiedTopology: true }
);
mongoose.set("useFindAndModify", false);
const itemSchema = {
name: String,
};
const Item = mongoose.model("Item", itemSchema);
let items = [];
const daySchema = {
name: String,
items: [itemSchema],
};
const Day = mongoose.model("Day", daySchema);
// display todays list when going to home page as default
app.get("/", (req, res) => {
// requested todays list route
res.redirect("/date/" + today);
});
// handle the request
app.get("/date/:requestedDate", (req, res) => {
const requestedDate = req.params.requestedDate;
// search for the requested date in DB
Day.findOne({ name: requestedDate }, (err, foundDate) => {
// foundDate is an object, so we cannot check its length: check if it exists or not instead
if (!err) {
if (!foundDate) {
//if date does not exist yet, create a new date
const day = new Day({
name: requestedDate,
items: items,
});
day.save();
// redirect to the requested date URL
res.redirect("/date/" + requestedDate);
} else {
// render existing list
res.render("list", {
listTitle: foundDate.name,
newListItems: foundDate.items,
});
}
}
});
});
app.post("/", (req, res) => {
// what happens when you pick a new date in datepicker:
const clickedDate = req.body.datepicker;
// search for the picked date in DB
Day.findOne({ name: clickedDate }, (err, foundDate) => {
if (!err) {
if (!foundDate) {
//if a date does not exist yet, create a new date list
const day = new Day({
name: clickedDate,
items: items,
});
day.save();
// redirect to the requested date URL
res.redirect("/date/" + clickedDate);
} else {
// show an existing list: redirect to the requested date URL => renders
res.redirect("/date/" + clickedDate);
}
}
});
});
// adding new items:
app.post("/add", (req, res) => {
const dateToPostTo = req.body.getDayToAddItemTo;
const itemAdded = req.body.newItem; // store input in a variable
const item = new Item({
name: itemAdded,
});
Day.findOne({ name: dateToPostTo }, (err, foundDay) => {
if (!err) {
foundDay.items.push(item);
foundDay.save();
res.redirect("/date/" + foundDay.name);
}
});
});
app.post("/delete", (req, res) => {
const checkedItemId = req.body.delete;
const DateToDeleteFrom = req.body.DateToDeleteFrom;
Day.findOneAndUpdate(
{ name: DateToDeleteFrom },
{ $pull: { items: { _id: checkedItemId } } },
(err, foundDate) => {
// findOne corresponds to finding a list (therefore findList)
if (!err) {
res.redirect("/date/" + DateToDeleteFrom);
}
}
);
});
app.get("/about", (req, res) => {
res.render("about");
});
app.get("/contact", (req, res) => {
res.render("contact");
});
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`ToDoList running on port ${port}`);
});