-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
174 lines (143 loc) · 4.51 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
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
const express = require('express');
const app = express();
const cors = require('cors');
const environment = process.env.NODE_ENV || 'development';
const configuration = require('./knexfile')[environment];
const database = require('knex')(configuration);
app.set('port', process.env.PORT || 3000);
app.use(express.json());
app.use(cors());
app.listen(app.get('port'), () => {
console.log(`App is running on 'http://localhost:${app.get('port')}'`);
});
app.get('/', (request, response) => {
response.status(200).send('Welcome to Brews!');
});
// get all breweries
app.get('/api/v1/breweries', (request, response) => {
database('breweries').select()
.then(breweries => {
response.status(200).json(breweries);
})
.catch(error => {
response.status(500).json({ error });
})
});
// get all beers
app.get('/api/v1/beers', (request, response) => {
database('beers').select()
.then(beers => {
response.status(200).json(beers);
})
.catch(error => {
response.status(500).json({ error });
})
});
// get a random beer
app.get('/api/v1/beers/random', (request, response) => {
database('beers').select()
.then(data => {
const count = data.length;
const randomIndex = Math.floor(Math.random() * Math.floor(count - 1));
const randomBeer = data[randomIndex];
response.status(200).json(randomBeer);
})
.catch(error => {
response.status(500).json({ error });
})
})
// get all beers for a given brewery
app.get('/api/v1/beers/:brewery_id', (request, response) => {
let { brewery_id } = request.params;
let breweryId = parseInt(brewery_id);
database('beers').where('brewery_id', breweryId).select()
.then(data => {
if (data.length) {
response.status(200).json(data);
} else {
response.status(404).json({ error: 'Could not find any beers for the selected brewery.' });
}
})
.catch(error => {
response.status(500).json({ error });
})
});
// add a brewery
app.post('/api/v1/breweries', (request, response) => {
const brewery = request.body;
for (let requiredParameter of ['name', 'year_est', 'num_locations']) {
if (!brewery[requiredParameter]) {
return response
.status(422)
.send( { error: `Expected format: { name: <String>, year_est: <Integer>, num_locations: <Integer>}. You're missing a "${requiredParameter}" property` })
}
}
database('breweries').insert(brewery, 'id')
.then(brewery => {
response.status(201).json({ id: brewery[0] });
})
.catch(error => {
response.status(500).json({ error });
})
});
// add a beer to a brewery
app.post('/api/v1/beers', (request, response) => {
const newBeer = request.body;
for (let requiredParameter of ['beer', 'style', 'abv', 'ibu', 'brewery_id']) {
if (!newBeer[requiredParameter]) {
return response
.status(422)
.send({ error: `Expected format: { beer: <String>, style: <String>, abv: <String>, ibu: <Integer>} brewery_id: <Integer>. You're missing a "${requiredParameter}" property` });
}
}
database('beers').insert(newBeer, 'id')
.then(beer => {
response.status(201).json({ id: beer[0] });
})
.catch(error => {
response.status(500).json({ error });
})
});
// delete a brewery and all of its beers
app.delete('/api/v1/breweries/:id', (request, response) => {
const badBrew = request.params;
database('breweries')
.where({ id: badBrew.id })
.select()
.then(brew => {
if (!brew.length) {
return response.status(404).send({ error: 'Could not find the requested brewery, unable to delete.'});
}
})
database('beers')
.where({ brewery_id: badBrew.id})
.del()
.then(() => {
database('breweries')
.where({ id: badBrew.id })
.del()
.then(() => response.status(202).json({ message: 'Successfully deleted the brewery and its beers.'}))
})
.catch(error => {
response.status(500).json({ error });
})
});
// delete a beer
app.delete('/api/v1/beers/:id', (request, response) => {
const badBeer = request.params;
database('beers')
.where({ id: badBeer.id })
.select()
.then(beer => {
if (!beer.length) {
return response.status(404).send({ error: 'Could not find the requested beer, unable to delete.'});
}
})
database('beers')
.where({ id: badBeer.id })
.del()
.then(() => response.status(202).json({ message: 'Successfully deleted the beer.'}))
.catch(error => {
response.status(500).json({ error });
})
});