-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
189 lines (167 loc) · 3.83 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
178
179
180
181
182
183
184
185
186
187
188
189
const express = require("express");
const cors = require("cors");
const Task = require("./schemas/Task.model");
const Note = require("./schemas/Notes.model");
const app = express();
const port = 8080;
function myMiddleware(req, res, next) {
console.log("This is my custom middleware.");
if (req.query.token === `${process.env.API_TOKEN_KEY}`) {
next();
} else {
res.status(401).send("Unauthorized");
}
}
app.use(express.json());
app.use(cors());
app.use(myMiddleware);
const mongoose = require("mongoose");
mongoose
.connect(`${process.env.MONGO_URI}`)
.then(() => console.log("Connected!"));
app.get("/", (req, res) => {
res.send("Hello World!");
});
const saveTask = async (title, description, dueDate, completed) => {
try {
const data = new Task({
title,
description,
dueDate,
completed,
});
const result = await data.save();
console.log(result);
} catch (error) {
console.log(error);
}
};
const saveNote = async (id, title, emoji, content, intrash) => {
try {
const data = new Note({
id,
title,
emoji,
content,
intrash: false,
});
const result = await data.save();
console.log(result);
} catch (error) {
console.log(error);
}
};
app.post("/add-task", (req, res) => {
console.log(req.body);
if (
!req.body.title ||
!req.body.dueDate ||
typeof req.body.completed === Boolean
) {
res.status(400).send("Please enter a title or a date");
}
try {
saveTask(
req.body.title,
req.body.description,
req.body.dueDate,
req.body.completed
);
res.status(200).send({
message: "Note saved",
});
} catch (error) {
res.send(error);
}
});
app.get("/get-all-tasks", async (req, res) => {
try {
const data = await Task.find().sort({ dueDate: -1 });
res.status(200).send(data);
} catch (error) {
res.send(error);
}
});
app.delete("/delete-task", async (req, res) => {
try {
const data = await Task.deleteOne({ _id: req.query.id });
res.status(200).send(data);
} catch (error) {
res.send(error);
}
});
app.put("/update-task", async (req, res) => {
try {
const data = await Task.findOneAndUpdate({ _id: req.query.id }, req.body);
res.status(200).send({
message: "Task updated successfully",
});
} catch (error) {
res.send(error);
}
});
app.listen(port, () => {
console.log(`Listening on port ${port}...`);
});
app.post("/add-note", (req, res) => {
console.log(req.body);
if (!req.body.title && !req.body.content) {
res.status(400).send("Please fill all details");
}
try {
saveNote(
req.body.id,
req.body.title,
req.body.emoji,
req.body.content,
req.body.intrash
);
res.status(200).send({
message: "Note saved",
});
} catch (error) {
res.send(error);
}
});
app.put("/update-note", async (req, res) => {
try {
const data = await Note.findOneAndUpdate({ _id: req.query.id }, req.body);
res.status(200).send({
message: "Note updated successfully",
});
} catch (error) {
res.send(error);
}
});
app.get("/get-note", async (req, res) => {
try {
const data = await Note.findById(req.query.id);
res.status(200).send(data);
} catch (error) {
res.send(error);
}
});
app.get("/get-all-notes", async (req, res) => {
try {
const data = await Note.find();
res.status(200).send(data);
} catch (error) {
res.send(error);
}
});
app.delete("/delete-note", async (req, res) => {
try {
const data = await Note.deleteOne({ _id: req.query.id });
console.log(data);
res.status(200).send(data);
} catch (error) {
res.send(error);
}
});
// const server = Bun.serve({
// port: 3000,
// fetch(req) {
// return new Response("Bun!");
// },
// });
console.log(`Listening on http://localhost:${port} ...`);