-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
192 lines (166 loc) · 5.16 KB
/
server.js
File metadata and controls
192 lines (166 loc) · 5.16 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
"use strict";
require("dotenv").config();
const express = require("express");
const app = express();
const cors = require("cors");
app.use(cors());
const mongoose = require("mongoose");
app.use(express.json());
const BucketListModel = require("./models/bucketList");
const PORT = process.env.PORT || 3001;
const countryRouter = require("./routes/countryRouter");
const foodRouter = require("./routes/foodRouter");
const advisoryRouter = require("./routes/advisoryRouter");
const enviroRouter = require("./routes/enviroRouter");
app.use("/country", countryRouter);
app.use("/food", foodRouter);
app.use("/advisory", advisoryRouter);
app.use("/enviro", enviroRouter);
app.get("/", (req, res) => {
res.send("Hello World");
});
// seed db route
app.use("/seed", seed);
// GET from db
// bucket route
app.get('/bucketList', async (req, res) => {
console.log('req', req.query.email);
let email = req.query.email;
try {
await BucketListModel.find({ email }, (err, bucket) => {
if (err) {
res.status(500).send('Cannot access the database');
} else {
res.status(200).send(bucket);
}
});
}
catch (err) {
res.status(500).send('Server error - fix and come back later!');
}
});
// POST new note to db
app.post('/bucketList', async (req, res) => {
let { country, countryCode, email, note } = req.body;
try {
let newBucket = new BucketListModel({ country, countryCode, email, note });
await newBucket.save();
res.status(200).send(newBucket);
}
catch (err) {
console.log('error', err);
res.status(500).send('Server error');
}
})
// // PUT req to update bucket list in db
// app.put('/bucketList/:countryCode', async (req, res) => {
// let bucketId = req.params.countryCode;
// console.log('req.body:', req.body)
// // user is defined with auth0
// try {
// await BucketListModel.findByIdAndDelete(bucketId, req.body);
// res.status(200).send('Updated your Bucket!');
// }
// catch (err) {
// console.log('updateError', err);
// res.status(500).send('Server error!');
// }
// })
// Marks
// PUT req to update bucket list in db
app.put('/bucketList/:id', async (req, res) => {
let bucketId = req.params.id;
try {
await BucketListModel.findByIdAndUpdate(bucketId, req.body);
res.status(200).send('Updated your Bucket!');
}
catch (err) {
console.log('updateError', err);
res.status(500).send('Server error!');
}
})
// DELETE request to delete a bucket from your list in db
app.delete('/bucketList/:id', async (req, res) => {
let bucketId = req.params.id;
console.log('req.query:', req.query);
try {
await BucketListModel.findByIdAndDelete(bucketId);
res.send('Deleted that bucket! You should go add another!');
}
catch (err) {
console.log('deleteError', err);
res.status(500).send('Server error!');
}
})
// test seed the db
// Seeding the database
async function seed(req, res) {
let testBuckets = await BucketListModel.find({});
if (testBuckets.length === 0) {
const testBucketOne = new BucketListModel({
countryCode: "CO",
email: "vbchomp@gmail.com",
note: "sure, let's go",
});
const testBucketTwo = new BucketListModel({
countryCode: "RE",
email: "vbchomp@gmail.com",
note: "Maybe in a few years.",
});
const testBucketThree = new BucketListModel({
countryCode: "PT",
email: "vbchomp@gmail.com",
note: "On the top of my list of places that I want to go!",
});
const testBucketFour = new BucketListModel({
countryCode: "KZ",
email: "vbchomp@gmail.com",
note: "I should look more into this.",
});
testBucketOne.save();
testBucketTwo.save();
testBucketThree.save();
testBucketFour.save();
console.log(
"I have excersiiiiiiiiiiized the database",
"http://localhost:3001/seed"
);
}
console.log("DB has been seeded");
}
// MONGODB_URI is the MongoDB Atlas Server URI saved as variable in heroku ***
// snagged directions on how to setup connection for Atlas Server from
// https://github.com/vbchomp/seattle-code-301n22/tree/main/class-14
// setup default db connection
mongoose.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
// connect to the default db connection
const db = mongoose.connection;
// get notifications of connections errors
db.on("error", (error) => console.error(error))
// ***seed a few test buckets. delete .then, seed(), and closing}) before production***
.then(async () => {
// seed();
// get notified when connected to db
db.once("open", () => console.log("Connected to Database"));
})
//clearing the database - USE ONLY IN CASES OF EXTREME PARANOIA
async function clear(req, res) {
try {
await BucketListModel.deleteMany({});
res.status(200).send('Ooops, I did again!');
}
catch (err) {
res.status(500).send('Uhhh, Houston we have a problem!');
}
}
// clear route - BE GENTLE with PARANOIA
app.get('/clear', clear);
app.get("*", (req, res) => {
res.status(404).send("Page not found");
});
app.listen(PORT, () => {
console.log(`listening on ${PORT}`);
});