-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrest.api.ts
More file actions
65 lines (46 loc) · 1.52 KB
/
rest.api.ts
File metadata and controls
65 lines (46 loc) · 1.52 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
import * as functions from "firebase-functions";
import * as express from 'express'
import * as cors from 'cors'
import { auth, order } from '../middleware'
const admin = require('firebase-admin');
const db = admin.firestore()
const app = express();
app.use(cors({ origin: true }));
//rest api
//gets allOrders
app.get('/', auth.validate, async (req, res) => {
const snapshot = await db.collection('orders').get();
let orders: any = [];
snapshot.forEach((doc: any) => {
let id = doc.id
let data = doc.data();
orders.push({ id, ...data });
});
res.status(200).send(JSON.stringify(orders));
})
app.get('/:id', async (req, res) => {
const snapshot = await db.collection('orders').doc(req.params.id).get();
const orderId = snapshot.id
const orderData = snapshot.data();
res.status(200).send(JSON.stringify({ id: orderId, ...orderData }));
})
//giving data into the database
app.post('/', order.validate, async (req, res) => {
const order = req.body
await db.collection('orders').add(order);
res.status(201).send();
})
//updating the values which were updated by the user
app.put('/:id', async (req, res) => {
const body = req.body;
await db.collection('orders').doc(req.params.id).update({
...body
})
res.status(200).send();
})
//deleting the specified value
app.delete('/:id', async (req, res) => {
await db.collection('orders').doc(req.params.id).delete;
res.status(200).send();
})
export const orders = functions.https.onRequest(app)