-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrud.ts
94 lines (84 loc) · 2.23 KB
/
crud.ts
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
import * as functions from 'firebase-functions';
import * as express from 'express';
import { db } from './init';
const app = express();
const cors = require('cors');
app.use(cors({ origin: true }));
export const crudApi = functions.https.onRequest(app);
//GET all product With Id
app.get('/products', async (req, res) => {
try {
const docs = await db.collection('products').get();
res.send(
docs.docs.map((doc) => {
return {
productId: doc.id,
...doc.data(),
};
})
);
} catch (error) {
res.status(400).send(`Cannot get contacts: ${error}`);
}
});
// Get a Product byt ID
app.get('/products/:id', async (req, res) => {
try {
const productId = req.params.id;
await db
.collection('products')
.doc(`${productId}`)
.get()
.then((doc) => res.status(200).json({ doc }));
} catch (error) {
res.status(400).send(`Cannot get contacts: ${error}`);
}
});
// Add new product
app.post('/products', async (req, res) => {
try {
const products = await db.collection('products').add({
name: req.body['name'],
categories: req.body['categories'],
brand: req.body['brand']
});
res.status(200).send(`Created a new product: ${products.id}`);
} catch (error) {
console.log('Error From post req', error);
res.status(404).send(error);
}
});
// Update Product by Id
app.put('/products/:id', async (req, res) => {
try {
const productId = req.params.id;
const data = {
name: req.body['name'],
category: req.body['category'],
brand: req.body['brand'],
};
const updatedDoc = await db
.collection('products')
.doc(productId)
.set(data, { merge: true });
res.status(200).send(`Update a new contact: ${updatedDoc}`);
} catch (error) {
console.log(error);
res.status(404).send(error);
}
});
// Delete a contact
app.delete('/products/:id', async (req, res) => {
try {
const productId = req.params.id;
const deletedProduct = await db
.collection('products')
.doc(productId)
.delete();
res.status(200).send(`Product is deleted: ${deletedProduct}`);
} catch (error) {
console.log(error);
res.status(404).send(error);
}
});
export { app };