-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
137 lines (127 loc) · 4.49 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const express = require("express");
const cors = require("cors");
require("dotenv").config();
const app = express();
const port = process.env.PORT || 5000;
const { MongoClient, ServerApiVersion, ObjectId } = require("mongodb");
const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@cluster0.zbraxw8.mongodb.net/euroguide`;
// 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,
},
});
// middleware
// Middleware Connections
const corsConfig = {
origin: "https://magnificent-bavarois-f6e8db.netlify.app", //important
credentials: true,
};
app.use(cors(corsConfig));
app.use(express.json());
async function run() {
try {
// Connect the client to the server (optional starting in v4.7)
// await client.connect();
app.get("/countries", async (req, res) => {
try {
const db = client.db("euroguide");
const collection = db.collection("countries");
const documents = await collection.find().toArray();
// Send the retrieved documents as JSON response
res.json(documents);
} catch (error) {
console.error("Error fetching countries:", error);
}
});
app.get("/review", async (req, res) => {
try {
const db = client.db("euroguide");
const collection = db.collection("reviews");
const documents = await collection.find().toArray();
// Send the retrieved documents as JSON response
res.json(documents);
} catch (error) {
console.error("Error fetching reviews:", error);
}
});
app.get("/added_spot", async (req, res) => {
try {
// Select the database
const db = client.db("euroguide");
// Select the collection
const collection = db.collection("tourist_spots");
// Find documents in the collection
const documents = await collection.find().toArray();
// Send the retrieved documents as JSON response
res.json(documents);
} catch (error) {
console.error("Error fetching added spots:", error);
}
});
app.post("/added_spot", async (req, res) => {
const spot = req.body;
try {
// Select the database
const db = client.db("euroguide");
const collection = db.collection("tourist_spots");
// Insert the received data into the collection
const result = await collection.insertOne(spot);
res.send(result);
} catch (error) {
console.error("Error adding spot:", error);
}
});
app.put("/added_spot/:id", async (req, res) => {
const db = client.db("euroguide");
const collection = db.collection("tourist_spots");
const id = req.params.id;
const filter = { _id: new ObjectId(id) };
const options = { upsert: true };
const updateSpot = req.body;
const updatedSpot = {
$set: {
image: updateSpot.image,
tourists_spot_name: updateSpot.tourists_spot_name,
country_Name: updateSpot.country_Name,
location: updateSpot.location,
description: updateSpot.description,
average_cost: updateSpot.average_cost,
seasonality: updateSpot.seasonality,
travel_time: updateSpot.travel_time,
totalVisitorsPerYear: updateSpot.totalVisitorsPerYear,
},
};
const result = await collection.updateOne(filter, updatedSpot, options);
res.send(result);
});
app.delete("/added_spot/:id", async (req, res) => {
const db = client.db("euroguide");
const collection = db.collection("tourist_spots");
const id = req.params.id;
const query = { _id: new ObjectId(id) };
const result = await collection.deleteOne(query);
res.send(result);
});
app.get("/added_spot/:id", async (req, res) => {
const db = client.db("euroguide");
const collection = db.collection("tourist_spots");
const id = req.params.id;
const query = { _id: new ObjectId(id) };
const result = await collection.findOne(query);
res.send(result);
});
await client.db("admin").command({ ping: 1 });
console.log(
"Pinged your deployment. You successfully connected to MongoDB!"
);
} catch (error) {
console.error("Error connecting to MongoDB:", error);
}
}
run().catch(console.dir);
app.listen(port, () => {
console.log("Server is running on port:", port);
});