-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
121 lines (112 loc) · 4.01 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
const express = require('express')
const cors = require('cors')
const fs = require('fs')
const MongoClient = require('mongodb').MongoClient;
const fileupload = require('express-fileupload')
require('dotenv').config()
const port = 1929
const ObjectId = require('mongodb').ObjectId;
const app = express()
app.use(cors())
app.use(express.json())
app.use(express.static('serviceimg'))
app.use(fileupload())
app.get('/', (req, res) => {
res.send('Hello World!')
})
const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@cluster0.xzynl.mongodb.net/${process.env.DATABASE}?retryWrites=true&w=majority`;
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
client.connect(err => {
console.log(err);
const serviceCollection = client.db(`${process.env.DATABASE}`).collection("services");
const orderCollection = client.db(`${process.env.DATABASE}`).collection("orders");
const reviewCollection = client.db(`${process.env.DATABASE}`).collection("reviews");
const adminsCollection = client.db(`${process.env.DATABASE}`).collection("admins");
// Add Booking
app.post('/addBooking', (req, res) => {
orderCollection.insertOne(req.body)
.then(result => res.send(result.insertedCount > 0))
})
// Add Booking
app.post('/addReview', (req, res) => {
reviewCollection.insertOne(req.body)
.then(result => res.send(result.insertedCount > 0))
})
// Get Orderlist of User
app.post('/orderList', (req, res) => {
const email = req.body.email
adminsCollection.find({ email: email })
.toArray((err, docs) => {
if (docs.length > 0) {
orderCollection.find()
.toArray((err, docs) => res.send(docs))
} else {
orderCollection.find({ 'booking.email': email })
.toArray((err, docs) => res.send(docs))
}
})
})
// Add A Service
app.post('/addAService', (req, res) => {
console.log('service is adding..')
const file = (req.files.file)
const service = req.body.service
const description = req.body.description
const newImg = file.data;
const encImg = newImg.toString('base64');
const image = {
contentType: file.mimetype,
size: file.size,
img: Buffer.from(encImg, 'base64')
};
serviceCollection.insertOne({ service, description, image })
.then(result => {
res.send(result.insertedCount > 0);
})
})
// Get Reviews
app.get('/reviews', (req, res) => {
console.log('review req recieved')
reviewCollection.find().toArray((err, docs) => res.send(docs))
})
// Get Services
app.get('/services', (req, res) => {
serviceCollection.find()
.toArray((err, docs) => res.send(docs))
})
// Check Admin
app.post('/checkAdmin', (req, res) => {
const email = req.body.email
adminsCollection.find({ email: email })
.toArray((err, docs) => {
res.send(docs.length > 0)
})
})
// Add Admin
app.post('/addAdmin', (req, res) => {
const admin = req.body
adminsCollection.insertOne(admin)
.then(result => {
res.send(result.insertedCount > 0);
})
})
// Update Order
app.patch('/updateOrder', (req, res) => {
const status = req.body.status
orderCollection.updateOne({ _id: ObjectId(req.body.id) },
{
$set: { 'booking.status': status }
}
).then(result => {
res.send(result.modifiedCount > 0)
})
})
// Delete Service
app.delete('/deleteService/:id', (req, res) => {
serviceCollection.deleteOne({ _id: ObjectId(req.params.id) })
.then(result => {
res.send(result.deletedCount > 0);
})
})
});
app.listen(process.env.PORT || port)