-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
68 lines (60 loc) · 2.68 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
const marathons = require('./marathon-data')
const express = require('express');
const app = express();
app.use(express.json());
app.set('port', 3002);
app.locals.title='Top US Marathons'
app.locals.marathons = marathons.marathons;
app.listen(app.get('port'), () => {
console.log(`${app.locals.title} is up and running on http://localhost:${app.get('port')}.`);
});
app.get('/api/v1/marathons', (request, response) => {
response.status(200).json({marathons: app.locals.marathons})
})
app.get('/api/v1/marathons/:id', (request, response) => {
const id = +request.params.id;
const requestedRace = app.locals.marathons.find(marathon => marathon.id === id);
if (!requestedRace) {
response.status(404).json({error: `Sorry, we couldn't find a race with an id of ${id}`})
}
response.status(200).json({requestedRace})
})
app.post('/api/v1/marathons', (request, response) => {
const requiredProperties = ['name', 'location', 'maxRunners', 'date'];
for (let property of requiredProperties) {
if(!request.body[property]) {
return response.status(422).json({error: `Unsuccessful POST. Property of ${property} must be included.`})
}
};
const { name, location, maxRunners, date } = request.body;
const id = Date.now();
const newMarathon = { id, name, location, maxRunners, date }
app.locals.marathons.push(newMarathon);
response.status(201).json(newMarathon);
})
app.patch('/api/v1/marathons/:id', (request, response) => {
const id = +request.params.id;
const raceToUpdate = app.locals.marathons.find(marathon => marathon.id === id);
if (!raceToUpdate) {
return response.status(404).json({ error: `Sorry, we couldn't find a race with an id of ${id}` })
};
const existingProperties = ['name', 'location', 'maxRunners', 'date'];
const propertyToUpdate = Object.keys(request.body)[0];
if (!existingProperties.includes(propertyToUpdate)) {
return response.status(422).json({error: `Unsuccessful PATCH. Property of ${propertyToUpdate} does not exist.`})
};
const raceToUpdateIndex = app.locals.marathons.indexOf(raceToUpdate)
const updatedRace = raceToUpdate;
updatedRace[propertyToUpdate] = request.body[propertyToUpdate];
app.locals.marathons.splice(raceToUpdateIndex, 1, updatedRace);
response.status(201).json(updatedRace);
})
app.delete('/api/v1/marathons/:id', (request, response) => {
const id = +request.params.id;
const raceToDelete = app.locals.marathons.find(marathon => marathon.id === id);
if (!raceToDelete) {
return response.status(404).json({ error: `Sorry, cannot find a marathon with id of ${id}` })
}
app.locals.marathons = app.locals.marathons.filter(marathon => marathon.id !== id)
response.status(200).json(raceToDelete);
})