-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
166 lines (146 loc) · 5.18 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
const express = require('express');
const bodyParser = require('body-parser');
const mongoDB = require('mongodb');
const cors = require('cors');
let app = express();
app.use(bodyParser.json());
app.use(cors({
origin: true,
credentials: true,
}));
// Connecting to the database
let db;
mongoDB.MongoClient.connect('mongodb://localhost:27017/', {useUnifiedTopology: true}, (err, client) => {
db = client.db('cst3145-cw2');
console.log(client ? ('Connection established.') : (err));
});
// Define Collection and DB names
app.param('collectionName', (req, res, next, collectionName) => {
req.collection = db.collection(collectionName);
return next()
});
//Express end-routes
// Info message
app.get('/', (req, res) => {
res.send('Select a collection, e.g., /collections/messages')
});
// Gets all data from collection
app.get('/collections/:collectionName', (req, res, next) => {
req.collection.find({}).toArray((e, results) => {
if (e) return next(e);
res.send(results)
})
});
// Inserts a new query of data
app.post('/collections/:collectionName', (req, res, next) => {
req.collection.insert(req.body, (e, results) => {
if (e) return next(e);
res.send(results);
})
});
// retrieve data by email
app.get('/collections/:collectionName/:email', (req, res, next) => {
req.collection.find({email: (req.params.email)}).toArray((e, result) => {
if (e) return next(e);
res.send(result);
})
});
// get course by id
app.get('/collections/:collectionName/get/:id', (req, res, next) => {
req.collection.find(mongoDB.ObjectId(req.params.id)).toArray((e, result) => {
if (e) return next(e);
res.send(result);
})
});
// Get user who's status is ON
app.get('/collections/:collectionName/status/:status', (req, res, next) => {
req.collection.findOne({on: true}, (err, result) => {
if (err) return next(err);
res.send(result);
})
});
// retrieve data by email while registering
app.get('/collections/:collectionName/register/:email', (req, res, next) => {
req.collection.findOne({email: (req.params.email)}, (e, result) => {
if (e) return next(e);
res.send(result);
})
});
// update a user by email to logout
app.put('/collections/:collectionName/logout/:email', (req, res, next) => {
req.collection.updateOne({email: (req.params.email)},
{$set: {on: false}},
{returnOriginal: false, upsert: true},
(e, result) => {
if (e) return next(e);
res.send(result);
})
});
// update a course by id
app.put('/collections/:collectionName/put/:id', (req, res, next) => {
req.collection.updateOne({_id: mongoDB.ObjectId(req.params.id)},
{
$set: {
topic: req.body.topic, price: req.body.price, about: req.body.about,
rating: req.body.rating, location: req.body.location
}
},
{returnOriginal: false, upsert: true}, (e, result) => {
if (e) next(e);
res.send((result.result.n === 1) ? {msg: 'success'} : {msg: 'error'})
})
});
// reset ratings by id
app.put('/collections/:collectionName/rating/:id', (req, res, next) => {
req.collection.updateOne({
_id: mongoDB.ObjectId(req.params.id)
}, {$set: {rating: []}}, (e, result) => {
if (e) return next(e);
res.send(result)
})
});
// update ratings by id
app.put('/collections/:collectionName/update/:id-:user-:score', (req, res) => {
req.collection.findOneAndUpdate({
_id: mongoDB.ObjectID(req.params.id),
"rating.user": (req.params.user)
}, {$set: {"rating.$.score": (req.params.score)}})
.then(results => {
if (results.value === null) {
req.collection.findOneAndUpdate({
_id: mongoDB.ObjectID(req.params.id)
},
{$push: {rating: {user: (req.params.user), score: (req.params.score)}}}
)
.then(value => res.send(value))
.catch(err => res.send(err))
} else {
res.send(results)
}
})
.catch(err => res.send(err));
});
// validate login by email and password
app.put('/collections/:collectionName/:email/:password', (req, res, next) => {
req.collection.findOneAndUpdate({email: (req.params.email), password: (req.params.password)},
{$set: {on: true}})
.then(results => res.send(results))
.catch(err => res.send(err))
});
// delete a course by id
app.delete('/collections/:collectionName/:id', (req, res, next) => {
req.collection.deleteOne({_id: mongoDB.ObjectID(req.params.id)}, (e, result) => {
if (e) return next(e);
res.send((result.result.n === 1) ? {msg: 'success'} : {msg: 'error'})
})
});
// delete a user by email
app.delete('/collections/:collectionName/:email', (req, res, next) => {
req.collection.deleteOne({email: (req.params.email)}, (e, result) => {
if (e) return next(e);
res.send((result.result.n === 1) ? {msg: 'success'} : {msg: 'error'})
})
});
// Start the server
let port = 3000;
app.listen(port, console.log(`Server is listening on :${port}`));