-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
292 lines (259 loc) · 8.01 KB
/
server.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const cors = require("cors");
app.use(express.static("public"));
app.use(cors());
app.use(express.json());
mongoose
.connect("mongodb+srv://gbujoreanu:[email protected]/?retryWrites=true&w=majority")
.then(() => console.log("Connected to MongoDB"))
.catch((error) => console.error("Couldn't connect to MongoDB", error));
// Define Mongoose Schemas and Models for each app
const taskSchema = new mongoose.Schema({
name: String,
description: String,
due_date: Date,
priority: String,
status: String
});
const Task = mongoose.model("Task", taskSchema);
const checklistSchema = new mongoose.Schema({
name: String,
items: [{ item: String, completed: Boolean }]
});
const Checklist = mongoose.model("Checklist", checklistSchema);
const goalSchema = new mongoose.Schema({
name: String,
description: String,
due_date: Date,
progress: Number,
notes: String
});
const Goal = mongoose.model("Goal", goalSchema);
const eventSchema = new mongoose.Schema({
date: Date,
time: String,
description: String
});
const Event = mongoose.model("Event", eventSchema);
// Task Endpoints
app.get("/api/tasks", async (req, res) => {
try {
const tasks = await Task.find();
res.send(tasks);
} catch (error) {
res.status(500).send("Error retrieving tasks: " + error.message);
}
});
app.post("/api/tasks", async (req, res) => {
try {
const newTask = new Task({
name: req.body.name,
description: req.body.description,
due_date: req.body.due_date,
priority: req.body.priority,
status: req.body.status
});
const savedTask = await newTask.save();
res.status(201).send(savedTask);
} catch (error) {
res.status(500).send("Error creating task: " + error.message);
}
});
app.put("/api/tasks/:id", async (req, res) => {
try {
const updatedTask = await Task.findByIdAndUpdate(
req.params.id,
{
name: req.body.name,
description: req.body.description,
due_date: req.body.due_date,
priority: req.body.priority,
status: req.body.status
},
{ new: true }
);
if (!updatedTask) {
return res.status(404).send("Task not found.");
}
res.send(updatedTask);
} catch (error) {
res.status(500).send("Error updating task: " + error.message);
}
});
app.delete("/api/tasks/:id", async (req, res) => {
try {
const deletedTask = await Task.findByIdAndDelete(req.params.id);
if (!deletedTask) {
return res.status(404).send("Task not found.");
}
res.send(deletedTask);
} catch (error) {
res.status(500).send("Error deleting task: " + error.message);
}
});
// Checklist Endpoints
app.get("/api/checklists", async (req, res) => {
try {
const checklists = await Checklist.find();
res.send(checklists);
} catch (error) {
res.status(500).send("Error retrieving checklists: " + error.message);
}
});
app.post("/api/checklists", async (req, res) => {
try {
const newChecklist = new Checklist({
name: req.body.name,
items: req.body.items
});
const savedChecklist = await newChecklist.save();
res.status(201).send(savedChecklist);
} catch (error) {
res.status(500).send("Error creating checklist: " + error.message);
}
});
app.put("/api/checklists/:id", async (req, res) => {
try {
const updatedChecklist = await Checklist.findByIdAndUpdate(
req.params.id,
{
name: req.body.name,
items: req.body.items
},
{ new: true }
);
if (!updatedChecklist) {
return res.status(404).send("Checklist not found.");
}
res.send(updatedChecklist);
} catch (error) {
res.status(500).send("Error updating checklist: " + error.message);
}
});
app.delete("/api/checklists/:id", async (req, res) => {
try {
const deletedChecklist = await Checklist.findByIdAndDelete(req.params.id);
if (!deletedChecklist) {
return res.status(404).send("Checklist not found.");
}
res.send(deletedChecklist);
} catch (error) {
res.status(500).send("Error deleting checklist: " + error.message);
}
});
// Goal-Tracker Endpoints
app.get("/api/goals", async (req, res) => {
try {
const goals = await Goal.find();
res.send(goals);
} catch (error) {
res.status(500).send("Error retrieving goals: " + error.message);
}
});
app.post("/api/goals", async (req, res) => {
try {
const newGoal = new Goal({
name: req.body.name,
description: req.body.description,
due_date: req.body.due_date,
progress: req.body.progress,
notes: req.body.notes
});
const savedGoal = await newGoal.save();
res.status(201).send(savedGoal);
} catch (error) {
res.status(500).send("Error creating goal: " + error.message);
}
});
app.put("/api/goals/:id", async (req, res) => {
try {
const updatedGoal = await Goal.findByIdAndUpdate(
req.params.id,
{
name: req.body.name,
description: req.body.description,
due_date: req.body.due_date,
progress: req.body.progress,
notes: req.body.notes
},
{ new: true }
);
if (!updatedGoal) {
return res.status(404).send("Goal not found.");
}
res.send(updatedGoal);
} catch (error) {
res.status(500).send("Error updating goal: " + error.message);
}
});
app.delete("/api/goals/:id", async (req, res) => {
try {
const deletedGoal = await Goal.findByIdAndDelete(req.params.id);
if (!deletedGoal) {
return res.status(404).send("Goal not found.");
}
res.send(deletedGoal);
} catch (error) {
res.status(500).send("Error deleting goal: " + error.message);
}
});
// Calendar Endpoints
app.get("/api/events", async (req, res) => {
try {
const events = await Event.find();
res.send(events);
} catch (error) {
res.status(500).send("Error retrieving events: " + error.message);
}
});
app.post("/api/events", async (req, res) => {
try {
const newEvent = new Event({
date: req.body.date,
time: req.body.time,
description: req.body.description
});
const savedEvent = await newEvent.save();
res.status(201).send(savedEvent);
} catch (error) {
res.status(500).send("Error creating event: " + error.message);
}
});
app.put("/api/events/:id", async (req, res) => {
try {
const updatedEvent = await Event.findByIdAndUpdate(
req.params.id,
{
date: req.body.date,
time: req.body.time,
description: req.body.description
},
{ new: true }
);
if (!updatedEvent) {
return res.status(404).send("Event not found.");
}
res.send(updatedEvent);
} catch (error) {
res.status(500).send("Error updating event: " + error.message);
}
});
app.delete("/api/events/:id", async (req, res) => {
try {
const deletedEvent = await Event.findByIdAndDelete(req.params.id);
if (!deletedEvent) {
return res.status(404).send("Event not found.");
}
res.send(deletedEvent);
} catch (error) {
res.status(500).send("Error deleting event: " + error.message);
}
});
app.get("/", (req, res) => {
res.sendFile(__dirname + "/public/hub/index.html");
});
app.listen(3000, () => {
console.log("Server is running on port 3000");
});