-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
139 lines (125 loc) · 3.9 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
// Require necessary modules
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const _ = require("lodash");
// Create Express app
const app = express();
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
mongoose.connect("mongodb+srv://adil:Test123@cluster0.xtynrsw.mongodb.net/todolistDB");
const itemsSchema = new mongoose.Schema({
name: String,
});
const Item = mongoose.model("Item", itemsSchema);
const defaultItems = [
{ name: "Welcome to your todolist!" },
{ name: "Hit the + button to add a new item" },
{ name: "<-- check the box to delete the item" },
];
// Item.insertMany(defaultItems);
const listSchema = {
name: String,
items: [itemsSchema],
};
const List = mongoose.model("List", listSchema);
// Define routes
app.get("/", async (req, res) => {
try {
const todos = await Item.find();
res.render("list", { listTitle: "Today", newListItems: todos });
} catch (error) {
res
.status(500)
.json({ message: "Error fetching todo items", error: error.message });
}
});
app.get("/:catagory", async (req, res) => {
const customListName = _.toLower(req.params.catagory);
try {
const foundList = await List.findOne({ name: customListName });
if (!foundList) {
console.log("List with customListName does not exist.");
//make new List in mongoose db
const list = new List({
name: customListName,
items: defaultItems,
});
await list.save();
res.render("list", {
listTitle: _.capitalize(customListName), // change to upper case
newListItems: defaultItems,
});
} else {
console.log("List with customListName already exists:", foundList);
// show an existing list
res.render("list", {
listTitle: _.capitalize(foundList.name), // changed to capitalized
newListItems: foundList.items,
});
}
} catch (err) {
console.error("Error finding the list:", err);
}
});
app.post("/", async (req, res) => {
let itemName = req.body.todoitem;
const listName = _.toLower(req.body.list); // to lower case
const item = new Item({
name: itemName,
});
if (listName === "today") {
item.save();
res.redirect("/");
} else {
try {
const foundList = await List.findOne({ name: listName });
foundList.items.push(item);
await foundList.save();
res.redirect("/" + listName);
} catch (err) {
console.error("Error finding the list:", err);
}
}
});
app.post("/delete", (req, res) => {
const checkedItemId = req.body.checkBox;
const listTitle = _.toLower(req.body.listName); // change case
//todo: remove from database whose checkbox is ticked
console.log(listTitle + "is name of list & id=" + checkedItemId);
if (listTitle === "today" && checkedItemId != null) {
deleteItem();
res.redirect("/");
} else {
deleteItemFromList();
res.redirect("/" + listTitle); // TODO:redirect to where it came
}
async function deleteItem() {
try {
const result = await Item.findByIdAndRemove(checkedItemId);
console.log("deleted successfully :" + result);
} catch (error) {
console.log(`Error: ` + error);
}
}
async function deleteItemFromList() {
// checkedItemId listTitle
const condition = { name: listTitle };
const update = { $pull: { items: { _id: checkedItemId } } };
const options = { new: true }; // Set the 'new' option to true to return the updated list after the update
const updatedList = await List.findOneAndUpdate(condition, update, options);
console.log("updated list" + updatedList);
}
});
app.get("/about", (req, res) => {
res.render("about");
});
// Start the server
let port = process.env.PORT;
if (port == null || port == "") {
port = 3000;
}
app.listen(port, () => {
console.log("Server is running on port 3000");
});