...

/

Adding Routes to Get a Single Movie and its Ratings

Adding Routes to Get a Single Movie and its Ratings

Let's learn how to create routes so we can access resources in the database.

We'll cover the following...

In this lesson, we’ll add two more routes—a route to get a specific movie (with its reviews) and a route to get the ratings of all the movies in the database.

MOVIEREVIEWS_DB_URI=mongodb+srv://Cluster09422:XVNEenFBU3N8@cluster09422.yqome88.mongodb.net/sample_mflix?retryWrites=true&w=majority
MOVIEREVIEWS_NS=sample_mflix
PORT=3000
The initial files we will build on

In the movies.route.js route file, add the two routes shown below:

router.route('/').get(MoviesController.apiGetMovies)
router.route("/id/:id").get(MoviesController.apiGetMovieById)//route to get a specific movie
router.route("/ratings").get(MoviesController.apiGetRatings) // route to get all ratings

Line 2: This route retrieves a specific movie and all reviews associated with that movie.

Line 3: This route returns a list of movie ratings (such as G, PG, R) so that a user can select the ratings from a dropdown menu in the frontend.

Retrieving movies by ID and rating

Next, let’s implement the apiGetMovieById and apiGetRatings methods in MoviesController. Add the following two methods to the movies.controller.js file:

...
static async apiGetMovieById(req,res, next){
try{
let id = req.params.id || {}
let movie = await MoviesDAO.getMovieById(id)
if(!movie){
res.status(404).json({ error: "not found"})
return
}
res.json(movie)
}
catch(e){
console.log(`api, ${e}`)
res.status(500).json({error: e})
}
}
static async apiGetRatings(req,res,next){
try{
let propertyTypes = await MoviesDAO.getRatings()
res.json(propertyTypes)
}
catch(e){
console.log(`api,${e}`)
res.status(500).json({error: e})
}
}
...

Line 6: We first look for an id parameter, which is the value ...