-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
147 lines (127 loc) · 3.24 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
//Importing server libraries
const express = require("express");
const app = express();
const cors = require("cors");
//Importing database
const { connectDatabase } = require("./db/database");
connectDatabase();
const { modal } = require("./db/schema");
//Setting port number
const port = 4000;
// Middlewares
app.use(express.json());
app.use(express.urlencoded());
app.use(cors());
app.use(express.static(__dirname+"/renderer"))
// Server
app.listen(port, () => {
console.log(`http://localhost:${4000}`);
});
// Routes
app.get('/', (req, res)=>{
res.sendFile('/index.html')
})
app.get("/rendernotes", async(req, res) => {
const allNotes = await modal.find({});
if (allNotes.length === 0) {
return res
.status(200)
.json({ success: true, message: "Notes fetched", allNotes: [] });
}
try {
res.status(200).json({ success: true, message: "Notes fetched", allNotes });
} catch (error) {
res.status(400).json({ success: false, message: error.message });
}
});
app.post("/createnote", async (req, res) => {
const { title, description } = req.body;
const time = new Date();
const formattedTime = time.toLocaleTimeString("en-IN", {
hour12: true,
hour: "2-digit",
minute: "2-digit",
});
const date = new Date();
const formattedDate = date.toLocaleDateString("en-IN", {
weekday: "short",
day: "2-digit",
month: "short",
year: "2-digit",
});
try {
const createdNote = await modal.create({
title:title===""?"":title,
description:description===""?"":description,
dateTime: `${formattedTime} ${formattedDate}`,
});
res.status(200).json({
success: true,
message: "Note created",
createdNote,
});
} catch (error) {
res.status(400).json({ success: false, message: error.message });
}
});
app.post("/modifynote", async (req, res) => {
const { id, newTitle, newDescription } = req.body;
const noteToBeModified = modal.findById(id);
const time = new Date();
const formattedTime = time.toLocaleTimeString("en-IN", {
hour12: true,
hour: "2-digit",
minute: "2-digit",
});
const date = new Date();
const formattedDate = date.toLocaleDateString("en-IN", {
weekday: "short",
day: "2-digit",
month: "2-digit",
year: "2-digit",
});
try {
if (!noteToBeModified) {
return res.status(404).json({
success: false,
message: "ID not found. Enter the ID that exists.",
});
}
await modal.findByIdAndUpdate(id, {
title: newTitle,
description: newDescription,
dateTime: `${formattedTime} ${formattedDate} (Modified)`,
});
res.status(200).json({
success: true,
message: "Note modified",
modifiedNote: {
id: id,
title: newTitle,
description: newDescription,
},
});
} catch (error) {
res.status(400).json({ success: false, message: error.message });
}
});
app.post("/deletenote", async (req, res) => {
const { id } = req.body;
const noteToBeDeleted = await modal.findById(id);
try {
if (!noteToBeDeleted) {
return res.status(404).json({
success: false,
message: "ID not found. Enter the ID that exists.",
});
}
const deletedNote = await modal.deleteOne({ _id: id });
res.status(200).json({
success: true,
message: "Note deleted",
deletedNote,
});
} catch (error) {
res.status(400).json({ success: false, message: error.message });
}
});