Full-Stack Web Development with Vue.js and Node

(singke) #1
Building the Real Application Chapter 5

if (error) { console.error(error); }
res.send(movie)
})
})

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

rating.save(function (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 movie = new Movie({
name: req.body.name,
description: req.body.description,
release_year: req.body.release_year,
genre: req.body.genre
})

movie.save(function (error, movie) {
if (error) { console.log(error); }
res.send(movie)
})
})
}

The endpoint saves the user ratings in a separate collection called Rating, which we


haven't created yet. Let's go ahead and do that as well. Create a file called Rating.js


inside the models directory and add the following content:


const mongoose = require('mongoose')
const Schema = mongoose.Schema
const RatingSchema = new Schema({
movie_id: String,
Free download pdf