-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (62 loc) · 1.55 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
const functions = require('firebase-functions')
const admin = require('firebase-admin')
const app = require('express')()
// Dot env config
require('dotenv').config()
var serviceAccount = require(`../${process.env.SERVICE_NAME}`)
/**
* Initialising the app with service account and db url
*/
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: process.env.DB_URL
})
/**
* Calling the firestore
*/
const db = admin.firestore()
/**
* API call to create quote
*/
app.post('/create', (req, res) => {
(async () => {
try {
await db.collection('quotes').doc('/' + req.body.id + '/')
.create({ author: req.body.author, quote: req.body.quote })
console.log('Record created')
return res.status(200).send()
} catch (error) {
console.log(error)
return res.status(500).send(error)
}
})()
})
/**
* Read Call
*/
app.get('/show', (req, res) => {
(
async () => {
try {
const query = db.collection('quotes')
const response = []
await query.get().then(querySnapshot => {
const docs = querySnapshot.docs
for (const doc of docs) {
const quotes = {
id: doc.id,
author: doc.data().author,
quote: doc.data().quote
}
response.push(quotes)
}
})
return res.status(200).send(response)
} catch (error) {
console.log(error)
return res.status(500).send(error)
}
}
)()
})
exports.app = functions.https.onRequest(app)