-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
140 lines (124 loc) · 3.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
require('dotenv').config();
const express = require('express');
const app = express();
const cors = require('cors');
app.use(express.json());
app.use(cors());
const { MongoClient, ServerApiVersion, ObjectId } = require('mongodb');
const username = process.env.USERNAMED;
const password = encodeURIComponent(process.env.PASSWORD);
const cluster = process.env.CLUSTER;
const options = process.env.OPTIONS;
const uri = `mongodb+srv://${username}:${password}@${cluster}/${options}`;
const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
}
});
let db;
async function run() {
try {
await client.connect();
console.log("Connected to MongoDB!");
db = client.db("checkLists"); // replace with your database name
} catch (err) {
console.error(err);
}
}
run().catch(console.dir);
app.get('/data', async (req, res) => {
try {
const data = await db.collection('templates').find().toArray(); // replace with your collection name
res.json(data);
} catch (err) {
res.status(500).json({ error: 'Failed to fetch data' });
}
});
app.post('/data', async (req, res) => {
try {
const result = await db.collection('templates').insertOne(req.body);
res.json(result);
} catch (err) {
res.status(500).json({ error: 'Failed to insert data' });
}
});
app.put('/data/:id', async (req, res) => {
try {
const result = await db.collection('templates').updateOne(
{ _id: new ObjectId(req.params.id) },
{ $set: req.body }
);
res.json(result);
} catch (err) {
res.status(500).json({ error: 'Failed to update data' });
}
});
app.delete('/data/:id', async (req, res) => {
try {
const result = await db.collection('templates').deleteOne({ _id: new ObjectId(req.params.id) });
res.json(result);
} catch (err) {
res.status(500).json({ error: 'Failed to delete data' });
}
});
app.get('/checklists', async (req, res) => {
try {
const checklists = await db.collection('checkLists').find().toArray();
res.json(
checklists
);
} catch (err) {
res.status(500).json({ error: 'Failed to fetch checklists' });
}
});
app.get('/checklists/:id', async (req, res) => {
try {
const checklist = await db.collection('checkLists').findOne({ _id: new ObjectId(req.params.id) });
res.json(checklist);
} catch (err) {
res.status(500).json({ error: 'Failed to fetch checklist' });
}
});
app.post('/checklists', async (req, res) => {
try {
console.log(req?.body);
const checklist = {
...req.body,
createdOn: new Date().toISOString(),
updatedOn: new Date().toISOString(),
};
const result = await db.collection('checkLists').insertOne(checklist);
console.log(result);
res.json(result);
} catch (err) {
console.log(err);
res.status(500).json({ error: 'Failed to create checklist' });
}
});
app.put('/checklists/:id', async (req, res) => {
try {
const updateFields = {
...req.body,
updatedOn: new Date().toISOString(),
};
const result = await db.collection('checkLists').updateOne(
{ _id: new ObjectId(req.params.id) },
{ $set: updateFields }
);
res.json(result);
} catch (err) {
res.status(500).json({ error: 'Failed to update checklist' });
}
});
app.delete('/checklists/:id', async (req, res) => {
try {
const result = await db.collection('checkLists').deleteOne({ _id: new ObjectId(req.params.id) });
res.json(result);
} catch (err) {
res.status(500).json({ error: 'Failed to delete checklist' });
}
});
// Correct the port number
app.listen(3001, () => console.log('Server is running on port 3001'));