forked from cs-4241-2024/a3-persistence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
215 lines (171 loc) · 5.67 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
require("dotenv").config();
let loggedInUser = null;
const express = require("express"),
{ MongoClient, ObjectId } = require("mongodb"),
cookie = require( 'cookie-session' ),
app = express()
app.use(express.static("public") )
app.use(express.json() )
app.use( express.urlencoded({ extended:true }) )
// cookie middleware! The keys are used for encryption and should be
// changed
app.use( cookie({
name: 'session',
keys: ['key1', 'key2']
}))
app.post( '/login', async (req, res) => {
const user = req.body.username;
const password = req.body.password;
const username = await client.db("datatest").collection("accounts").findOne({ username: user });
if(username !== null){
if(username.password === password){
req.session.login = true
loggedInUser = user;
res.redirect( 'main.html' )
}else{
return res.status(401).send("Incorrect password.");
}
}else{
return res.status(404).send("Username not found.");
}
})
// add some middleware that always sends unauthenicaetd users to the login page
app.use( function( req,res,next) {
if( req.session.login === true )
next()
else
res.sendFile( __dirname + '/public/index.html' )
})
// serve up static files in the directory public
app.use( express.static('public') )
const uri = 'mongodb+srv://asjacob:[email protected]/?retryWrites=true&w=majority&appName=Cluster0'
const client = new MongoClient( uri )
let collection = null
let collection2 = null
async function run() {
console.log("Hi");
await client.connect()
console.log("Connected to DB");
collection = await client.db("datatest").collection("List")
collection2 = await client.db("datatest").collection("accounts")
}
app.post("/docs", async (req, res) => {
if (collection !== null) {
const docs = await collection.find({}).toArray()
console.log("Docs" + docs)
res.json( docs )
}
})
run()
app.listen(3000)
console.log("listening on port 3000");
app.use( (req,res,next) => {
if( collection !== null ) {
next()
}else{
res.status( 503 ).send()
}
})
app.post("/delete-doc", async (req, res) => {
if (collection !== null) {
const priorityNumber = req.body.id +1;
const result = await collection.findOneAndDelete({ priority: priorityNumber });
// Retrieve and sort all remaining documents by type (reverse order) and date
const docs = await collection.find({ user: loggedInUser } )
.sort({ type: -1, date: 1 }) // Sort type in reverse order and date in ascending order
.toArray();
// Update priority based on the new sorted order
const updatedDocs = docs.map((doc, index) => ({
...doc,
priority: index + 1
}));
// Bulk update documents with the new priority
await Promise.all(updatedDocs.map(doc =>
collection.updateOne({ _id: doc._id }, { $set: { priority: doc.priority } })
));
// Retrieve and return all documents from the collection in the desired order
const finalDocs = await collection.find({})
.sort({ type: -1, date: 1 }) // Again sort by type in reverse order and date in ascending order
.toArray();
res.json(finalDocs);
}
});
app.post('/addAccount', async (req, res) => {
const account = req.body;
try {
await collection2.insertOne(account);
res.status(201).json({ message: 'Account created successfully' }); // Success response
} catch (error) {
console.error(error);
res.status(500).json({ message: 'Failed to create account' }); // Error response
}
});
// Route to add a document and return all documents
app.post("/add-doc", async (req, res) => {
console.log("adding item");
if (collection !== null) {
// Add a new item to the collection
const newDoc = req.body;
if (Object.keys(newDoc).length !== 0) {
if (newDoc.type === 'work') {
newDoc.priority = 1;
} else if (newDoc.type === 'school') {
newDoc.priority = 2;
} else if (newDoc.type === 'personal') {
newDoc.priority = 3;
}
newDoc.user = loggedInUser;
await collection.insertOne(newDoc);
// Update priority by type and date and order
const docs = await collection.find({ user: loggedInUser }).sort({ type: -1, date: 1 }).toArray();
const updatedDocs = docs.map((doc, index) => ({
...doc,
priority: index + 1
}));
// update documents with the new priority
await Promise.all(updatedDocs.map(doc =>
collection.updateOne({ _id: doc._id }, { $set: { priority: doc.priority } })
));
}
const docs = await collection.find({ user: loggedInUser }).sort({ type: -1, date: 1 }).toArray();
console.log(docs);
res.json(docs);
} else {
res.status(503).send("Service unavailable");
}
});
app.post("/update-doc", async (req, res) => {
if (collection !== null) {
const { ToDo, type, date } = req.body;
// Validate input
if (!ToDo || !type || !date) {
return res.status(400).send("Invalid input");
}
// Prepare the document to update
const updatedDoc = {
ToDo,
type,
date
};
// Update the document with the given id
const result = await collection.updateOne({ priority: parseInt(req.body.priority) },
{
$set: {
ToDo: req.body.ToDo,
type: req.body.type,
date: req.body.date
}
});
console.log(result, req.body)
if (result.matchedCount === 0) {
return res.status(404).send("Document not found");
}
// Retrieve and return all documents from the collection in the desired order
const docs = await collection.find({ user: loggedInUser })
.sort({ type: -1, date: 1 }) // Sort type in reverse order and date in ascending order
.toArray();
res.json(docs);
} else {
res.status(503).send("Service unavailable");
}
});