From 38065d17511a9404a6b4c8e8a2a4765695a84ce9 Mon Sep 17 00:00:00 2001 From: Sushma Priyadharssini Date: Wed, 3 Oct 2018 00:59:12 +0530 Subject: [PATCH 1/2] load series details from external api --- back-end/controllers/series_movie_controller.js | 11 +++++++++++ back-end/package.json | 3 ++- back-end/routes.js | 1 + 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/back-end/controllers/series_movie_controller.js b/back-end/controllers/series_movie_controller.js index b364088..556398c 100644 --- a/back-end/controllers/series_movie_controller.js +++ b/back-end/controllers/series_movie_controller.js @@ -1,6 +1,7 @@ const mongoose = require('mongoose'); const Series = require('../models').Series; const Movies = require('../models').Movie; +const request = require('request'); module.exports = { series(req, res) { @@ -66,5 +67,15 @@ module.exports = { res.send(series.episodes.id(episodeId)); } }) + }, + + loadAdditionalSeriesDetails(req, res) { + var showName = req.params.name; + request.get("http://api.tvmaze.com/search/shows?q="+ showName, (error, response, body) => { + if(error) { + return console.dir(error); + } + res.send(body); + }); } } diff --git a/back-end/package.json b/back-end/package.json index 126edbf..2cbc3ee 100644 --- a/back-end/package.json +++ b/back-end/package.json @@ -11,7 +11,8 @@ "license": "MIT", "dependencies": { "express": "^4.16.2", - "mongoose": "^4.12.2" + "mongoose": "^4.12.2", + "request": "^2.88.0" }, "devDependencies": { "reload": "^2.2.2" diff --git a/back-end/routes.js b/back-end/routes.js index 45b5311..0859870 100644 --- a/back-end/routes.js +++ b/back-end/routes.js @@ -14,6 +14,7 @@ function initRoutes(app) { app.get('/series/:id/episodes', SeriesMovieController.episodes); app.get('/series/:id/episodes/:eid', SeriesMovieController.episodesByEid); app.get('/movies', SeriesMovieController.movies); + app.get('/search/:name', SeriesMovieController.loadAdditionalSeriesDetails); } module.exports = initRoutes; From d9fb751a728e57779e1ab52f6418c07ef9ccf1fb Mon Sep 17 00:00:00 2001 From: Sushma Priyadharssini Date: Thu, 4 Oct 2018 22:56:57 +0530 Subject: [PATCH 2/2] Created separate service to fetch info from external API --- back-end/controllers/series_movie_controller.js | 9 ++------- back-end/package-lock.json | 9 +++++++-- back-end/package.json | 3 ++- back-end/services/tvmaze.service.js | 13 +++++++++++++ package-lock.json | 14 +++++++------- 5 files changed, 31 insertions(+), 17 deletions(-) create mode 100644 back-end/services/tvmaze.service.js diff --git a/back-end/controllers/series_movie_controller.js b/back-end/controllers/series_movie_controller.js index 19c6c82..27ff8d2 100644 --- a/back-end/controllers/series_movie_controller.js +++ b/back-end/controllers/series_movie_controller.js @@ -1,7 +1,7 @@ const mongoose = require('mongoose'); const Series = require('../models').Series; const Movies = require('../models').Movie; -const request = require('request'); +var TVMazeService = require('../services/tvmaze.service'); module.exports = { series(req, res) { @@ -85,11 +85,6 @@ module.exports = { loadAdditionalSeriesDetails(req, res) { var showName = req.params.name; - request.get("http://api.tvmaze.com/search/shows?q="+ showName, (error, response, body) => { - if(error) { - return console.dir(error); - } - res.send(body); - }); + TVMazeService.loadDataByName(showName).then((data) => res.send(data)); } } diff --git a/back-end/package-lock.json b/back-end/package-lock.json index e0a6ab6..751328b 100644 --- a/back-end/package-lock.json +++ b/back-end/package-lock.json @@ -86,7 +86,7 @@ }, "commander": { "version": "2.9.0", - "resolved": "http://registry.npmjs.org/commander/-/commander-2.9.0.tgz", + "resolved": "https://registry.npmjs.org/commander/-/commander-2.9.0.tgz", "integrity": "sha1-nJkJQXbhIkDLItbFFGCYQA/g99Q=", "dev": true, "requires": { @@ -400,7 +400,7 @@ }, "minimist": { "version": "1.2.0", - "resolved": "http://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=", "dev": true }, @@ -523,6 +523,11 @@ "integrity": "sha1-yobR/ogoFpsBICCOPchCS524NCw=", "dev": true }, + "node-fetch": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.2.0.tgz", + "integrity": "sha512-OayFWziIxiHY8bCUyLX6sTpDH8Jsbp4FfYd1j1f7vZyfgkcOnAyM4oQR16f8a0s7Gl/viMGRey8eScYk4V4EZA==" + }, "on-finished": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz", diff --git a/back-end/package.json b/back-end/package.json index 3246ac1..b7ab1cb 100644 --- a/back-end/package.json +++ b/back-end/package.json @@ -11,7 +11,8 @@ "license": "MIT", "dependencies": { "express": "^4.16.3", - "mongoose": "^5.3.1" + "mongoose": "^5.3.1", + "node-fetch": "^2.2.0" }, "devDependencies": { "reload": "^2.3.1" diff --git a/back-end/services/tvmaze.service.js b/back-end/services/tvmaze.service.js new file mode 100644 index 0000000..299d37d --- /dev/null +++ b/back-end/services/tvmaze.service.js @@ -0,0 +1,13 @@ +const fetch = require('node-fetch'); + +var TVMazeService = { + loadDataByName: loadDataByName +} + +function loadDataByName(showName) { + return fetch("http://api.tvmaze.com/search/shows?q="+ showName) + .then((response) => response.json()) + .catch((err) => console.error(err)) +} + +module.exports = TVMazeService; diff --git a/package-lock.json b/package-lock.json index 2e9bafc..c81617d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -473,7 +473,7 @@ }, "duplexer": { "version": "0.1.1", - "resolved": "http://registry.npmjs.org/duplexer/-/duplexer-0.1.1.tgz", + "resolved": "https://registry.npmjs.org/duplexer/-/duplexer-0.1.1.tgz", "integrity": "sha1-rOb/gIwc5mtX0ev5eXessCM0z8E=", "dev": true }, @@ -1297,7 +1297,7 @@ }, "get-stream": { "version": "3.0.0", - "resolved": "http://registry.npmjs.org/get-stream/-/get-stream-3.0.0.tgz", + "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-3.0.0.tgz", "integrity": "sha1-jpQ9E1jcN1VQVOy+LtsFqhdO3hQ=", "dev": true }, @@ -1339,7 +1339,7 @@ }, "got": { "version": "6.7.1", - "resolved": "http://registry.npmjs.org/got/-/got-6.7.1.tgz", + "resolved": "https://registry.npmjs.org/got/-/got-6.7.1.tgz", "integrity": "sha1-JAzQV4WpoY5WHcG0S0HHY+8ejbA=", "dev": true, "requires": { @@ -1578,7 +1578,7 @@ }, "is-obj": { "version": "1.0.1", - "resolved": "http://registry.npmjs.org/is-obj/-/is-obj-1.0.1.tgz", + "resolved": "https://registry.npmjs.org/is-obj/-/is-obj-1.0.1.tgz", "integrity": "sha1-PkcprB9f3gJc19g6iW2rn09n2w8=", "dev": true }, @@ -1741,7 +1741,7 @@ }, "minimist": { "version": "1.2.0", - "resolved": "http://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=", "dev": true }, @@ -1942,7 +1942,7 @@ }, "pause-stream": { "version": "0.0.11", - "resolved": "http://registry.npmjs.org/pause-stream/-/pause-stream-0.0.11.tgz", + "resolved": "https://registry.npmjs.org/pause-stream/-/pause-stream-0.0.11.tgz", "integrity": "sha1-/lo0sMvOErWqaitAPuLnO2AvFEU=", "dev": true, "requires": { @@ -2418,7 +2418,7 @@ }, "through": { "version": "2.3.8", - "resolved": "http://registry.npmjs.org/through/-/through-2.3.8.tgz", + "resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz", "integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=", "dev": true },