-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
108 lines (86 loc) · 2.73 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
const express = require("express");
const fetch = require("node-fetch");
const { redirect } = require("statuses");
let { destinations } = require("./db");
const { generateUniqueId, getUnsplashPhoto } = require("./services");
const cors = require("cors");
const server = express();
server.use(express.json());
server.use(cors());
server.use(express.urlencoded());
let PORT = process.env.PORT || 3000;
server.listen(PORT, function () {
console.log(`Server listening on {PORT}`);
});
// POST => create destinations
// data => {name^, location^, photo, description}
server.post("/destinations", async (req, res) => {
const { name, location, description } = req.body;
// Make sure we have a name AND location
if (
name === undefined ||
name.length === 0 ||
location === undefined ||
location.length === 0
) {
return res
.status(400)
.json({ message: "Name and Location are both required" });
}
const dest = { id: generateUniqueId(), name, location };
dest.photo = await getUnsplashPhoto({ name, location });
if (description && description.length !== 0) {
dest.description = description;
}
destinations.push(dest);
res.redirect("/destinations");
});
// GET => read destinations
// accepts the follow query parameters
// continent
server.get("/destinations", (req, res) => {
res.send(destinations);
});
// PUT => edit a destination
server.put("/destinations/", async (req, res) => {
const { id, name, location, description } = req.body;
if (id === undefined) {
return res.status(400).json({ message: "id is required" });
}
if (name !== undefined && name.length === 0) {
return res.status(400).json({ message: "Name can't be empty" });
}
if (location !== undefined && location.length === 0) {
return res.status(400).json({ message: "Location can't be empty" });
}
for (const dest of destinations) {
if (dest.id === id) {
if (name !== undefined) {
dest.name = name;
}
if (location !== undefined) {
dest.location = location;
}
if (name !== undefined || location !== undefined) {
dest.photo = await getUnsplashPhoto({
name: dest.name,
location: dest.location,
});
}
if (description !== undefined) {
dest.description = description;
}
return res.json(dest);
}
}
});
// DELETE => delete a destination
// HOW TO GET THE ID from the reqs
// route parameters /destinations/:id => req.params.id
// query /destinations?id=198745 => req.query.id
server.delete("/destinations/:id", (req, res) => {
const destId = req.params.id;
const newDestinations = destinations.filter((dest) => dest.id !== destId);
destinations = newDestinations;
res.redirect(303, "/destinations");
});