-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
294 lines (259 loc) · 10.2 KB
/
index.js
File metadata and controls
294 lines (259 loc) · 10.2 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
const express = require('express')
const bodyParser = require('body-parser');
const cors = require('cors');
const ObjectId = require('mongodb').ObjectID;
const { MongoClient } = require('mongodb');
const admin = require("firebase-admin");
require('dotenv').config();
const port = process.env.PORT || 5000;
// Mongodb connection:
const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@cluster0.xgsrn.mongodb.net/${process.env.DB_MYDATA}?retryWrites=true&w=majority`;
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
// Google service account:
const serviceAccount = require("./configs/domainamex-firebase-adminsdk-bymxj-45a863d756.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
// Call the all packages:
const app = express()
app.use(bodyParser.json());
app.use(cors());
// Root url:
app.get('/', (req, res) => {
res.send("Hello domainamex-server, I'm ready for work.");
})
// Connect mongodb collection:
client.connect(err => {
const blogsCollection = client.db(`${process.env.DB_MYDATA}`).collection(`${process.env.DB_BLOGS}`);
const commentsCollection = client.db(`${process.env.DB_MYDATA}`).collection(`${process.env.DB_COMMENTS}`);
const themesCollection = client.db(`${process.env.DB_MYDATA}`).collection(`${process.env.DB_THEMES}`);
const ordersCollection = client.db(`${process.env.DB_MYDATA}`).collection(`${process.env.DB_ORDERS}`);
const adminsCollection = client.db(`${process.env.DB_MYDATA}`).collection(`${process.env.DB_ADMINS}`);
const contactsCollection = client.db(`${process.env.DB_MYDATA}`).collection(`${process.env.DB_CONTACTS}`);
console.log("Mongodb database connect okay");
// BLOGS ROUTES FUNCTIONS ----------------------------------------------------------------
// POST blogs to MDB cloud:
app.post('/addBlogs', (req, res) => {
const newBlog = req.body;
blogsCollection.insertOne(newBlog)
.then(result => {
res.send(result.insertedCount > 0)
})
})
// GET all blogs from MDB cloud:
app.get('/blogs', (req, res) => {
blogsCollection.find({})
.toArray((err, blogs) => {
res.send(blogs)
})
})
// GET same package blog (by topics) from MDB cloud:
app.get('/bloggers/:topics', (req, res) => {
blogsCollection.find({ "topics": req.params.topics })
.toArray((err, result) => {
res.send(result)
})
})
// GET single blog (by _id) from MDB cloud:
app.get('/blog-single/:id', (req, res) => {
blogsCollection.find({ "_id": ObjectId(req.params.id) })
.toArray((err, result) => {
res.send(result[0])
})
})
// Patch/update to mongodb database: DashboardCode
app.patch("/updateBlog/:id", (req, res) => {
blogsCollection.updateOne(
{ _id: ObjectId(req.params.id) },
{
$set: {
title: req.body.title,
category: req.body.category,
author: req.body.author,
date: req.body.date,
image: req.body.image,
description: req.body.description,
topics: req.body.topics,
tags: req.body.tags,
}
})
.then(result => {
res.send(result.modifiedCount > 0);
})
})
// Delete one blog from MDB cloud: DashboardCode
app.delete('/deleteBlog/:id', (req, res) => {
blogsCollection.deleteOne({ _id: ObjectId(req.params.id) })
.then(result => {
res.send(result.deletedCount > 0);
})
})
// COMMENTS ROUTES FUNCTIONS ----------------------------------------------------------------
// POST comment to MDB cloud:
app.post('/addComments', (req, res) => {
const newComment = req.body;
commentsCollection.insertOne(newComment)
.then(result => {
res.send(result.insertedCount > 0)
})
})
// GET specific blog comments (by blogID) from MDB cloud:
app.get('/comments', (req, res) => {
commentsCollection.find({})
.toArray((err, comments) => {
res.send(comments)
})
})
// THEMES ROUTES FUNCTIONS ----------------------------------------------------------------
// POST themes to mongodb cloud:
app.post('/addThemes', (req, res) => {
const newTheme = req.body;
themesCollection.insertOne(newTheme)
.then(result => {
res.send(result.insertedCount > 0)
})
})
// GET all themes from MDB cloud:
app.get('/themes', (req, res) => {
themesCollection.find({})
.toArray((err, themes) => {
res.send(themes)
})
})
// Patch/update to mongodb database: DashboardCode
app.patch("/updateTheme/:id", (req, res) => {
themesCollection.updateOne(
{ _id: ObjectId(req.params.id) },
{
$set: {
name: req.body.name,
price: req.body.price,
brand: req.body.brand,
category: req.body.category,
author: req.body.author,
date: req.body.date,
url: req.body.url,
detail: req.body.detail,
version: req.body.version,
image: req.body.image,
discount: req.body.discount,
relaced: req.body.relaced,
react: req.body.react,
code: req.body.code,
}
})
.then(result => {
res.send(result.modifiedCount > 0);
})
})
// Delete one theme from MDB cloud: DashboardCode
app.delete('/deleteTheme/:id', (req, res) => {
themesCollection.deleteOne({ _id: ObjectId(req.params.id) })
.then(result => {
res.send(result.deletedCount > 0);
})
})
// ORDERS ROUTES FUNCTIONS ----------------------------------------------------------------
// POST orders to the MDB cloud:
app.post('/addOrder', (req, res) => {
const newOrder = req.body;
ordersCollection.insertOne(newOrder)
.then(result => {
res.send(result.insertedCount > 0);
})
})
// GET all orders from the MDB cloud:
app.get('/orders', (req, res) => {
const bearer = (req.headers.authorization);
if (bearer && bearer.startsWith('Bearer ')) {
const idToken = bearer.split(' ')[1];
// console.log({ idToken });
// idToken comes from the client app
admin.auth().verifyIdToken(idToken)
.then((decodedToken) => {
const tokenEmail = decodedToken.email;
const queryEmail = req.query.email;
// console.log(tokenEmail, queryEmail);
if (tokenEmail == queryEmail) {
adminsCollection.find({ email: queryEmail })
.toArray((error, results) => {
if (results.length === 0) {
ordersCollection.find({ email: queryEmail })
.toArray((err, documents) => {
res.status(200).send(documents);
})
}
else {
ordersCollection.find({})
.toArray((err, services) => {
res.send(services)
})
}
})
}
else {
res.status(401).send('Unathorised access. Please try again letter!');
}
})
.catch((error) => {
res.status(401).send('Unathorised access. Please try again letter!');
});
}
else {
res.status(401).send('Unathorised access. Please try again letter!');
}
})
// Patch/update to mongodb database: DashboardCode
app.patch("/updateOrder/:id", (req, res) => {
ordersCollection.updateOne(
{ _id: ObjectId(req.params.id) },
{
$set: { status: req.body.status }
}
)
.then(result => {
res.send(result.modifiedCount > 0);
})
})
// ADMINS ROUTES FUNCTIONS ----------------------------------------------------------------
// POST admins to the MDB cloud:
app.post('/addAdmins', (req, res) => {
const newAdmin = req.body;
adminsCollection.insertOne(newAdmin)
.then(result => {
res.send(result.insertedCount > 0);
})
})
// GET all admins from the MDB cloud:
app.get('/admins', (req, res) => {
adminsCollection.find({})
.toArray((err, admins) => {
res.send(admins)
})
})
// GET/Check admins account from MDB cloud:
app.post('/checkAdmins', (req, res) => {
const email = req.body.email;
adminsCollection.find({ email: email })
.toArray((err, admins) => {
res.send(admins.length > 0)
})
})
// CONTACTS ROUTES FUNCTIONS ----------------------------------------------------------------
// POST contact to the MDB cloud:
app.post('/addContact', (req, res) => {
const newContact = req.body;
contactsCollection.insertOne(newContact)
.then(result => {
res.send(result.insertedCount > 0);
})
})
// GET all admins from the MDB cloud:
app.get('/contacts', (req, res) => {
contactsCollection.find({})
.toArray((err, contacts) => {
res.send(contacts)
})
})
});
app.listen(port);