-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
162 lines (142 loc) · 4.7 KB
/
index.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
require("dotenv").config();
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const { Schema } = mongoose;
const _ = require("lodash");
// Setting the preferences
const app = express();
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/public"));
app.set("views", __dirname + "/views");
app.use(bodyParser.urlencoded({ extended: true }));
// Initialise the connection to the database
mongoose.connect(process.env.MONGODB_URI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => {
console.log("Connected to MongoDB Atlas");
})
.catch(error => {
console.error("MongoDB Connection Error:", error);
});
// mongoose.connect("mongodb+srv://vercel-admin-user-64d65e4e877fad3dcc5a0752:[email protected]/todoListDB").catch((error)=>{console.error(error)});
// Creating the schema and model
const inputsSchema = {
Name: String
};
const Input = mongoose.model("Input", inputsSchema);
const listSchema = {
Name: String,
Item: [inputsSchema]
};
const List = mongoose.model("List", listSchema);
// Rendering the date
const currentDay = new Date();
const options = {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric'
}
const day = currentDay.toLocaleDateString("en-US", options);
// Setting the default value in the database
const input1 = new Input({
Name: "Welcome to your Todo List"
});
const input2 = new Input({
Name: "Hit + to add new task"
});
const input3 = new Input({
Name: "Hit the checkbox to delete the task"
})
const defautltItems = [input1, input2, input3];
// Setting the routes
app.get("/", (req, res) => {
Input.find({})
.then(Inputs => {
if (Inputs == 0) {
Input.insertMany(defautltItems).then(console.log("Tasks Added Sucessfully!!"))
.catch(error => {
console.error("Error fetching inputs:", error);
res.status(500).send("Internal Server Error");
});
}
else {
const title = "Today";
res.render('index', { listTitle: title, date: day, newListItems: Inputs });
}
console.log(Inputs);
})
.catch(error => {
console.error("The error shown is: ", error)
});
});
app.post("/", (req, res) => {
let listName = req.body.list;
const inputName = req.body.task;
const input = new Input({
Name: inputName
});
if (listName === "Today") {
input.save();
res.redirect("/");
}
else {
List.findOne({ Name: listName })
.then(foundList => {
if (foundList) {
foundList.Item.push(input);
foundList.save();
res.redirect("/" + listName);
}
else {
console.log("List not found");
res.redirect("/");
}
})
.catch(error => {
console.error(error);
res.redirect("/")
});
}
})
app.post("/delete", (req, res) => {
const checkedItemId = req.body.checkbox;
const listName = req.body.listName;
if (listName === "Today") {
Input.findByIdAndRemove(checkedItemId).then(res.redirect("/")).catch((error) => { console.error("The error is: ", error) });
}
else {
List.findOneAndUpdate({ Name: listName }, { $pull: { Item: { _id: checkedItemId } } })
.then(res.redirect("/" + listName)).catch((error) => { console.error("The error is: ", error) });
}
});
app.get("/about", (req, res) => {
res.render('about');
});
app.get("/:title", (req, res) => {
const title = _.capitalize(req.params.title);
List.findOne({ Name: title })
.then(foundList => {
if (!foundList) {
// Create a list
const list = new List({
Name: title,
Items: defautltItems
});
list.save();
console.log("List does not exist");
res.render('index', { title: title, listTitle: title, date: day, newListItems: defautltItems });
} else {
// Render the existing list
console.log("List exists");
res.render('index', { title: title, date: day, listTitle: title, newListItems: foundList.Item });
}
})
.catch(error => {
console.error(error);
});
});
app.listen(process.env.PORT || 3000, () => {
console.log("[Status] The server is running on port 3000");
});
// Vercel changes