-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
55 lines (49 loc) · 1.28 KB
/
routes.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
const express = require('express');
const router = express.Router();
const Movie = require('./models/movie');
// Fetch all movies
router.get("/movies", async (req, res) => {
try {
const movies = await Movie.find();
res.send(movies)
} catch(err) {
return res.status(500).json({ message: err.message });
}
})
// Add movie
router.post("/movies", async (req, res) => {
const movie = new Movie({
title: req.body.title,
director: req.body.director,
year: req.body.year
});
try {
const newMovie = await movie.save();
res.status(201).json({ newMovie });
} catch(err) {
return res.status(500).json({ message: err.message });
}
})
// Delete movie by title
router.delete("/movies", async (req, res) => {
await Movie.deleteOne({title: req.body.title}, (err, result) => {
if (err) {
return res.status(500).json({ message: err.message });
}
else {
res.status(200).json(result);
}
});
})
// Update movie by id
router.put("/movies/:id", async (req, res) => {
await Movie.findOneAndUpdate({ _id: req.params.id }, req.body, {new: true}, (err, result) => {
if (err){
return res.status(500).json({ message: err.message });
}
else{
res.status(200).json({ result });
}
});
})
module.exports = router;