-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
105 lines (92 loc) · 2.79 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
const express = require("express");
const app = express();
const port = process.env.PORT || 8000;
const cors = require("cors");
const {
MongoClient,
ServerApiVersion,
Collection,
ObjectId,
} = require("mongodb");
require("dotenv").config();
app.use(cors());
app.use(express.json());
const uri = `mongodb+srv://AbiAbdullah:[email protected]/myFirstDatabase?retryWrites=true&w=majority`;
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
serverApi: ServerApiVersion.v1,
});
async function run() {
try {
await client.connect();
const productCollection = client.db("products").collection("items");
const reviewsCollection = client.db("products").collection("review");
// Get all Product items Collection
app.get("/product", async (req, res) => {
const page = parseInt(req.query.page);
const size = parseInt(req.query.size);
const qurey = {};
const cursor = productCollection.find(qurey);
let products;
if (page || size) {
products = await cursor
.skip(page * size)
.limit(size)
.toArray();
} else {
products = await cursor.toArray();
}
res.send(products);
});
// Add Product
app.post("/product", async (req, res) => {
const qurey = req.body;
const product = await productCollection.insertOne(qurey);
res.send(product);
});
// Product Details
app.get("/product/:id", async (req, res) => {
const id = req.params.id;
const qurey = { _id: ObjectId(id) };
const productId = await productCollection.findOne(qurey);
res.send(productId);
});
// My Products Find Email Address
app.get("/myProducts", async (req, res) => {
const email = req.query.email;
const query = { email: email };
const cursor = productCollection.find(query);
const result = await cursor.toArray();
res.send(result);
});
// Product Count Pagination
app.get("/productCount", async (req, res) => {
const query = req.body;
const cursor = productCollection.find(query);
const count = await cursor.count();
res.send({ count });
});
// Get All review All product
app.get("/review", async (req, res) => {
const qurey = {};
const cursor = reviewsCollection.find(qurey);
const review = await cursor.toArray();
res.send(review);
});
// Add Reviews
app.post("/review", async (req, res) => {
const query = req.body;
const review = await reviewsCollection.insertOne(query);
res.send(review);
});
} finally {
}
}
run().catch(console.dir);
app.get("/", (req, res) => {
res.send("Hello World!");
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});