-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
96 lines (84 loc) · 2.68 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
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const MongoClient = require('mongodb').MongoClient;
const ObjectId = require('mongodb').ObjectId;
require('dotenv').config();
// ===========================================================
// Default Port:
const port = 5000;
const app = express();
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// Mongodb Connection:
const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@cluster0.jqvnn.mongodb.net/${process.env.DB_NAME}?retryWrites=true&w=majority`;
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
client.connect((err) => {
// Collection
const tasksCollection = client.db('volunteerNetwork').collection('tasks');
const registrationCollection = client
.db('volunteerNetwork')
.collection('registration');
// Add Volunteer Registration (Create)
app.post('/addRegistration', (req, res) => {
const newRegistration = req.body;
registrationCollection.insertOne(newRegistration).then((result) => {
res.send(result.insertedCount > 0);
});
});
// Show data for login user (Read)
app.get('/userTasks', (req, res) => {
// console.log(req.query.email)
registrationCollection
.find({ email: req.query.email })
.toArray((err, documents) => {
res.send(documents);
});
});
// Admin dashboard, show all register (Read)
app.get('/adminTasks', (req, res) => {
registrationCollection.find({}).toArray((err, documents) => {
res.send(documents);
});
});
// Add event (create)
app.post('/addEvent', (req, res) => {
const taskEvent = req.body;
tasksCollection.insertOne(taskEvent).then((result) => {
// console.log(result)
res.send(result.insertedCount > 0);
});
});
// Read tasksData from mongo:
app.get('/taskEvents', (req, res) => {
tasksCollection.find({}).toArray((err, documents) => {
res.send(documents);
});
});
// Read single task from mongo based on user selection:
app.get('/taskEvents/:_id', (req, res) => {
tasksCollection
.find({ _id: ObjectId(req.params._id) })
.toArray((err, documents) => {
res.send(documents[0]);
});
});
// Delete Task from UserDashboard and Admin Dashboard
app.delete('/deleteTask/:_id', (req, res) => {
registrationCollection
.deleteOne({ _id: ObjectId(req.params._id) })
.then((result) => {
res.send(result.deletedCount > 0);
});
});
});
// Root:
app.get('/', (req, res) => {
res.send('The Server is running');
});
// Listener port
app.listen(process.env.PORT || port);