-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
228 lines (168 loc) · 5.98 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
const express = require('express');
const app = express();
const port = process.env.PORT || 5000;
//cors for remote get and post request handling start
const cors = require('cors');
app.use(cors());
//cors for remote get and post request handling ends
//Mongo DB code for DB connect Start
const MongoClient = require('mongodb').MongoClient;
const ObjectId = require('mongodb').ObjectId;
const assert = require('assert');
//Mongo DB code for DB connect ends
//env config
require('dotenv').config();
//env config ends
// Mongo Connection URL
const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@cluster0.jcglm.mongodb.net/${process.env.DB_NAME}?retryWrites=true&w=majority`;
//body parse for getting body json data through API start
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
//body parse for getting body json data through API ends
app.get('/', (req, res) => {
res.send('Hello dear FOr testing');
})
// Mongo Use connect method to connect to the Server
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
client.connect(err => {
const serviceCollection = client.db("appsMakerBD").collection("services");
const portfolioCollection = client.db("appsMakerBD").collection("portfolio");
const adminUserCollection = client.db("appsMakerBD").collection("adminUser");
const reviewCollection = client.db("appsMakerBD").collection("review");
const orderCollection = client.db("appsMakerBD").collection("orders");
console.log('Database connected new');
//Product Add API
app.post('/addService', (req, res) => {
const services = req.body;
console.log(services);
serviceCollection.insertOne(services)
.then(result => {
console.log(services, 'service added');
res.send(result.insertedCount > 0);
})
})
//portfolio Add API
app.post('/addPortfolio', (req, res) => {
const portfolio = req.body;
console.log(portfolio);
portfolioCollection.insertOne(portfolio)
.then(result => {
console.log(portfolio, 'portfolio added');
res.send(result.insertedCount > 0);
})
})
//Showing All Portfolio API
app.get('/portfolio', (req, res) => {
portfolioCollection.find({})
.toArray((err, documents) => {
res.send(documents);
})
})
//Add Admin Add API
app.post('/addAdmin', (req, res) => {
const admin = req.body;
console.log(admin);
adminUserCollection.insertOne(admin)
.then(result => {
console.log(admin, 'admin added');
res.send(result.insertedCount > 0);
})
})
//Review Add API
app.post('/addReview', (req, res) => {
const review = req.body;
console.log(review);
reviewCollection.insertOne(review)
.then(result => {
console.log(review, 'review added');
res.send(result.insertedCount > 0);
})
})
//Showing All Reviews API
app.get('/clientReview', (req, res) => {
reviewCollection.find({})
.toArray((err, documents) => {
res.send(documents);
})
})
//Update Order Status Add API
app.post('/updateOrderStatus', (req, res) => {
const updatedData = req.body;
console.log(updatedData);
orderCollection.updateOne(
{ orderNumber: updatedData.orderNumber }, { $set: { status: updatedData.status }})
.then(function(result) {
// process result
if(result){
res.send(true);
}else{
res.send(false);
}
})
})
//Order Submit/Checkout API
app.post('/addOrder', (req, res) => {
const order = req.body;
//console.log(order);
orderCollection.insertOne(order)
.then(result => {
res.send(result.insertedCount > 0);
})
})
//Showing All Product API
app.get('/services', (req, res) => {
serviceCollection.find({})
.toArray((err, documents) => {
res.send(documents);
})
})
//Showing Product By ID API
app.get('/showServiceById/:id', (req, res) => {
const id = req.params.id;
serviceCollection.find({ _id: ObjectId(id) })
.toArray((err, documents) => {
//console.log(documents);
res.send(documents[0]);
})
})
//SHowing Order By email API
app.get('/showOrders/:email', (req, res) => {
const email = req.params.email;
adminUserCollection.find({ email: email })
.toArray((err, admin) => {
let filter = { email: email }
if (admin.length !== 0) {
filter = '';
}
console.log(admin.length);
orderCollection.find(filter)
.toArray((err, documents) => {
console.log(documents)
res.send(documents);
})
})
})
//Checking for Admin or User
app.post('/isAdmin', (req, res) => {
const email = req.body.email;
adminUserCollection.find({ email: email })
.toArray((err, admin) => {
//const response=doctors.length;
//console.log(admin.length);
res.send(admin.length !== 0)
})
})
//Delete Service by ID API
app.delete('/deleteService/:id', (req, res) => {
const id = ObjectId(req.params.id);
console.log('delete this', id);
serviceCollection.findOneAndDelete({ _id: id })
.then(documents => {
res.send(documents);
})
})
//client.close();
});
//last line of the code
app.listen(port);