Full-Stack Web Development with Vue.js and Node

(singke) #1

Building Authentication with passport.js Chapter 6


});

// fetch a single movie
app.get('/api/movies/:id', (req, res) => {
MovieSchema.findById(req.params.id, 'name description release_year
genre', (error, movie) => {
if (error) { console.error(error); }
res.send(movie);
});
});

// rate a movie
app.post('/movies/rate/:id', (req, res) => {
const newRating = new Rating({
movie_id: req.params.id,
user_id: req.body.user_id,
rate: req.body.rate,
});

newRating.save((error, rating) => {
if (error) { console.log(error); }
res.send({
movie_id: rating.movie_id,
user_id: rating.user_id,
rate: rating.rate,
});
});
});

// add a new movie
app.post('/movies', (req, res) => {
const newMovie = new MovieSchema({
name: req.body.name,
description: req.body.description,
release_year: req.body.release_year,
genre: req.body.genre,
});

newMovie.save((error, movie) => {
if (error) { console.log(error); }
res.send(movie);
});
});
};
Free download pdf