-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
177 lines (137 loc) · 4.75 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import Express from "express";
import bodyParser from "body-parser";
import connectDB from "./db/connectDB.js";
import itemSchema from "./models/itemSchema.js";
import { model } from "mongoose";
import dotenv from 'dotenv';
// Load environment variables from .env file
dotenv.config();
const app = Express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(Express.static("public"));
const DATABASE_URI = process.env.DATABASE_URI || "mongodb://localhost:27017";
const connectionOptions = JSON.parse(process.env.MONGODB_OPTIONS || {
dbName: 'todokeeperDB',
});
// Connect to MongoDB
await connectDB(DATABASE_URI, connectionOptions);
const HomeItem = new model("homeItem", itemSchema);
const WorkItem = new model("workItem", itemSchema);
app.get("/", async (req, res) => {
try{
const homeList = await HomeItem.find();
// console.log(homeList);
const data = {
name: "home",
list: homeList,
heading: "Today"
};
res.render("index.ejs", data);
}catch(error){
console.error(error);
return res.status(500).json({ message: 'Internal Server Error' });
}
});
app.get("/work", async (req, res) => {
try{
const workList = await WorkItem.find();
// console.log(workList);
const data = {
name: "work",
list: workList,
heading: "Work"
};
res.render("index.ejs", data);
}catch(error){
console.error(error);
return res.status(500).json({ message: 'Internal Server Error' });
}
});
app.post("/addItem", async (req, res) => {
const name = Object.keys(req.body)[1];
const newItem = req.body["newItem"];
try{
if (name === "home") {
const item = HomeItem({ name: newItem });
const result = await item.save();
console.log(result);
res.redirect("/");
} else {
const item = WorkItem({ name: newItem });
const result = await item.save();
console.log(result);
res.redirect("/work");
}
}catch(error){
console.error(error);
return res.status(500).json({ message: 'Internal Server Error' });
}
});
app.post("/itemDone", async (req, res) => {
console.log(req.body);
const name = req.body["name"];
let isDone = false;
if (req.body.isDone) {
isDone = req.body.isDone === "on";
}
const itemId = req.body["itemId"];
if (name === "home") {
try {
const updateData = { "isDone": isDone };
// Find the item by its ID and update it using $set operator to update specific fields
const updatedItem = await HomeItem.findByIdAndUpdate(itemId, { $set: updateData }, { new: true });
if (!updatedItem) {
return res.status(404).json({ message: 'Item not found' });
}
res.redirect("/");
} catch (error) {
console.error(error);
return res.status(500).json({ message: 'Internal Server Error' });
}
} else {
try {
const updateData = { "isDone": isDone };
// Find the item by its ID and update it using $set operator to update specific fields
const updatedItem = await WorkItem.findByIdAndUpdate(itemId, { $set: updateData }, { new: true });
if (!updatedItem) {
return res.status(404).json({ message: 'Item not found' });
}
res.redirect("/work");
} catch (error) {
console.error(error);
return res.status(500).json({ message: 'Internal Server Error' });
}
}
});
app.post("/deleteItem", async (req, res) => {
const name = req.body["name"];
const itemId = req.body["itemId"];
if (name === "home") {
try {
const deleteItem = await HomeItem.findByIdAndRemove(itemId);
if (!deleteItem) {
return res.status(500).json({ message: "Item not found" });
}
res.redirect("/");
} catch (error) {
console.error(error);
return res.status(500).json({ message: "Internal server error" });
}
} else {
try {
const deleteItem = await WorkItem.findByIdAndRemove(itemId);
if (!deleteItem) {
return res.status(500).json({ message: "Item not found" });
}
res.redirect("/work");
} catch (error) {
console.error(error);
return res.status(500).json({ message: "Internal server error" });
}
}
});
// Start the Express server
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server running on port ${port}...`);
});