Related media list
The related media list will consist of other media records that belong to the same genre as the given video and is sorted by the highest number of views.
Related list API
In order to retrieve the list of related media from the database, we will set up an API on the server that will receive a GET request at '/api/media/related/:mediaId'
.
mern-mediastream/server/routes/media.routes.js
:
router.route('/api/media/related/:mediaId') .get(mediaCtrl.listRelated)
The listRelated
controller method will query the Media collection to find records with the same genre as the media provided, and also exclude this media record from the results returned. The results returned will be sorted by the highest number of views and limited to the top four media records. Each media
object in the returned results will also contain the name and ID of the user who posted the media.
mern-mediastream/server/controllers/media.controller.js
:
const listRelated = (req, res) => { Media.find({ "_id...