-
Notifications
You must be signed in to change notification settings - Fork 122
/
server.js
139 lines (102 loc) · 3.64 KB
/
server.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
138
139
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const port = process.env.PORT || 3001;
const app = express();
const { users, sportTeams, animals } = require('./data.js');
app.locals = {
title: 'Network Request Practice',
users,
sportTeams,
animals
}
app.use(cors());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.get('/api/v1/users', (req, res) => {
res.status(200).json(app.locals.users);
});
app.post('/api/v1/users', (req, res) => {
const newUser = req.body;
for (let requiredParameter of ['id', 'name', 'status', 'interests']) {
if (!newUser[requiredParameter]) {
return res.status(422).json({
message: `You are missing a required parameter of ${requiredParameter}`
});
}
}
const { id, name, status, interests } = newUser;
app.locals.users = [...app.locals.users, { id, name, status, interests }];
return res.status(201).json({ id, name, status, interests });
});
app.delete('/api/v1/users/:id', (req, res) => {
const { id } = req.params;
const match = app.locals.users.find(user => user.id == id);
if (!match) {
return res.status(404).json({
message: `No user found with an id of ${id}`
});
}
const filteredUsers = app.locals.users.filter(user => user.id != id);
app.locals.users = filteredUsers;
return res.status(200).json(app.locals.users);
});
app.get('/api/v1/sport-teams', (req, res) => {
res.status(200).json(app.locals.sportTeams);
});
app.post('/api/v1/sport-teams', (req, res) => {
const newSportTeam = req.body;
for (let requiredParameter of ['id', 'name', 'head_coach', 'sport']) {
if (!newSportTeam[requiredParameter]) {
return res.status(422).json({
message: `You are missing a required parameter of ${requiredParameter}`
});
}
}
const { id, name, head_coach, sport } = newSportTeam;
app.locals.sportTeams = [...app.locals.sportTeams, { id, name, head_coach, sport }];
return res.status(201).json({ id, name, head_coach, sport });
});
app.delete('/api/v1/sport-teams/:id', (req, res) => {
const { id } = req.params;
const match = app.locals.sportTeams.find(sportTeam => sportTeam.id == id);
if (!match) {
return res.status(404).json({
message: `No sport team found with an id of ${id}`
});
}
const filteredSportTeams = app.locals.sportTeams.filter(user => user.id != id);
app.locals.sportTeams = filteredSportTeams;
return res.status(200).send(app.locals.sportTeams);
});
app.get('/api/v1/animals', (req, res) => {
res.status(200).json(app.locals.animals);
});
app.post('/api/v1/animals', (req, res) => {
const newAnimal = req.body;
for (let requiredParameter of ['id', 'name', 'diet', 'fun_fact']) {
if (!newAnimal[requiredParameter]) {
return res.status(422).json({
message: `You are missing a required parameter of ${requiredParameter}`
});
}
}
const { id, name, diet, fun_fact } = newAnimal;
app.locals.animals = [...app.locals.animals, { id, name, diet, fun_fact }];
return res.status(201).json({ id, name, diet, fun_fact });
});
app.delete('/api/v1/animals/:id', (req, res) => {
const { id } = req.params;
const match = app.locals.animals.find(sportTeam => sportTeam.id == id);
if (!match) {
return res.status(404).json({
message: `No animal found with an id of ${id}`
});
}
const filteredAnimals = app.locals.animals.filter(user => user.id != id);
app.locals.animals = filteredAnimals;
return res.status(200).json(app.locals.animals);
});
app.listen(port, () => {
console.log(`${app.locals.title} is now running on http://localhost:${port} !`)
});