-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
141 lines (117 loc) Β· 3.5 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
const express = require("express");
const app = express();
require('dotenv').config();
const bodyParser = require("body-parser");
const date = require(__dirname + ("/date.js"));
const _ = require("lodash");
const mongoose = require("mongoose");
mongoose.connect(process.env.MONGODB_URL, {
useNewUrlParser: true,
useUnifiedTopology: true
}).then(() => {
console.log('Connected to MongoDB');
}).catch((error) => {
console.log('Error connecting to MongoDB', error);
});
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
app.set('view engine', 'ejs');
//schema for mongodb
const itemSchema = new mongoose.Schema({
name: {
type: String
}
})
const listSchema = new mongoose.Schema({
name: String,
items: [itemSchema]
})
const Item = mongoose.model("Item", itemSchema);
const List = mongoose.model("List", listSchema);
//route
app.get("/", function (req, res) {
const day = date.getDate();
Item.find()
.then((items) => {
res.render("list", { date: day, listtitle: "Today", tasklist: items });
})
.catch((error) => {
console.log("Error occurred: ", error);
});
});
app.post("/", function (req, res) {
const taskName = req.body.firstinput;
const listName = req.body.list;
const item = new Item({
name: taskName
});
const day = date.getDate();
if (listName === "Today") {
item.save();
res.redirect("/");
} else {
List.findOne({ name: listName })
.then(foundList => {
if (foundList) { // add null check here
foundList.items.push(item);
foundList.save();
res.redirect("/" + listName);
} else {
console.log(`List with name '${listName}' not found.`);
res.redirect("/");
}
})
.catch(error => {
console.log(error);
});
}
});
app.post("/delete", function (req, res) {
const id = req.body.deletedTask;
const listName = req.body.listName;
if (listName === "Today") {
Item.findByIdAndRemove(id)
.then(() => {
res.redirect("/");
})
.catch((error) => {
console.log(error);
});
} else {
List.findOneAndUpdate(
{ name: listName },
{ $pull: { items: { _id: id } } }
)
.then(() => {
res.redirect("/" + listName);
})
.catch((error) => {
console.log(error);
});
}
});
app.get("/:topic", function (req, res) {
const customListName = _.capitalize(req.params.topic);
List.findOne({ name: customListName }).then((list) => {
if (!list) {
const newList = new List({
name: customListName
});
newList.save().then(() => {
res.redirect("/" + customListName);
}).catch((error) => {
console.error(error);
res.status(500).send("Error creating new list");
});
} else {
res.render("list", { listtitle: list.name, tasklist: list.items });
}
}).catch((error) => {
console.error(error);
res.status(500).send("Error finding list");
});
});
const port = process.env.PORT || 3000;
app.listen(port, function () {
console.log(`The server is running on port ${port}`);
});