forked from mdalmamunit427/mern-book-store-inventory-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
109 lines (90 loc) · 3.32 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
106
107
108
109
const express = require('express')
const app = express();
const cors = require('cors')
const port = process.env.PORT || 5000;
// middlewear
app.use(cors());
app.use(express.json());
app.get('/', (req, res) => {
res.send('Hello World!')
})
// mongodb confiq here
const { MongoClient, ServerApiVersion, ObjectId } = require('mongodb');
const uri = "mongodb+srv://bookManager:<password>@cluster0.lpzqr6l.mongodb.net/?retryWrites=true&w=majority";
// Create a MongoClient with a MongoClientOptions object to set the Stable API version
const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
}
});
async function run() {
try {
// Connect the client to the server (optional starting in v4.7)
await client.connect();
// Send a ping to confirm a successful connection
const bookCollections = client.db("BookInventory").collection("Books");
// insert a book to db: Post Method
app.post("/upload-book", async (req, res) => {
const data = req.body;
// console.log(data);
const result = await bookCollections.insertOne(data);
res.send(result);
})
// // get all books from db
// app.get("/all-books", async (req, res) => {
// const books = bookCollections.find();
// const result = await books.toArray();
// res.send(result)
// })
// get all books & find by a category from db
app.get("/all-books", async (req, res) => {
let query = {};
if (req.query?.category) {
query = { category: req.query.category }
}
const result = await bookCollections.find(query).toArray();
res.send(result)
})
// update a books method
app.patch("/book/:id", async (req, res) => {
const id = req.params.id;
// console.log(id);
const updateBookData = req.body;
const filter = { _id: new ObjectId(id) };
const updatedDoc = {
$set: {
...updateBookData
}
}
const options = { upsert: true };
// update now
const result = await bookCollections.updateOne(filter, updatedDoc, options);
res.send(result);
})
// delete a item from db
app.delete("/book/:id", async (req, res) => {
const id = req.params.id;
const filter = { _id: new ObjectId(id) };
const result = await bookCollections.deleteOne(filter);
res.send(result);
})
// get a single book data
app.get("/book/:id", async (req, res) => {
const id = req.params.id;
const filter = { _id: new ObjectId(id) };
const result = await bookCollections.findOne(filter);
res.send(result)
})
await client.db("admin").command({ ping: 1 });
console.log("Pinged your deployment. You successfully connected to MongoDB!");
} finally {
// Ensures that the client will close when you finish/error
// await client.close();
}
}
run().catch(console.dir);
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})