-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercises_controller.mjs
84 lines (69 loc) · 2.36 KB
/
exercises_controller.mjs
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
import * as exercises from './exercises_model.mjs';
import express from 'express';
const PORT = 3000;
const app = express();
app.use(express.json());
/**
* Create a new movie with the title, year and language provided in the body
*/
app.post('/exercises', (req, res) => {
exercises.createExercise(req.body.name, req.body.reps, req.body.weight, req.body.unit, req.body.date)
.then(exercise => {
res.status(201).json(exercise);
})
.catch(error => {
console.error(error);
res.status(500).json({ Error: 'Request failed' });
})
});
/**
* Retrieve exercises.
*/
app.get('/exercises', (req, res) => {
exercises.findExercises()
.then(exercises => {
res.json(exercises);
})
.catch(error => {
console.error(error);
res.status(500).json({ Error: 'Request failed'})
})
});
/**
* Update the exercise whose id is provided in the path parameter and set
* its name, reps, weight, unit, and date to those provided in the body
*/
app.put('/exercises/:_id', (req, res) => {
exercises.replaceExercise(req.params._id, req.body.name, req.body.reps, req.body.weight, req.body.unit, req.body.date)
.then(numUpdated => {
if (numUpdated === 1) {
res.json({_id: req.params._id, name: req.body.name, reps: req.body.reps, weight: req.body.weight, unit: req.body.unit, date: req.body.date})
} else {
res.status(404).json({ Error: 'Exercise not found' });
}
})
.catch(error => {
console.error(error);
res.status(500).json({ Error: 'Request Failed' });
})
});
/**
* Delete the exercise whose id is provided in the query parameters
*/
app.delete('/exercises/:_id', (req, res) => {
exercises.deleteExercise(req.params._id)
.then(deletedCount => {
if (deletedCount ===1 ) {
res.status(204).send();
} else {
res.status(404).json({ Error: 'Exercise not found'});
}
})
.catch(error => {
console.error(error);
res.send({ error: 'Request failed'});
});
});
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}...`);
});